The problem is that the styleColorBar function creates some Javascript code to create the background based on range(pval_data) , but this code applies to the values created by datatable, in this case a .
One trick can be cbind a and pval_data , and pass this to the output so that all the data necessary to complete your actions is transferred to the browser.
You can then color the background of the first five columns ( a in this case) according to the values in the last five columns ( pval_data ) and hide the last 5 columns if you do not want them to be displayed.
Here is an example:
library(DT) library(shiny) server <- function(input, output) { a<-reactive({ data.frame(matrix(1, nrow=input$obs, ncol=5)) }) pval_data <- reactive({ data.frame(matrix(rnorm(n = input$obs*5), ncol=5)) }) output$pivot_table = DT::renderDataTable( datatable(cbind(a(),pval_data()), options = list(columnDefs = list(list(targets = 6:10, visible = FALSE)),rowCallback = JS( paste0("function(row, data) { for (i = 1; i < 6; i++) { value = data[i+5] if (value < ",input$cutoff,") backgroundValue =",styleColorBar(range(pval_data()), 'lightblue')[1]," else backgroundValue =",styleColorBar(range(pval_data()), 'red')[1]," $('td', row).eq(i).css('background',backgroundValue); $('td', row).eq(i).css('background-repeat','no-repeat'); $('td', row).eq(i).css('background-position','center'); $('td', row).eq(i).css('background-size','98% 88%') } }")) ))) } ui <- shinyUI(fluidPage( sidebarLayout( sidebarPanel( sliderInput("obs", "Number of observations:", min = 5, max = 20, value = 10), sliderInput("cutoff", "Cutoff:", min = -5, max = 5, value = 0,step=0.5) ), mainPanel(dataTableOutput('pivot_table') ) ))) shinyApp(ui = ui, server = server)
In the options part of the datatable , columnDefs used to hide the last 5 columns and rowCallback to color the background. With this code, the background will be lightblue if the values are less than 0 and red if it is above 0.