首页 > 解决方案 > R Shiny:使用 updateSelectInput 更改多个参数的值?

问题描述

我想创建一个闪亮的应用程序,其中可以选择的输入取决于使用该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参数的值。有没有办法绕过它?

标签: rshiny

解决方案


正如指出的那样,用selectInputandupdateSelectInput替换selectizeInputandupdateSelectizeInput可以工作。这意味着我必须删除multiple = FALSEoptions = list(maxItems = 1)改为添加,如此所述。

这是固定代码:

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),
                              selectizeInput(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"){
      updateSelectizeInput(session,
                           inputId = "test",
                           choices = unique(data$foo_name),
                           selected = NULL)
    }
    else if(input$choice == "Type Y"){
      updateSelectizeInput(session,
                           inputId = "test",
                           choices = unique(data$foo_name),
                           selected = NULL,
                           options = list(maxItems = 1))
    }
  })

  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)

推荐阅读