css - How to color specific cells in a Data Frame / Table in R? -


i color specific cells in following dataframe. example, in inputval column, highlight cells in range of [0.8, 0.9) magenta, , cells in same column in range of [0.7, 0.8) blue. similarly, i'd output column cells value of 1 colored magenta , value 4 colored blue. rest of cells in dataframe, them remain white.

i have following reproducible code highlights row only, , limits me coloring in magenta , white only. how can add color , cell?

set.seed(123) df <- data.frame(id       = sample(1:100, 20, replace = true),                  inputval = sample(seq(0, 1, by=0.01), 20, replace = true),                  outcome  = sample(1:4, 20, replace = true))  cols <- with(df, ifelse(outcome == 1, 'magenta', 'white'))  library('htmltable') htmltable(as.matrix(df), col.rgroup = cols) 

i realize issue adding different colors ifelse call in with limits me magenta , white. how can add condition here?

while know what's causing multiple color issue, i'm pretty clueless how color specific cells.

this same example the accepted answer question. thanks!

have considered dt?

library(dt) datatable(df, rownames = false) %>%   formatstyle(columns = "inputval",                background = styleinterval(c(0.7, 0.8, 0.9)-1e-6, c("white", "lightblue", "magenta", "white"))) %>%   formatstyle(columns = "outcome",                background = styleequal(c(1, 4), c("magenta", "lightblue")))  

enter image description here


Comments