我有一个下拉菜单,其值应在我更改标签时更新。但是到目前为止,当我更改选项卡时,我仍然必须去下拉菜单中选择先前的值以获得新的值列表。如何解决这些问题,即一旦切换选项卡,下拉值也将被更新。下面是我的代码。
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)