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)