【问题标题】:Update dropdown values in R shiny dynamically动态更新 R Shiny 中的下拉值
【发布时间】:2018-07-24 05:15:57
【问题描述】:

我正在尝试动态更新闪亮应用的下拉列表中的值。因此,如果在下拉分类中选择了值,它应该更新产品系列下拉列表中的值。我在下面的代码中缺少什么。它似乎没有选择任何值,并且仅从 else 条件中获取值。

library(shiny)
library(shinyWidgets)

# Define the UI
ui <- bootstrapPage(
  column(3, uiOutput("class_level")),
  column(3,uiOutput("product"))
)


# Define the server code
server <- function(input, output) {

  output$class_level <- renderUI({
    selectInput(
      "selected_class",
      label = h4("Classification Level"),
      choices = list(
        "Brand" = "Brand",
        "Brand1" = "Brand1",
        "Brand2" = "Brand2"
      ),
      selected = "Brand"
    )
  })


  getFlavor <- reactive({

    if (input$selected_class =="Brand") {
      c( "a " = "a",
         "b" = "b",
         "c" = "c"
        )
    }
    else if  (input$selected_class =="Brand1")
    {
      c(
        "1" = "1",
        "2" = "2",
        "3" = "3"
      )
    }
    else   (input$selected_class =="Brand2")
    {
      c(
        "x" = "x",
        "y" = "y",
        "z" = "z"
      )
    }

  })

  output$product <- renderUI({
    pickerInput(
      "selected_product",
      label = h4("Product Family"),
      choices = as.list(getFlavor()),
      selected = as.list(getFlavor()),
      options = list(
        `deselect-all-text` = "None",
        `select-all-text` = "Total",
        `actions-box` = TRUE
      ),
      multiple = F,
      width = "100%"
    )
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您需要在 if 语句中返回内容。你可以使用return来做到这一点

    library(shiny)
    library(shinyWidgets)
    
    # Define the UI
    ui <- bootstrapPage(
      column(3, uiOutput("class_level")),
      column(3,uiOutput("product"))
    )
    
    
    # Define the server code
    server <- function(input, output) {
    
      output$class_level <- renderUI({
        selectInput(
          "selected_class",
          label = h4("Classification Level"),
          choices = list(
            "Brand" = "Brand",
            "Brand1" = "Brand1",
            "Brand2" = "Brand2"
          ),
          selected = "Brand"
        )
      })
    
    
      getFlavor <- reactive({
    
        if (input$selected_class =="Brand") {
          return(c( "a " = "a",
             "b" = "b",
             "c" = "c"
            ))
        }
        else if  (input$selected_class =="Brand1")
        {
          return(c(
            "1" = "1",
            "2" = "2",
            "3" = "3"
          ))
        }
        else   (input$selected_class =="Brand2")
        {
          return(
          c(
            "x" = "x",
            "y" = "y",
            "z" = "z"
          ))
        }
    
      })
    
      output$product <- renderUI({
        pickerInput(
          "selected_product",
          label = h4("Product Family"),
          choices = as.list(getFlavor()),
          selected = as.list(getFlavor()),
          options = list(
            `deselect-all-text` = "None",
            `select-all-text` = "Total",
            `actions-box` = TRUE
          ),
          multiple = F,
          width = "100%"
        )
      })
    }
    
    # Return a Shiny app object
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      【解决方案2】:

      您的方法有些复杂,updatePickerInput 会让您的工作更轻松。这是一种可能的实现方式:

      library(shiny)
      library(shinyWidgets)
      
      ui <- bootstrapPage(
      
        column(
          width = 3, 
          selectInput(
            inputId = "selected_class",
            label = h4("Classification Level"),
            choices = c("Brand", "Brand1", "Brand2"),
            selected = "Brand"
          )
        ),
        column(
          width = 3, 
          pickerInput(
            inputId = "selected_product",
            label = h4("Product Family"),
            choices = c("a", "b", "c"),
            options = list(
              `deselect-all-text` = "None",
              `select-all-text` = "Total",
              `actions-box` = TRUE
            ),
            width = "100%"
          )
        )
      )
      
      server <- function(input, output, session) {
      
        observeEvent(input$selected_class, {
          if(input$selected_class =="Brand") {
            choices <- c("a", "b", "c")
          } else if(input$selected_class =="Brand1") {
            choices <- c("1", "2", "3")
          } else {
            choices <- c("x", "y", "z")
          }
          updatePickerInput(
            session,
            inputId = "selected_product",
            choices = choices
          )
        })
      
      }
      
      shinyApp(ui = ui, server = server)
      

      【讨论】:

        猜你喜欢
        • 2020-08-25
        • 2020-01-06
        • 1970-01-01
        • 2018-09-30
        • 2015-03-25
        • 1970-01-01
        • 1970-01-01
        • 2020-04-23
        • 1970-01-01
        相关资源
        最近更新 更多