0

I have already constructed a table in excel, through R. This table has n rows and m columns (the number of columns and rows can change). All the columns are numeric and have integer numbers from 1:nrow(). I'm trying to conditional format the table, i.e. having a background scale color palete from green (value=1) to red (value=nrow()), going too through yellow. How can I accomplish this? My code:

for(col in 1:ncol(df)){
    
    col_vals <- na.omit(df[[col]])
    
    # Define the color gradient for the column
    min_val <- min(col_vals)
    max_val <- max(col_vals)
    n <- max_val - min_val + 1
    gradient <- colorRampPalette(c("green", "yellow", "red"))(n)
    
    # Map the values to colors in the gradient
    col_colors <- gradient[col_vals - min_val + 1]
    
    # Add the colors to the workbook
    createStyle(fontSize = 10, fontName = 'Arial', 
                halign = "center", valign = "center", bgFill = col_colors) -> color_style
    
    addStyle(wb, sheet, style = color_style, 
             rows = (9+nrow(df)+2):(9+nrow(df)+2)+nrow(df), 
             cols = 2 + col)
    
  }

The warning message: "In stri_join(names(fill$fillBg), "="", fill$fillBg, """) : argument is not an atomic vector; coercing" The problem is that, by running this all my previous excel formatting work(including for other previously created sheets) goes all away. And the palette is not being applied.

0 Answers0