Im using openxlsx library in R. Excel output of my code is not considering 0.000 format and in my excel output instead of numbers like -0.019 I can see only 0 or 1 (underneath they have good numbers).
my dataset:
formatting_rows <- data.frame(
driver_group = c(1, 2),
brand = c("ALNATURA", "ALNATURA"),
apple = c(-0.0560447485, -0.0364285838),
banana = c(0.566816508, 0.956636920),
pear = c(-2.655737e-02, -2.264970e-02),
carrot = c(-0.0404093701, 0.0434975415),
celery = c(0.40294248, 0.41057630),
cucumber = c(-1.433510e-02, 3.704044e-03),
highlight_fruits = c(TRUE, FALSE),
highlight_veggies = c(TRUE, FALSE))
My code:
wb = createWorkbook(title = "workbook")
options("openxlsx.numFmt" = "0.000")
CellHighlight = createStyle(fontColour = '#b54c4a', textDecoration = "italic")
addStyle(wb, tab_name, CellHighlight,
rows = c(which(formatting_rows$highlight_fruits))+1,
cols = which(names(data) %in% c("apple", "banana", "pear")),
gridExpand = TRUE, stack = TRUE)
addStyle(wb, tab_name, CellHighlight,
rows = c(which(formatting_rows$highlight_veggies))+1,
cols = which(names(data) %in% c("carrot", "celery", "cucumber")),
gridExpand = TRUE, stack = TRUE)
I tried to add numFmt = "0.000" to CellHighlight (not worked)
CellHighlight = createStyle(fontColour = '#b54c4a', textDecoration = "italic", numFmt = "0.000")
I tried also to add styles:
addStyle(wb, tab_name, createStyle(numFmt = "0.000"),
rows = c(which(formatting_rows$highlight_fruits))+1,
cols = which(names(data) %in% c("apple", "banana", "pear")),
gridExpand = TRUE, stack = TRUE)
addStyle(wb, tab_name, createStyle(numFmt = "0.000"),
rows = c(which(formatting_rows$highlight_veggies))+1,
cols = which(names(data) %in% c("carrot", "celery", "cucumber")),
gridExpand = TRUE, stack = TRUE)
This is working, but when I'm using it on other data or on the same code next day is going back o 0 or 1 format. The cells are always highlighted in red. I'm writing data before addStyle, the stack = TRUE. I don't know what to do next.
The quickest way I could think of would be to use conditional formatting like this in
openxlsx2(sorry, no clue what happens in youropenxlsxexample).I switched the highlighting column in your example data to different rows. If you really need cell coloring there are other ways to do this (but this should be the quickest), especially if you only want to highlight non consecutive cells in the data set.