I have a matrix M with positive values and negative values. I have another matrix X (dim(X) = dim(M)). I am trying to display X as a table in shiny app, using the DT package. I would like to display the matrix X with different colors conditioned by the values of M. It means: color1 in cells of X where M > 0 (X[M>0]) , color2 in cells of X where M<0 (X[M<0]) and color3 in cells of X where M == 0 (X[M == 0]).
The next code shows where I got stuck: X <- matrix(c(1:9), 3) M <- matrix(c(-3:2), 3) # The matrix is more complex and it's created in a reactive environment. Here is only an example
X_colors <- reactive({
DT::datatable(X()) %>%
formatStyle(
columns = c(1:3),
valueColumns(¿How do reference to M matrix?),
backgroundColor = styleInterval(c(-1,0), c("lightgreen",
"lightred", "lightblue")
))
})
output$X_table_2 <- DT::renderDataTable(X_colors())
thanks !!
What you ask for is possible but not very straightforward. My approach includes using hidden columns. My "recipe":
cbind
the "conditions-matrix" M and the "values-matrix" X,options
argument indatatable
.valueColumns
and the non-hidden columns ascolumns
inDT::formatStyle
Notice that I used
targets = 0:1
to hide columns1:2
since this parameter follows javascript syntax where indexing begins with0
rather than1
.