Rule-based Conditional Formatting in R (openxlsx)

Viewed 62

I'm trying to use the conditionalFormatting() function from the openxlsx package in R. I would like the function to compare date values in two columns (A and B), and color the cell value in B only if the value is smaller than A. Is this a stretch?

conditionalFormatting(wb, "Sheet1", cols=12, rows=1:nrow(data), rule = 'B<A', style = myStyle)
1 Answers

You could use rule = "$A2>$B2":

df <- data.frame(x = 1:6, y = 1:6 + c(-1, 1))
wb <- createWorkbook()
addWorksheet(wb, "sheet1")
writeData(wb, "sheet1", df)
conditionalFormatting(wb, "sheet1", cols = 2, rows = 2:(nrow(df)+1), rule = "$A2>$B2")
saveWorkbook(wb, "conditionalFormattingExample.xlsx", TRUE)

enter image description here

Reference

https://ycphs.github.io/openxlsx/reference/conditionalFormatting.html

Related