I've been trying to implement these codes [1], [2] to use the backgroundcolor
argument from datatable
to conditionally change the bg color if result_post > result_pre
. However, something is going on and I imagine someone will be able to fix this code, which partially mimics the original one (which uses reactive values).
The goal is
library(shiny)
library(tidyverse)
library(DT)
table_math <- data.frame(age = c(5,10), test = "math", result_pre = rnorm(100,10,2), result_post = rnorm(100,11,1))
ui <- fluidPage(
sidebarLayout(
sidebarPanel( ),
mainPanel(dataTableOutput("main_results"))
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
#backend
table_with_results <- reactive({
datatable(
table_math %>%
select(result_post, result_pre)
,
#format datatable
options = list(
dom = 't',
pageLength = 200
),
rownames = FALSE) %>%
formatStyle(columns = "result_pre",
backgroundColor = styleInterval( 1, #here is the goal: instead of 1 if result_post > result_pre
c("red","green")))
})
#real output
output$main_results <- renderDataTable(
table_with_results()
)
}
# Run the application
shinyApp(ui = ui, server = server)