简体   繁体   中英

The icon that hides and seek right sidebar in shinydashboardPlus() is not working

In the shinydashboardPlus() below I activate and deactivate the right sidebar ability based on the tab I use. The issue is that when Im in the 2nd tab I cannot hide the right sidebar by clicking on the icon above it.

## app.R ##
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(
    
  ),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    tags$hr(),
    tabsetPanel(
      id ="tabA",
      type = "tabs",
      tabPanel("Front",icon = icon("accusoft")),
      tabPanel("Data", icon = icon("table")
      )
    )
  ),
  controlbar = dashboardControlbar()
  
)

server <- function(input, output) {
  observe({
    if (input$tabA == "Front") {
      hide(selector = "body > div.wrapper > header > nav > div:nth-child(4) > ul")
      removeClass(selector = "body", class = "control-sidebar-open")
    } else {
      show(selector = "body > div.wrapper > header > nav > div:nth-child(4) > ul")
      addClass(selector = "body", class = "control-sidebar-open")
    }
  })
}

shinyApp(ui = ui, server = server)

By now we can use shinydashboardPlus::updateControlbar :

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

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    useShinyjs(),
    tags$script(HTML("$(document).find('body > div.wrapper > header > nav > div:nth-child(4) > ul').css('display', 'none');")),
    tags$hr(),
    tabsetPanel(
      id ="tabA",
      type = "tabs",
      tabPanel("Front",icon = icon("accusoft")),
      tabPanel("Data", icon = icon("table")
      )
    )
  ),
  controlbar = dashboardControlbar(id = "dashboardControlbarID", collapsed = TRUE)
)

server <- function(input, output) {
  observeEvent(input$tabA, {
    if (input$tabA == "Front") {
      hide(selector = "body > div.wrapper > header > nav > div:nth-child(4) > ul")
    } else {
      show(selector = "body > div.wrapper > header > nav > div:nth-child(4) > ul")
    }
    updateControlbar("dashboardControlbarID")
  }, ignoreInit = TRUE)
}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM