I have a data table and I would like to highlight the cells when the value is in between the slider range. red
if the value is between 100 - 75
, yellow
if it is btw 75 - 50
, and green
if it is btw < 50
. How to get around this? Many thanks in advance.
library(shiny)
library(ggplot2) # for the diamonds dataset
diamonds <- diamonds[,c("carat", "depth", "table", "price", "x", "y","z" )]
ui <- fluidPage(
title = "Examples of DataTables",
sidebarLayout(
sidebarPanel(
conditionalPanel(
'input.dataset === "diamonds"',
checkboxGroupInput("show_vars", "Columns in diamonds to show:",
names(diamonds), selected = names(diamonds)),
sliderInput("slider2", label = h3("Slider Range"), min = 0, max = 100,
value = c(25, 75))
)
),
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("diamonds", DT::dataTableOutput("mytable1"))
)
)
)
)
server <- function(input, output) {
# choose columns to display
diamonds2 = diamonds[sample(nrow(diamonds), 1000), ]
output$mytable1 <- DT::renderDataTable({
DT::datatable(diamonds2[, input$show_vars, drop = FALSE])
})
}
shinyApp(ui, server)
CodePudding user response:
You can try using formatStyle
and styleInterval
and make dependent on your inputs.
server <- function(input, output) {
# choose columns to display
diamonds2 = diamonds[sample(nrow(diamonds), 1000), ]
output$mytable1 <- DT::renderDataTable({
formatStyle(datatable(diamonds2[, input$show_vars, drop = FALSE]),
input$show_vars,
backgroundColor = styleInterval(c(input$slider2[1], input$slider2[2]),
c('green', 'yellow', 'red')))
})
}