0

I have a dropdown whose values should update as I change tabs. But as of now when I change my tab I still have to go and select the previous value in the drop down to get a new list of values. How can this be fixed in a way where as soon as the tabs are switched the dropdown values are updated too. Below is my code.

library(shiny)
library(shinyWidgets)
library(shinydashboard)

sidebar <- dashboardSidebar(
  sidebarMenu(id = "tab",
              menuItem("1", tabName = "1"),
              menuItem("2", tabName = "2"),
              menuItem("3", tabName = "3"),
              menuItem("4", tabName = "4")

  )
)
body <-   ## Body content
  dashboardBody(box(width = 12,fluidRow(
    column(
      width = 3,
      pickerInput(
        inputId = "metric",
        label = h4("Metric Name"),
        choices = c(
          "alpha",
          "beta"
        ),

        width = "100%"
      )
    )
  )))

ui <-   dashboardPage(dashboardHeader(title = "Scorecard"),
                      sidebar,
                      body)

# Define the server code
server <- function(input, output,session) {
  observeEvent(input$metric, {
    if (input$tab == "1"){
      choices <- c(
        "alpha",
        "beta"
      )
    }
    else if (input$tab == "2") {
      choices <- c(
        "apple",
        "orange"
      )
    }
    else {
      choices <- c(
        "foo",
        "zoo",
        "boo"
      )
    }
    updatePickerInput(session,
                      inputId = "metric",
                      choices = choices)
  })

}

shinyApp(ui = ui, server = server)
SNT
  • 1,283
  • 3
  • 32
  • 78
  • 1
    The observer looks at `input$metric` - it should instead look to `input$tab` – Ryan Morton Aug 08 '18 at 19:14
  • you could paste it as an answer to mark this resolved. Thank you. – SNT Aug 08 '18 at 19:28
  • Thanks, but Shiny observers are well documented on SO. I'm not that interested in the points, mostly just enjoy helping people (I've received plenty of help over the years myself). Best of luck with your project! – Ryan Morton Aug 08 '18 at 19:37

0 Answers0