单击按钮时在 Shinydashboard 的 SidebarMenu 中折叠(关闭)菜单

Collapse (close) Menu in SidebarMenu of Shinydashboard on button click

我正在使用与以下应用程序类似的应用程序。我的输入面板位于最初展开的菜单下的侧边栏中。我想折叠菜单并隐藏所有输入面板,以便我的边栏干净。但它应该在我展开时​​出现(而不是永久隐藏)。我尝试了以下解决方案,但它不起作用。请帮助我找到解决方案或任何替代方法。

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(
  header = dashboardHeader(),
  sidebar = dashboardSidebar(
    sidebarMenu(
      menuItem(
        startExpanded = TRUE,
        "Menu 1",
        column(
          width = 12,
          actionButton("hideMe", label = "Collapse Me", icon = icon("close"))
        )
      )
    )
  ),
  body = dashboardBody()
)
server <- function(input, output, server){
  observeEvent(input$hideMe, {
    shinyjs::hide(selector = "ul.menu-open");
  })
}

runApp(shinyApp(ui, server))

您需要将 useShinyjs() 添加到 ui 部分

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(
        header = dashboardHeader(),
        sidebar = dashboardSidebar(
                useShinyjs(),
                sidebarMenu(
                        menuItem(
                                startExpanded = TRUE,
                                "Menu 1",
                                column(
                                        width = 12,
                                        actionButton("hideMe", label = "Collapse Me", icon = icon("close"))
                                )
                        )
                )
        ),
        body = dashboardBody()
)
server <- function(input, output, server){
        observeEvent(input$hideMe, {
                shinyjs::hide(selector = "ul.menu-open");
        })
}

runApp(shinyApp(ui, server))