r语言 - 根据Shiny的标准突出显示特定的单元格



我有一个数据表,当值在滑块范围之间时,我想突出显示单元格。在100 - 75之间为red,在btw75 - 50之间为yellow,在btw< 50之间为green。如何解决这个问题?提前感谢。

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)

您可以尝试使用formatStylestyleInterval,并根据您的输入进行依赖。

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')))
})

}

最新更新