当我的应用程序用户单击ShinyDashboard侧边栏中的特定menuItem时,我想用模式向他们致意。这是我先前尝试的简单再现:
# libraries
library(shiny)
library(shinydashboard)
## UI ##
ui <- dashboardPage(
skin = "black",
dashboardHeader(),
dashboardSidebar(
sidebarMenu(id = "sidebarmenu",
menuItem("Dashboard", tabName = "dashboard"),
menuItem("Subitems", tabName = "subitems",
menuSubItem("Upload", "upload"),
menuSubItem("Browse", "browse")
),
menuItem("Widgets", tabName = "widgets")
)
),
dashboardBody(
uiOutput('tab')
)
)
## server ##
server <- function(input, output) {
output$tab <- renderUI({
paste("The selected tab is", input$sidebarmenu)
})
observeEvent(input$sidebarmenu == "widgets", {
showModal(
modalDialog(title = "You selected Widgets", "Or did you?")
)
})
}
shinyApp(ui, server)
目标是仅在选择menuItem widgets
时打开模态。尽管条件input$sidebarmenu == "widgets"
,这不会发生。而是,只要用户切换菜单项,就会显示模式。为什么会这样,我该如何正确执行呢?
预先感谢您的任何投入。
答案 0 :(得分:2)
将此添加到observeEvent
observeEvent(input$sidebarmenu, {
req(input$sidebarmenu == "widgets")
showModal(
modalDialog(title = "You selected Widgets", "Or did you?")
)
})