disabling/enabling 服务器端的侧边栏

disabling/enabling sidebar from server side

有没有办法从服务器端手动 disabling/enabling shiny dashboard 应用程序的侧边栏?

我想在需要更多 space 时自动隐藏边栏,而不使用 header 上的切换按钮。

谢谢

我对仪表板不是很熟悉,因为我从未构建过仪表板,但快速浏览一下,似乎单击 open/hide 侧边栏按钮时,所发生的一切都是 sidebar-collapse class 获取 added/removed 到 <body> 标签。也许还有更多我不知道的事情发生了,但这似乎是最明显的事情。

因此您可以轻松使用 shinyjs 包(免责声明:我是作者)到 add/remove 即 class

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

shinyApp(
  ui = 
    dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(
        shinyjs::useShinyjs(),
        actionButton("showSidebar", "Show sidebar"),
        actionButton("hideSidebar", "Hide sidebar")
      )
    ),
  server = function(input, output, session) {
    observeEvent(input$showSidebar, {
      shinyjs::removeClass(selector = "body", class = "sidebar-collapse")
    })
    observeEvent(input$hideSidebar, {
      shinyjs::addClass(selector = "body", class = "sidebar-collapse")
    })
  }
)