jack ryan
jack ryan

Reputation: 59

Changing the color of the datatable rows based on multiple conditions

I have a data table with many rows and for the sake of convenience Ill just use only two columns. Below are the two columns in the datatable.

rawdata <- data.frame(
  id = c(1,1,2,2,2,3,3),
  time = c(45,92,30,100,79,199,248)
)

I want to color the entire row of the dt based on certain conditions.

  1. If id=1 , I should be able to apply backgroundColor = styleInterval(c(30,50), c('#C6EFCE', '#FFEB9C','#FFC7CE'))

  2. If id=2 , I should be able to apply backgroundColor = styleInterval(c(45,90), c('#e60000, '#000000','#ffffff))

  3. If id=3 , I should be able to apply backgroundColor = styleInterval(c(x,y), c('a, 'b,'c))

Please find my current code for the datatable

library(DT)
datatable(
  rawdata,
  rownames = FALSE,
  options=list(
    pageLength = 20,
    scrollX = TRUE, 
    dom = 'prlti',
    initComplete =JS(
      "function(settings, json) {",
      "$(this.api().table().header()).css({'font-size': '20px','background-color': '#000', 'color': '#fff'});",
      "}"),
    columnDefs = list(list(className = 'dt-center', targets ="_all")),autowidth=T)
) %>%
  formatStyle(
    colnames(rawdata)[1:NCOL(rawdata)],target = 'row',
    # color =styleInterval(c(30,35), c('black', 'black', 'black')),
    backgroundColor =styleInterval(c(48,120), c('#C6EFCE', '#FFEB9C','#FFC7CE'))

  ) %>%
  formatStyle(columns = c(1:NCOL(rawdata)),'font-size' = '25px',fontWeight = 'Bold')

Upvotes: 2

Views: 3023

Answers (1)

A. S. K.
A. S. K.

Reputation: 2816

In this situation, I would store the color you want in each row of the table itself, and then use styleEqual to set the color of each row. (We'll also want to hide the column that contains the color.)

Add a column with the color you want to display for each row:

library(dplyr)
rawdata <- data.frame(
  id = c(1,1,2,2,2,3,3),
  time = c(45,92,30,100,79,199,248)
) %>%
  mutate(row.color = case_when(id == 1 & time <= 30 ~ "#C6EFCE",
                               id == 1 & time <= 50 ~ "#FFEB9C",
                               id == 1 ~ "#FFC7CE",
                               id == 2 & time <= 45 ~ "#E60000",
                               id == 2 & time <= 90 ~ "#000000",
                               id == 2 ~ "#FFFFFF",
                               T ~ "#888888"))

(I used gray for id = 3 since the original post omits the actual colors for that condition.)

Use styleEqual to set cell colors in the time column and columnDefs to hide the column with color hex values:

library(DT)
datatable(
  rawdata,
  rownames = FALSE,
  options=list(
    pageLength = 20,
    scrollX = TRUE, 
    dom = 'prlti',
    initComplete =JS(
      "function(settings, json) {",
      "$(this.api().table().header()).css({'font-size': '20px', 'background-color': '#000', 'color': '#fff'});",
      "}"),
    columnDefs = list(list(className = 'dt-center', targets = "_all"),
                      list(targets = 2, visible = F)),
    autowidth = T)
) %>%
  formatStyle(
    c("time"), "row.color",
    backgroundColor = styleEqual(sort(unique(rawdata$row.color)), sort(unique(rawdata$row.color)))

  ) %>%
  formatStyle(columns = c(1:NCOL(rawdata)),'font-size' = '25px',fontWeight = 'Bold')

We get the following result:

enter image description here

If you want to render white text in black rows, you can add yet another column with text.color and use styleEqual with the color option too.

Upvotes: 2

Related Questions