datatable - 在 Shiny 中更改 DT 表上单元格的颜色
问题描述
我是 Shiny/R 的新手,并试图根据单元格的值更改单元格(DT 表)的背景颜色。例如,Rec_Color 列上的单元格值为 RED、GREEN 和 YELLOW。我想根据字符串值为单元格着色。我尝试使用“formatStyle”功能,但它不适合我。我收到此错误:
错误。您指定了列:Rec_Color,但数据的列名是
这是我的表(和过滤器输入)的代码:
output$spTable <- DT::renderDataTable ({data <- TrustSp_Data # Code for the trust species table and the selecInputs
if (input$Tax != "All") {
data <- data[data$Taxon == input$Tax,] # selectInput for Taxa
}
if (input$Rcolor != "All") {
data <- data[data$Rec_Color == input$Rcolor,] # selectInput for Recovery color
}
if (input$Cstat != "All") {
data <- data[data$Status == input$Cstat,] # selectInput for conservation status ( T&E, At-risk, etc...)
}
if (input$Rtime != "All") {
data <- data[data$Rec_Time == input$Rtime,] # selectInput for Recovery Timeline (1:6)
}
if (input$Autho != "All") {
data <- data[data$Authority == input$Autho,] # selectInput for federal Authority ( ESA, MBTA, etc...)
}
data
}, rownames=FALSE, #remove first column row numbers
extensions = c('ColReorder','Responsive',"FixedHeader"), # add "extensions = "Responsive" to add large number of columns
# caption = "Table 1: This is a sample caption for the table.", # Add caption at the top
caption = htmltools::tags$caption( # Add caption at the bottom of the table
style = 'caption-side: bottom; text-align: center;',
'Dataset:', htmltools::strong("Version 03-January 10, 2019")),
colnames = c("ID"=1,"Species Name"=3,"Scientific Name"=4, "Sustainability Color"=7, "Sustainability Timeline"=8, "ITIS ID"=9), # Change columns names
options = list(
fixedHeader = TRUE,
scrolly = TRUE,
colReorder = TRUE,
columnDefs = list(list(className = 'dt-center', targets = c(0,1,7))), # columns aligment to center
language = list(sSearch = "Search Table:"),
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#22415e', 'color': '#fff'});",
"}")
) %>% formatStyle(columns = "Rec_Color",
backgroundColor = styleEqual(
c("GREEN", "RED", "YELLOW"), c('green', "red", 'yellow'))
)
解决方案
尽管您没有提到数据的样子,但我相信解决方案是更改以下行
formatStyle(columns = "Rec_Color",
backgroundColor = styleEqual(
c("GREEN", "RED", "YELLOW"), c('green', "red", 'yellow'))
至
formatStyle(columns = "Sustainability Color",
backgroundColor = styleEqual(
c("GREEN", "RED", "YELLOW"), c('green', "red", 'yellow'))
原因是您通过指定colnames=
选项更改了列名。
您的代码中还有另一个问题,即该formatStyle()
函数被调用DT::renderDataTable()
但它应该是DT::datatable()
. 因此,您还应该将代码修改为:
output$spTable <- DT::renderDataTable ({data <- TrustSp_Data # Code for the trust species table and the selecInputs
if (input$Tax != "All") {
data <- data[data$Taxon == input$Tax,] # selectInput for Taxa
}
if (input$Rcolor != "All") {
data <- data[data$Rec_Color == input$Rcolor,] # selectInput for Recovery color
}
if (input$Cstat != "All") {
data <- data[data$Status == input$Cstat,] # selectInput for conservation status ( T&E, At-risk, etc...)
}
if (input$Rtime != "All") {
data <- data[data$Rec_Time == input$Rtime,] # selectInput for Recovery Timeline (1:6)
}
if (input$Autho != "All") {
data <- data[data$Authority == input$Autho,] # selectInput for federal Authority ( ESA, MBTA, etc...)
}
DT::datatable(
data, rownames=FALSE, #remove first column row numbers
extensions = c('ColReorder','Responsive',"FixedHeader"), # add "extensions = "Responsive" to add large number of columns
# caption = "Table 1: This is a sample caption for the table.", # Add caption at the top
caption = htmltools::tags$caption( # Add caption at the bottom of the table
style = 'caption-side: bottom; text-align: center;',
'Dataset:', htmltools::strong("Version 03-January 10, 2019")),
colnames = c("ID"=1,"Species Name"=3,"Scientific Name"=4, "Sustainability Color"=7, "Sustainability Timeline"=8, "ITIS ID"=9), # Change columns names
options = list(
fixedHeader = TRUE,
scrolly = TRUE,
colReorder = TRUE,
columnDefs = list(list(className = 'dt-center', targets = c(0,1,7))), # columns aligment to center
language = list(sSearch = "Search Table:"),
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#22415e', 'color': '#fff'});",
"}")
)
) %>% formatStyle('Sustainability Color', backgroundColor = styleEqual(c("RED","GREEN","YELLOW"), c('red',"green","yellow")))
})
我希望这能解决你的问题。
顺便说一句,在提问时,该示例最好是可重现的。您的示例无法重现,因为 data.frame 是未知的,并且代码只是大型闪亮应用程序的一部分,无法直接运行。
推荐阅读
- django - DjangoModelFactory 中的日期时间字段破坏了集成测试 (2.1)
- c# - 为什么在性能分析器中禁用内存使用部分?
- reactjs - 自定义样式到使用 react-json 模式表单创建的表单
- ios - WKInterfaceController 的 becomeCurrentPage 没有按预期工作
- vb.net - 将ip地址从静态更改为动态
- java - 根据实时纬度和语言 Firebase 更改标记位置,不带 (Mmap.clear) 或 if (marker != null)
- sql - 比较两列时,有没有办法将 Case 函数与 nvarchar 数据类型一起使用?
- google-app-maker - 使用变量计算减法的表达式的语法
- cordova - 如何减少 ionic 4 应用程序加载时间?
- vue.js - 更改路线和调整窗口大小后,Slick Carousel 的高度降至零