具体来说,我使用 pickerInput (类似于 selectInput)和 renderDataTable。
应用程序如下所示(您可以看到我试图让过滤器更新数据表的位置 - 如果我选择“setosa”,表应更新为仅包含 setosa 行):
这是我的最低限度可重现的代码:
library(shiny)
library(data.table)
results <- iris
results$Species <- as.character(results$Species)
# UI
ui <- fluidPage(
# Application title
titlePanel(
h1("Iris Table", align="center")
),
fluidRow(
column(3,
pickerInput("speciesInput", "Species", choices=unique(results$Species), options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)
),
column(9,
DT::dataTableOutput('table')))
)
# Server
server <- function(input, output) {
output$table <- DT::renderDataTable(
DT::datatable(#filter='top',
escape = FALSE,
iris
))
}
# Run the application
shinyApp(ui = ui, server = server)
试试这个
library(shiny)
library(data.table)
results <- iris
results$Species <- as.character(results$Species)
# UI
ui <- fluidPage(
# Application title
titlePanel(
h1("Iris Table", align="center")
),
fluidRow(
column(3,
pickerInput("speciesInput", "Species", choices=unique(results$Species), options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)
),
column(9,
DT::dataTableOutput('table')))
)
# Server
server <- function(input, output) {
mydata <- reactive({
if (is.null(input$speciesInput)) {df <- results
} else df <- results[results$Species %in% input$speciesInput,]
df
})
output$table <- DT::renderDataTable(
datatable(mydata())
)
}
# Run the application
shinyApp(ui = ui, server = server)