r语言 - 从反应表中访问列名,以便使用 DT 包进行格式化



我正在尝试在一个闪亮的应用程序中格式化一个反应式表格,并使用DT包显示它。关于如何使用非反应表执行此操作,有很多示例,例如:https://rstudio.github.io/DT/010-style.html。但是当我尝试以相同的方式使用响应式函数的输出时,我收到列名错误。下面是一个示例:

library(shiny)
library(DT)

ui <- fluidPage(
fluidRow(
column(3,numericInput("nrows","Enter the number of rows",value=10)),
column(9,DT::dataTableOutput("table"))
)
)
server <- function(input, output, session) {
cars2 <- reactive({
cars <- datasets::cars
cars2 <- cars[1:input$nrows,]
return(cars2)
})
output$table <- DT::renderDataTable(cars2()%>% formatStyle( 'speed',backgroundColor = c('yellow')),options=list(pageLength=50))

}

shinyApp(ui=ui,server=server)

这是错误

Warning: Error in name2int: You specified the columns: speed, but the column names of the data are 
Stack trace (innermost first):
94: name2int
93: appendFormatter
92: formatColumns
91: formatStyle
90: function_list[[k]]
89: withVisible
88: freduce
87: _fseq
86: eval
85: eval
84: withVisible
83: %>%
82: exprFunc
81: widgetFunc
80: func
79: origRenderFunc
78: renderFunc
77: origRenderFunc
76: output$table
1: runApp

这有效:

server <- function(input, output, session) {
cars2 <- reactive({
cars <- datasets::cars
cars2 <- cars[1:input$nrows,]
return(cars2)
})
output$table <- renderDataTable({
datatable(cars2(), options = list(pageLength = 50)) %>%
formatStyle('speed', backgroundColor = 'yellow')
})
}

最新更新