【问题标题】:Display Second Menu when a Menu Item is selected in Shiny Dashboard在 Shiny Dashboard 中选择菜单项时显示第二个菜单
【发布时间】:2019-08-24 20:30:01
【问题描述】:

我有一个闪亮的仪表板,在侧边栏中我有两个菜单。第一个有两个项目,而第二个有五个。

当仪表板加载第一个菜单中的第一项“主页”时,默认选择。选择了第二项时,显示了第二个菜单。

我已成功显示第二个菜单,但第一个菜单返回到“主页”菜单项。您可以单击第二个菜单中的任何项目,只需更好,但第一个菜单始终在选择第二项时返回到家庭项目。我希望它在选择第二个项目后停留在第二个项目上,并且基本上使用两个菜单,就好像它们是一个菜单一样。

我不希望第二个菜单成为子菜单,因为它不是。

下面是一些显示问题的虚拟代码。

library(shiny)
library(shinydashboard)

ui <- dashboardPage(skin = "blue", title = "",
                    dashboardHeader(),
                    dashboardSidebar(
                      sidebarMenu(id="fMenu", sidebarMenuOutput("firstMenu")),
                      sidebarMenu(id = "eMenu", sidebarMenuOutput("extendedMenu"))
                      ),
                    dashboardBody()
)

server <- (function(input, output, session) {



  output$firstMenu <- renderMenu({
    menu_list <- list(
      menuItem("Home", tabName = "home", icon = icon("home")),
      menuItem("2nd Item", tabName = "second", icon = icon("sort"))
    )

    sidebarMenu(.list = menu_list)
  })

  isolate({updateTabItems(session, "featureMenu", "home")})

  observeEvent(input$fMenu, {
    if(input$fMenu == "second"){
      output$extendedMenu <- renderMenu({
        menu_list <- list(
          menuItem("3rd Item", tabName = "third", icon = icon("sort")),
          menuItem("4th Item", tabName = "fourth", icon = icon("sort")),
          menuItem("5th Item", tabName = "fifth", icon = icon("sort"))
        )

        sidebarMenu(.list = menu_list)
      })
    }
  })

})

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny menu shinydashboard


    【解决方案1】:

    菜单的重新呈现会导致它重新选择第一个选项卡。您可以在menuItem 中使用参数selected 来保持选中第二个选项卡。但是,在这种情况下,您应该只有一个 sidebarMenu 和一个 sidebarMenuOutput

    工作示例

    library(shiny)
    library(shinydashboard)
    
    ui <- dashboardPage(skin = "blue", title = "",
                        dashboardHeader(),
                        dashboardSidebar(
                          sidebarMenu(id="fMenu", 
                                      sidebarMenuOutput("firstMenu"))
                        ),
                        dashboardBody()
    )
    
    server <- (function(input, output, session) {
      output$firstMenu <- renderMenu({
        menu_list <- list(
          menuItem("Home", tabName = "home", icon = icon("home")),
          menuItem("2nd Item", tabName = "second", icon = icon("sort"))
        )
        sidebarMenu(.list = menu_list)
      })
    
      observeEvent(input$fMenu, {
        if(input$fMenu == "second"){
          output$firstMenu <- renderMenu({
            menu_list <- list(
              menuItem("Home", tabName = "home", icon = icon("home")),
              menuItem("2nd Item", tabName = "second", icon = icon("sort"), selected=T),
              menuItem("3rd Item", tabName = "third", icon = icon("sort")),
              menuItem("4th Item", tabName = "fourth", icon = icon("sort")),
              menuItem("5th Item", tabName = "fifth", icon = icon("sort"))
            )
    
            sidebarMenu(.list = menu_list)
          })
    
        }
      })
    })
    
    shinyApp(ui, server)
    

    【讨论】:

    • @Nate 请记住,现在每次单击第二个选项卡时,都会重新呈现菜单。如果您只希望这种情况发生一次,您可能应该构建另一个 if 语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2014-04-07
    相关资源
    最近更新 更多