1
votes

There is a great example: https://rstudio.github.io/DT/010-style.html that allows creating 19 breaks and 20 rgb color values ranging from "white" to "red":

library(DT)

# 1. Data
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))

# 2. Create 19 breaks and 20 rgb color values ranging from white to red
brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
clrs <- round(seq(255, 40, length.out = length(brks) + 1), 0) %>%
    {paste0("rgb(255,", ., ",", ., ")")}

# 3. DT
datatable(df) %>% formatStyle(names(df), backgroundColor = 
 styleInterval(brks, clrs))

But how make the same colorizing from "GREEN" to "RED" ?

Thanks!

1

1 Answers

3
votes

You could use colorRampPalette:

ramp <- colorRampPalette(c("red", "green"))

and then select the number of colors from length(brks)+1:

clrs <- ramp(length(brks)+1)

Is this what you had in mind?

datatable with color ramp red to green