Hide main header toggle in R Shiny App

2019-06-14 14:45发布

问题:

I am looking for a way to hide main header toggle icon as we do for side bar in R Shiny App using ShinyJS Package. Attaching the Image for the reference.

Code

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

ui <- shinyUI(dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
useShinyjs()
)
))

server <- shinyServer(function(input, output, session) {
addClass(selector = "body", class = "sidebar-collapse") # Hide Side Bar
})

shinyApp(ui = ui, server = server)

回答1:

you can update the dashboardHeader function and remove the item which creates the button. Note that I just commented it out and renamed the function.

#rm(list = ls())
library(shiny)
library(shinydashboard)
library(shinyjs)
mydashboardHeader <- function(..., title = NULL, disable = FALSE,title.navbar=NULL, .list = NULL) {
  items <- c(list(...), .list)
  #lapply(items, tagAssert, type = "li", class = "dropdown")
  tags$header(class = "main-header",
              style = if (disable) "display: none;",
              span(class = "logo", title),
              tags$nav(class = "navbar navbar-static-top", role = "navigation",
                       # Embed hidden icon so that we get the font-awesome dependency
                       span(shiny::icon("bars"), style = "display:none;"),
                       # Sidebar toggle button
#                        a(href="#", class="sidebar-toggle", `data-toggle`="offcanvas",
#                          role="button",
#                          span(class="sr-only", "Toggle navigation")
#                        ),

                       title.navbar,
                       div(class = "navbar-custom-menu",
                           tags$ul(class = "nav navbar-nav",
                                   items
                           )
                       )
              )
  )
}

ui <- shinyUI(dashboardPage(
  mydashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs()
  )
))

server <- shinyServer(function(input, output, session) {})
shinyApp(ui = ui, server = server)



回答2:

I was stuck with same problem..below is the solution...Let me know if it works for you:-

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

ui <- shinyUI(dashboardPage(
  dashboardHeader(),
  dashboardSidebar( tags$head(
    tags$script(
      HTML(#code for hiding sidebar tabs 
        "Shiny.addCustomMessageHandler('manipulateMenuItem1', function(message)
        {
        var aNodeList = document.getElementsByTagName('a');

        for (var i = 0; i < aNodeList.length; i++) 
        {
        if(aNodeList[i].getAttribute('data-toggle') == message.toggle && aNodeList[i].getAttribute('role') == message.role) 
        {
        if(message.action == 'hide')
        {
        aNodeList[i].setAttribute('style', 'display: none;');
        } 
        else 
        {
        aNodeList[i].setAttribute('style', 'display: block;');
        };
        };
        }
        });"
                                                      )
    )
    )
    ),
  dashboardBody(
    useShinyjs(),
    actionButton("h1","Hide toggle"),
    actionButton("h2","Show toggle")
  )
))

server <- shinyServer(function(input, output, session) {
  observeEvent(input$h1,{
     session$sendCustomMessage(type = "manipulateMenuItem1", message = list(action = "hide",toggle = "offcanvas", role = "button"))
    })
  observeEvent(input$h2,{
    session$sendCustomMessage(type = "manipulateMenuItem1", message = list(action = "show",toggle = "offcanvas", role = "button"))
  })
})

shinyApp(ui = ui, server = server)