RanonKahn
RanonKahn

Reputation: 862

Not able to style data table in R Shiny

I am trying to style ELISA data (numeric) table as described here and I get the following error for the code

brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE) 

enter image description here

I used the following code as described in the web page. Can someone please point out to me what am I missing here?

df <- matrix(nrow=8, ncol=12)
for (i in 1:8) {
  for (j in 1:12)
    df[i,j] <- format(as.numeric(elisa65[i,j])/as.numeric(elisa74[i,j]),digits = 4)
}

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,", ., ",", ., ")")}

DT::datatable(df)  %>% formatStyle(names(df), backgroundColor = styleInterval(brks, clrs))

ELISA Data Table

Upvotes: 0

Views: 469

Answers (1)

Raad
Raad

Reputation: 2715

I noticed two issues in your code:

  1. clrs is a character and you aren't actually evaluating the calls to rgb
  2. df is a matrix and you are treating it like a data.frame in your code above

Try this out

require(dplyr)
require(DT)
df <- matrix(rnorm(8 * 12), nrow=8, ncol=12)

brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
clrs <- sapply(round(seq(255, 40, length.out = length(brks) + 1), 0), 
               function(x) rgb(255, x, x, maxColorValue = 255))

df <- data.frame(df)
datatable(df)  %>% formatStyle(names(df), backgroundColor = styleInterval(brks, clrs))

Upvotes: 3

Related Questions