【问题标题】:shiny - how to disable the dashboardHeader闪亮 - 如何禁用dashboardHeader
【发布时间】:2017-01-19 15:58:42
【问题描述】:

我是闪亮的新手。当我制作我的项目时,我需要在服务器端隐藏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>

我猜你已经知道了,还是不行。

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

【问题讨论】:

    标签: shiny dashboard


    【解决方案1】:

    Shinyjs 是一个很棒的库。您的代码的问题是您需要首先使用 shinyjs::useShinyjs() 初始化 shinyjs 并将其放入 dashboarBody 函数中。此外,要隐藏/显示标题,您不需要添加实际上用于侧边栏的类"sidebar-collapse"。您只需添加style="display:none" 即可隐藏标题,删除它即可显示标题。以下是修改为隐藏/显示标题的代码。使用的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) 
    

    【讨论】:

    • 不能再感谢了。我搞砸了侧边栏和标题。您的回答有助于我理解 shinyjs 功能。非常感谢您的帮助。再次感谢。
    • 很高兴知道它对您有帮助。如果它对您的问题有很好的回答,您可以接受带有复选标记的问题,这样您就可以帮助其他用户。
    猜你喜欢
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2020-08-08
    • 1970-01-01
    • 2018-07-21
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多