Shinydashboard:如果侧边栏项目扩展,则切换选项卡

时间:2020-09-12 11:09:36

标签: r shiny shinydashboard

我正在使用一个闪亮的仪表板,该仪表板的侧边栏包含多个菜单项。其中一项是可扩展的。如果单击菜单项,我想切换选项卡。这对于无法展开的菜单项非常适用,但是对于展开式菜单项,我有些麻烦。

一种解决方法是将动作链接放在展开菜单内并观察它。但是,我想做的是,一旦展开折叠菜单,立即切换选项卡。我尝试使用input$sidebarItemExpandshinydashboard::updateTabItem,但是不知怎么回事...如果有人知道出了什么问题,请告诉我

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#   UI ====
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

header <- shinydashboard::dashboardHeader()

sidebar <- shinydashboard::dashboardSidebar(
    shinydashboard::sidebarMenu(
        id="tabs",
        shinydashboard::menuItem(
            text="menu_expand",
            tabName="tab1",
            shiny::selectInput(
                inputId="sel_123",
                label="123",
                choices=c("A","B","C"),
                selected="A")),
        shinydashboard::menuItem(
            text="menu_item_normal1",
            tabName="tab2"),
        shinydashboard::menuItem(
            text="menu_item_normal2",
            tabName="tab3")))

body <- shinydashboard::dashboardBody(
    shinydashboard::tabItems(
        shinydashboard::tabItem(
            tabName = "tab1",
            shiny::h3("Tab 1")
        ),
        shinydashboard::tabItem(
            tabName = "tab2",
            shiny::h3("Tab 2")
        ),
        shinydashboard::tabItem(
            tabName = "tab3",
            shiny::h3("Tab 3"))))

ui <- shinydashboard::dashboardPage(header,sidebar,body)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#   SERVER ====
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

server <- function(input,output,session){
    shiny::observeEvent(input$sidebarItemExpanded, {
        print(input$sidebarItemExpanded)
        if(input$sidebarItemExpanded == "menu_expand"){
            print("expanded")
            shinydashboard::updateTabItems(
                session=session,"tabs","tab1"
            )
        }
    })
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#   LAUNCH APP ====
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

shiny::shinyApp(ui,server)

1 个答案:

答案 0 :(得分:2)

您需要将menutitem tabNametab1放在扩展菜单中的一个级别上,因为其中也可能有多个选项卡和子菜单。试试这个代码。

sidebar <- shinydashboard::dashboardSidebar(
  shinydashboard::sidebarMenu(
    id="tabs",
    shinydashboard::menuItem(text="menu_expand", icon = icon("bar-chart-o"),
                              menuItem("menu_expanded", tabName = "tab1", icon = icon("bar-chart-o")),
      # text="menu_expand",
      # tabName="tab1",
      shiny::selectInput(
        inputId="sel_123",
        label="123",
        choices=c("A","B","C"),
        selected="A")),
    shinydashboard::menuItem(
      text="menu_item_normal1",
      tabName="tab2"),
    shinydashboard::menuItem(
      text="menu_item_normal2",
      tabName="tab3")))