Do it by shinyjs
. Watch how I do by addClass
removeClass
, Check it out.
Left right bar closed when you are on “Front” and open on "Data" tab.
## app.R ##
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
library(shinyWidgets)
library(shinyjs)
ui <- dashboardPagePlus(
dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears",
fixed = T
),
dashboardSidebar(
),
dashboardBody(
useShinyjs(),
tags$hr(),
tabsetPanel(
id ="tabA",
type = "tabs",
tabPanel("Front",icon = icon("accusoft")),
tabPanel("Data", icon = icon("table")
)
)
),
rightsidebar = rightSidebar(
)
)
server <- function(input, output) {
observe({
if (input$tabA == "Front") {
addClass(selector = "body", class = "sidebar-collapse")
removeClass(selector = "body", class = "control-sidebar-open")
} else {
removeClass(selector = "body", class = "sidebar-collapse")
addClass(selector = "body", class = "control-sidebar-open")
}
})
}
shinyApp(ui = ui, server = server)
To answer your additional question in comment:
- I inspected the document, there is no ID for the right sidebar menu, so I can't use shiny functions to change that one, unless you change the source code of
shinydashboardPlus
to give it an ID at initiation, but it will be very tricky.
- I add some javascript
runjs
to sneakily hide the right side menu and add a button on "Front". When you click it, the right bar will show up.
## app.R ##
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
library(shinyWidgets)
library(shinyjs)
ui <- dashboardPagePlus(
dashboardHeaderPlus(
enable_rightsidebar = TRUE,
fixed = T
),
dashboardSidebar(
),
dashboardBody(
useShinyjs(),
tags$hr(),
tabsetPanel(
id ="tabA",
type = "tabs",
tabPanel(title = "Front", icon = icon("accusoft"),
actionButton(inputId = "openright", label = "Open Right")
),
tabPanel("Data", icon = icon("table")
)
)
),
rightsidebar = rightSidebar(
)
)
server <- function(input, output) {
observe({
runjs('document.querySelectorAll(".navbar-custom-menu")[1].style.visibility = "hidden"')
if (input$tabA == "Front") {
addClass(selector = "body", class = "sidebar-collapse")
removeClass(selector = "body", class = "control-sidebar-open")
} else {
removeClass(selector = "body", class = "sidebar-collapse")
addClass(selector = "body", class = "control-sidebar-open")
}
})
observeEvent(input$openright, {addClass(selector = "body", class = "control-sidebar-open")})
}
shinyApp(ui = ui, server = server)