闪亮 - 如何禁用 dashboardHeader

shiny - how to disable the dashboardHeader

我是闪亮的新手。当我制作我的项目时,我需要在服务器端隐藏 dashboardHeader。

在 shinydashboard 网站上,我找到了代码 dashboardHeader(disable = TRUE)。我试过了,但没用。

不过,我尝试使用shinyjs来解决问题。

    <code>

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

    ui <- dashboardPage(
          dashboardHeader(
                extendShinyjs(text = 'shinyjs.hidehead = function(params) {           
                $("header").addClass("sidebar-collapse") }'),
                          ),
          dashboardSidebar(),
          dashboardBody(
              actionButton("button","hide_header",width = 4 )
                       )
                       )

    server <- function(input, output) {
         observeEvent(input$button, {
                       js$hidehead()           
                  })}

   shinyApp(ui, server)</code>

我想你已经知道了,它仍然没有用。

对我的案例有什么想法吗?

Shinyjs 是一个很棒的库。您的代码的问题是您需要首先使用 shinyjs::useShinyjs() 初始化 shinyjs 并将其放入 dashboarBody 函数中。此外,对于 hide/show 和 header,您不需要添加实际上用于侧边栏的 class "sidebar-collapse"。您只需要添加 style="display:none" 来隐藏 header,并删除它来显示 header。下面是你的代码修改为hide/show header。使用的JS代码非常简单,直接从js$hidehead()函数接收参数添加。

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

ui <- dashboardPage(
        dashboardHeader(),
        dashboardSidebar(),
        dashboardBody(
          # initialize shinyjs
          shinyjs::useShinyjs(),
          # add custom JS code
          extendShinyjs(text = "shinyjs.hidehead = function(parm){
                                    $('header').css('display', parm);
                                }"),
          actionButton("button","hide header"),
          actionButton("button2","show header")
        )
      )

server <- function(input, output) {
  observeEvent(input$button, {
    js$hidehead('none')           
  })
  observeEvent(input$button2, {
    js$hidehead('')           
  })
}

shinyApp(ui, server)