动态更新R中的下拉列表值

动态更新R中的下拉列表值,r,shiny,R,Shiny,我正在尝试动态更新闪亮应用程序下拉列表中的值。因此,如果在下拉分类中选择了“值”,则应更新“产品系列”下拉列表中的值。我在下面的代码中遗漏了什么。它似乎不调用任何值,只从else条件中获取值 library(shiny) library(shinyWidgets) # Define the UI ui <- bootstrapPage( column(3, uiOutput("class_level")), column(3,uiOutput("product")) ) #

我正在尝试动态更新闪亮应用程序下拉列表中的值。因此,如果在下拉分类中选择了“值”,则应更新“产品系列”下拉列表中的值。我在下面的代码中遗漏了什么。它似乎不调用任何值,只从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)
库(闪亮)
图书馆(shinyWidgets)
#定义用户界面

ui您需要返回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)
库(闪亮)
图书馆(shinyWidgets)
#定义用户界面

ui您需要返回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)
库(闪亮)
图书馆(shinyWidgets)
#定义用户界面

ui您的方法有点复杂,
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)
库(闪亮)
图书馆(shinyWidgets)

ui您的方法有点复杂,
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)
库(闪亮)
图书馆(shinyWidgets)
用户界面