我想创建一个Shiny应用程序,在其中可以选择的输入取决于使用updateSelectInput
函数的数据库。我想根据不同的类型显示数据帧,并且这样做,我需要在multiple
中更改selectInput
参数的值。
更清楚地说,这是我要做的事的一个示例:
library(shiny) library(shinyWidgets) library(WDI) library(DT) library(dplyr) foo <- data.frame(foo_name = c("A", "A", "B", "B", "C", "C")) data <- cbind(head(mtcars), foo) ui <- navbarPage(position = "static-top", tabPanel(title = "Base 1", fluidRow( dropdownButton( selectInput( inputId = "choice", label = "Type of data", choices = c("Type X", "Type Y"), selected = "Type X", multiple = FALSE), selectInput(inputId = "test", label = "Test", choices = "", multiple = TRUE), circle = TRUE, status = "primary", icon = icon("gear"), width = "300px", tooltip = tooltipOptions(title = "Outils") ), column(width = 12, dataTableOutput("data")) )) ) server <- function(input, output, session) { observe({ if(input$choice == "Type X"){ updateSelectInput(session, inputId = "test", choices = unique(data$foo_name), selected = NULL) } else if(input$choice == "Type Y"){ updateSelectInput(session, inputId = "test", choices = unique(data$foo_name), selected = NULL, multiple = FALSE) } }) output$data <- renderDataTable({ if(input$choice == "Type X"){ data2 <- data %>% filter(foo_name %in% input$test) } else if(input$choice == "Type Y"){ data3 <- data %>% filter(foo_name %in% input$test) } }) } shinyApp(ui = ui, server = server)
正如您所看到的,当您启动应用程序并且数据类型为
Type X
时,一切正常:根据输入内容,数据帧被被动地显示。之所以起作用,是因为在selectInput
函数中,multiple
参数的值为TRUE
。
但是,如果我想在Type Y
之后显示数据,则该应用程序停止运行。这是因为我将multiple
参数设置为FALSE
。
显然,updateSelectInput
函数不接受我们更改multiple
参数的值。有没有办法绕过它?
我想创建一个Shiny应用程序,其中可以使用updateSelectInput函数选择的输入取决于数据库。我想根据不同的类型显示数据框,然后...