I've noticed something unusual with the conditionalFormatting() function in the Openxlsx package. When you specify a vector of columns, such as c(2,4,6), the function doesn't highlight those three columns specifically, but rather all columns from #2-#6. Consider the code below:
df <- data.frame(One = c('Dog', 'Dog'),
Two = c('Cat', 'Cat'),
Three = c('Bird', 'Bird'),
Four = c('Cow', 'Cow'),
Five = c('Horse', 'Horse'),
Six = c('Lion', 'Lion'),
Seven = c('Tiger', 'Tiger'))
style1 <- createStyle(fontName = 'Calibri',
fontSize = 11,
bgFill = "#FFC7CE")
conditionalFormatting(wb,
sheet,
rows = 1:nrow(df),
cols = c(2,4,6), #<--- treated as all columns in range 2-6
type = 'contains',
rule = "A", #<---highlight all rows that contain text
style = style1)
However, if you were to use the addStyle() function, then it seems as if it would color those three columns specifically, rather than the entire range:
style2 <- createStyle(fontName = "Calibri",
fontSize = 11,
fgFill = "#FFC7CE")
addStyle(wb,
sheet,
rows = 1:nrow(df),
cols = c(2,4,6), #<---treated as columns 2,4,6
style = style2,
gridExpand = TRUE)
I've tried using a for loop to see if it tricks it into formatting them correctly, but it doesn't.
cols.to.format <- c(2,4,6)
for(col in cols.to.format){
conditionalFormatting(wb,
sheet,
rows = 1:nrow(df),
cols = col,
style = style2,
gridExpand = TRUE)
}
Is there a way to use conditional formatting to format a specific segment of columns rather than a range?

