【发布时间】:2021-11-21 09:24:45
【问题描述】:
我正在尝试使用 DT 包中的数据表创建 shinyApp。 每当表格中的字段为 NA/空白时,我想用红色突出显示它。
我使用部分 iris 数据集创建了一个可重现的示例并创建了一些 NA 值。
# Loading iris dataset, shortening and creating NA in ‘Sepal.Length‘ variable
data(iris)
iris
df <- iris[c(1:10),]
df[c(5:10),1] <- NA
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput(outputId = "output_1")
)
server <- function(input, output) {
output$output_1 <- renderDataTable(server=FALSE, datatable({
iris %>% filter(Petal.Length>1.4) %>% formatStyle(
'largest_dimension',
background = styleColorBar(is.na(df$Sepal.Length), "red"),
backgroundSize = '100%',
backgroundRepeat = 'no-repeat',
backgroundPosition = 'center'
)},
extensions = 'Buttons',
options = list(
pageLength=10,
lengthMenu=c(10,20,50,100),
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Blfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
),
class = "display"))
}
shinyApp(ui = ui, server = server)
但是,代码不起作用。我收到以下错误: 警告:formatColumns 中的错误:无效的表参数;预期从 datatable() 创建的表对象 106:%>% 103:表达式函数 102:小部件函数 101: htmlwidgets::shinyRenderWidget 100:功能 87:渲染函数 86:渲染函数 82:渲染函数 81:输出$输出_1 1:运行应用程序
有人可以帮忙吗?
【问题讨论】:
标签: r shiny datatables dt