I am building a Shiny app in R to display multiple data frames, 6 in total on 1 Shiny page. I am able to conditional format 1 data table in Shiny, but I am unable to conditionally format all 6 tables. Each of the 6 tables have 5 variables and I want the 2nd variable in every data table: background light up red and bold if >= 75, and light up green and bold if <= 75:
library(shiny)
ui <- fluidPage(
titlePanel("xxx"),
title = 'xx',
fluidRow(
column(6
, fluidRow(
column(6, DT::dataTableOutput('1'), style = "font-size:
75%; width: 50%"),
column(6, DT::dataTableOutput('2'), style = "font-size:
75%; width: 50%")
),
# hr(),
fluidRow(
column(6, DT::dataTableOutput('3'), style = "font-size:
75%; width: 50%"),
column(6, DT::dataTableOutput('4'), style = "font-size:
75%; width: 50%")
),
# hr(),
fluidRow(
column(6, DT::dataTableOutput('5'), style = "font-size:
75%; width: 50%"),
column(6, DT::dataTableOutput('6'), style = "font-size:
75%; width: 50%")
)
)
server <- function(input, output) {
output$1 = DT::renderDataTable(1
, server = FALSE, selection = 'single'
, options = list(rowCallback = JS('function(nRow
, aData, iDisplayIndex, iDisplayIndexFull) {
// Bold and green cells for conditions
if (parseFloat(aData[2])
>= 75 | parseFloat(aData[2]) <= -75)
$("td:eq(2)", nRow).css("font-weight", "bold");
if (parseFloat(aData[2]) >= 75)
$("td:eq(2)", nRow).css("background-color"
, "#FF0000");
else if(parseFloat(aData[2]) <= -75)
$("td:eq(2)", nRow).css("background-color",
"#00FF00");
else
$("td:eq(2)", nRow).css("background-color"
, "#FFFFFF");
}'
)
, drawCallback = JS()
))
output$2 = DT::renderDataTable(2, server = FALSE,
selection = 'single')
output$3 = DT::renderDataTable(3, server = FALSE,
selection = 'single')
output$4 = DT::renderDataTable(4, server = FALSE,
selection = 'single')
output$5 = DT::renderDataTable(5, server = FALSE,
selection = 'single')
output$6 = DT::renderDataTable(6, server = FALSE,
selection = 'single')