Hide main header toggle in R Shiny App

吃可爱长大的小学妹 提交于 2019-12-08 06:36:15

问题


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:


One way to hide the sidebar toggle when the app starts is using the JS() function from htmlwidgets:

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

ui <- dashboardPage(
  dashboardHeader(title = "Title"),

  dashboardSidebar(
    # Remove the sidebar toggle element
    tags$script(JS("document.getElementsByClassName('sidebar-toggle')[0].style.visibility = 'hidden';"))
  ),

  dashboardBody()
)

server <- function(input, output, session) {
}

shinyApp(ui = ui, server = server)

I found the following threads useful to figure this out:

To enable and disable sidebar toggle button using a action button

Call javascript function on page load in R shiny




回答2:


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)




回答3:


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)


来源:https://stackoverflow.com/questions/41939346/hide-main-header-toggle-in-r-shiny-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!