Steve Kaszycki Steve Kaszycki - 11 months ago 73
R Question

Set background color of columns in Shiny tableOutput based on another vector

I have a matrix of data, which I will display in a Shiny app in tableOutput format. And I have a vector of colornames determined exogenous to the code. I need to set the background of column[i] to color[i].

For instance, if I have the following:

helpme <- data.frame(matrix(rnorm(200),nrow=20))
helpme.colors <- c(rep("white",6),

Then, what I am looking to do is something that'd function like this (R'ish pseudocode):

for(i in 1:dim(helpme)[2]){
BackgroundColor(helpme[,i]) <- helpme.colors[i]

I imagine the answer is a fairly straighforward javascript loop but I haven't been able to find it yet (I have minimal experience in javascript).

Answer Source

You can use some r package

1) DT

for (i in  1:ncol(helpme)){

2) htmlTable

htmlTable::htmlTable(helpme,css.cell=sapply(1:ncol(helpme),function(i) paste0("background-color:",helpme.colors[i],"")))

PS there is problem with "chocolate4"