updatePickerInput not updating values after changing tabs in R shiny

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)

Hello snt,

Check out renderUi and uiOutput functions it may help you with this issue. Also this link about how to Build a dynamic UI that reacts to user input may be useful too.

Here is a solution with this approach:

library(shiny)
library(shinyWidgets)
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box

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(uiOutput("dynamicPicker"), width = 3)
          )
        )
    )

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

# Define the server code
server <- function(input, output, session) {
  output$dynamicPicker <- renderUI({
    
    if (input$tab == "1") {
      uiOutput <- pickerInput(inputId = "metric", label = h4("Metric Name"), 
                            choices = c("alpha","beta"), width = "100%")
    } else if (input$tab == "2") {
      uiOutput <- pickerInput(inputId = "metric", label = h4("Metric Name"), 
                            choices = c("alpha","orange"), width = "100%")
    }else {
      uiOutput <- pickerInput(inputId = "metric", label = h4("Metric Name"), 
                            choices = c("foo","zoo", "boo"), width = "100%")
    }
    
    return(uiOutput)
  })
  
}

shinyApp(ui = ui, server = server)

Created on 2018-08-08 by the reprex package (v0.2.0).

Hope it helps!

1 Like

The issue was under observeEvent it was input$metric instead of input$tab. Thank you @pdelboca

1 Like