Put color a specific cell when export to excel

Hi community
Im have a data set with many NO EXISTE values because is the output of different merges.

In need export to excel and put one color in this specific cell for make more visual the interpretation.

DT <- data.frame(name = c("ARG","NO EXISTE","COL","NO EXISTE"),
                 value= c(1,2,5,4))

This is an example of expected output

image

Tnks!

The openxlsx library is great for adding formating to the files!
See: Add conditional formatting to cells — conditionalFormatting • openxlsx

library(openxlsx)

DT <- data.frame(name = c("ARG","NO EXISTE","COL","NO EXISTE"),
                 value= c(1,2,5,4))

# define style for header
HeaderStyle = createStyle(fontColour = "#000000", fontSize = 12, 
                          halign = "center", valign = "center", textDecoration = "Bold",
                          border = "Bottom",
                          borderStyle = getOption("openxlsx.borderStyle", "thin"),
                          wrapText = TRUE)

# define the Style for the highlights 
CellHighlight = createStyle(fontColour = "black", bgFill = "#7ee57c")

# Create new workbook
wb = createWorkbook(title = "Results")

# Add Sheet 1
addWorksheet(wb, "Results")

# Add the data
writeData(wb, "Results", DT,
          headerStyle = HeaderStyle)


# highlight missings for column 1
conditionalFormatting(wb, "Results", 
                      cols = 1, 
                      rows = 1:(nrow(DT)+1), 
                      rule= '=="NO EXISTE"', 
                      style = CellHighlight)

# OR: expand highlight to more columns
conditionalFormatting(wb, "Results", 
                      cols = 1:3, 
                      rows = 1:(nrow(DT)+1), 
                      rule= '$A1 ="NO EXISTE"', 
                      style = CellHighlight)

# Show the workbook
openXL(wb)

# Save the workbook
saveWorkbook(wb, "Results_Export.xlsx", overwrite = TRUE)
1 Like

Is good. :muscle:t4:
One things more, how put only the NO EXISTE cells, because I want highlight only this cells.

Like this:
image

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.