R闪亮:使用updateSelectInput更改多参数的值吗?

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

我想创建一个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函数选择的输入取决于数据库。我想根据不同的类型显示数据框,然后...

r shiny
1个回答
0
投票

所指出的,用selectInputupdateSelectInput代替selectizeInputupdateSelectizeInput是可行的。这意味着我必须删除multiple = FALSE并改为添加options = list(maxItems = 1),如here所述。

© www.soinside.com 2019 - 2024. All rights reserved.