Cez2020
Cez2020

Reputation: 21

Rule-based Conditional Formatting in R (openxlsx)

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)

Upvotes: 2

Views: 1366

Answers (1)

Darren Tsai
Darren Tsai

Reputation: 35554

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

Upvotes: 2

Related Questions