user9532692
user9532692

Reputation: 726

Changing cell color of R shiny data table dependent on its value

I am trying to color cells that meet certain conditions. For example, I would like to color cells whose value is equal to its respective column values of the first row of the data. I figured how to color specific values using javascript but can't figure out how to set the conditions as what I want. Here's the code:

library(DT)
df <- head(iris)
df %>% 
  datatable %>% 
  formatStyle(1:2, color = JS("value % 1 === 0 ? 'red' : ''"))

In this table, the highlighted cells are the ones whose values match the value of the column of the first row of the data. I would like to set javascript conditions as such that it will highlight the cells as shown in the image.

example

Upvotes: 2

Views: 1341

Answers (1)

R&#233;mi Coulaud
R&#233;mi Coulaud

Reputation: 1714

If you use styleEqual function, it almost work except that it underlines also the reference row :

library(DT)
df <- head(iris)
ref <- df[1,]
color <- "orange"
datatable(df) %>% 
    formatStyle(1:5,
                backgroundColor = styleEqual(ref[1:5], rep(color, 5)))

Upvotes: 1

Related Questions