如何将 selectInput 链接到 DataTable 以根据选择更新表?

问题描述 投票:0回答:1

具体来说,我使用 pickerInput (类似于 selectInput)和 renderDataTable。

应用程序如下所示(您可以看到我试图让过滤器更新数据表的位置 - 如果我选择“setosa”,表应更新为仅包含 setosa 行):

enter image description here

这是我的最低限度可重现的代码:

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)
jquery r shiny dt
1个回答
2
投票

试试这个

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)
© www.soinside.com 2019 - 2024. All rights reserved.