Conditional formatting using openxlsx and createStyle()

32 Views Asked by At

I would like to create conditional formatting for a correlation matrix where cells with correlation coefficients 0.75 to 1 and -0.75 to -1 have green backgrounds with black font (see provided code). However, when I export wb as Excel, all cells in the specified value range have default createStyle() colours (Default is createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")). I tried to use fgFill instead of bgFill, use only one argument for createStyle() or use only one createStyle() in "corr" sheet which did not help. No matter what I do I can't change the default cell background and font colour. Does anybody have an idea what am I doing wrong?

wb <- createWorkbook()
addWorksheet(wb, sheetName = "corr")
writeData(wb, sheet = "corr", DF, rowNames = F)
conditionalFormatting(wb, "corr",
                      cols = 1:ncol(DF),
                      rows = 1:nrow(DF),
                      type = "between",
                      rule = c(-1, -0.75),
                      operator = createStyle(fontColour = "#191515", bgFill = "#99BC85")
                      )
conditionalFormatting(wb, "corr",
                      cols = 1:ncol(DF),
                      rows = 1:nrow(DF),
                      type = "between",
                      rule = c(1, 0.75),
                      operator = createStyle(fontColour = "#191515", bgFill = "#99BC85")
                      )
1

There are 1 best solutions below

0
Rizek_a_salat On

OK, so my mistake. Instead of using the operator argument, there was supposed to be style argument. After this change, the code works as intended.