Shiny 隐藏R应用程序中的主标题切换

Shiny 隐藏R应用程序中的主标题切换,shiny,shinydashboard,shinyjs,Shiny,Shinydashboard,Shinyjs,我正在寻找一种方法来隐藏主标题切换图标,就像我们使用ShinyJS包在R Shinny应用程序中为侧栏所做的那样。附加图像以供参考 密码 您可以更新dashboardHeader函数并删除创建按钮的项。请注意,我刚刚注释了它并重命名了该函数 #rm(list = ls()) library(shiny) library(shinydashboard) library(shinyjs) mydashboardHeader <- function(..., title = NULL, disa

我正在寻找一种方法来隐藏主标题切换图标,就像我们使用ShinyJS包在R Shinny应用程序中为侧栏所做的那样。附加图像以供参考

密码 您可以更新dashboardHeader函数并删除创建按钮的项。请注意,我刚刚注释了它并重命名了该函数

#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)
您可以更新dashboardHeader函数并删除创建按钮的项。请注意,我刚刚注释了它并重命名了该函数

#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)

我也遇到了同样的问题。以下是解决方案…请告诉我它是否适用于您:-

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)

我也遇到了同样的问题。以下是解决方案…请告诉我它是否适用于您:-

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)

在应用程序启动时隐藏侧边栏切换的一种方法是使用htmlwidgets中的JS函数:

我发现以下线程有助于解决这一问题:


在应用程序启动时隐藏侧边栏切换的一种方法是使用htmlwidgets中的JS函数:

我发现以下线程有助于解决这一问题:


感谢您提出此方法。是否有一种方法可以动态执行相同的操作,例如在按钮单击时-切换图标出现和消失。感谢您提出此方法。是否有一种方法可以动态执行相同的操作,例如在按钮单击时-切换图标出现和消失。@string非常欢迎您,请接受答案如果你觉得有用的话@非常欢迎您,如果您觉得答案有用,请接受它。。!!非常干净的方法。谢谢。非常干净的方法。非常感谢。