我如何 link 我的 selectInput 到我的 DataTable 以根据选择更新 table? (这是 R Shiny)
How can I link my selectInput to my DataTable to update the table based on the selections? (this is R Shiny)
具体来说,我正在使用 pickerInput(类似于 selectInput)和 renderDataTable。
这是该应用程序的外观(您可以看到我试图让过滤器更新数据的位置table - 如果我 select 'setosa' table 应该更新以仅包含 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)
具体来说,我正在使用 pickerInput(类似于 selectInput)和 renderDataTable。
这是该应用程序的外观(您可以看到我试图让过滤器更新数据的位置table - 如果我 select 'setosa' table 应该更新以仅包含 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)