r语言 - formatStyle in renderDataTable in shiny



我试图从DT包创建一个带有数据表的shinyApp。当一个字段在表中为NA/空白时,我想用红色突出显示它。

我使用虹膜数据集的一部分创建了一个可重复的示例,并创建了一些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: exprFunc102: widgetFunc101: htmlwidgets:: shinyRenderWidget100:函数87: renderFunc86: renderFunc82: renderFunc81:输出output_1美元1: runApp

有人能帮忙吗?

你的语法错误。生成的数据表如下:

library(DT)
df <- iris[c(1:10),]
df[c(5:10),1] <- NA
datatable(
df,
options = list(
......
)
) %>% 
formatStyle("Sepal.Length", backgroundColor = styleEqual(NA, "red"))

然后你要做

output[["output_1"]] <- renderDT({
datatable(
df,
options = list(
......
)
) %>% 
formatStyle("Sepal.Length", backgroundColor = styleEqual(NA, "red"))
}, server = FALSE)

最新更新