Automatic rightSidebar popup when menuItem is clicked in shinydashboardPlus

I have an app that I made with shinydashboardPlus and shinydashboard that I would like to have the rightSidebar automatically open when the menuItem with my plots are clicked by the user.

I have been trying to find an answer to this for a couple hours now and I haven't found anything. I am not sure if this is possible but I figured I'd ask on here to see if anyone has any insight on how to do this (If possible).

Sample App:
UI

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

header<-dashboardHeaderPlus(enable_rightsidebar = TRUE,
rightSidebarIcon = "bars")

sidebar<- dashboardSidebar(
sidebarMenu(
menuItem("Data",
tabName = "data"),
menuItem("Plots",
tabName = "plots",
icon = icon("bar-chart-o"))))

body<-dashboardBody(
tabItems(
tabItem(tabName = "data","DATA"),
tabItem(tabName = "plots",
box(plotOutput("plot1")))))

rightsidebar<-rightSidebar(
background = "dark",
rightSidebarTabContent(
id=1,
title = "Customize Plots",
icon = "desktop",
active = T,
sliderInput("slider", "Number of observations:", 1, 100, 50)))

ui<- dashboardPagePlus(header = header,
sidebar = sidebar,
body = body,
rightsidebar = rightsidebar,

)

Server:

server<- function(input,output,session){
set.seed(122)
histdata <- rnorm(500)

output$plot1<- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})

}
shinyApp(ui, server)

Just answered on SO

Solution:

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

header<-dashboardHeaderPlus(enable_rightsidebar = TRUE,
                            rightSidebarIcon = "bars")

sidebar<- dashboardSidebar(
  sidebarMenu(id = "left_sidebar",
    menuItem("Data",
             tabName = "data"),
    menuItem("Plots",
             tabName = "plots",
             icon = icon("bar-chart-o"))))

body<-dashboardBody(
  tabItems(
    tabItem(tabName = "data","DATA"),
    tabItem(tabName = "plots",
            box(plotOutput("plot1")))))

rightsidebar<-rightSidebar(
  background = "dark",
  rightSidebarTabContent(
    id=1,
    title = "Customize Plots",
    icon = "desktop",
    active = T,
    sliderInput("slider", "Number of observations:", 1, 100, 50)))

ui<- dashboardPagePlus(
  shinyjs::useShinyjs(),
  header = header,
  sidebar = sidebar,
  body = body,
  rightsidebar = rightsidebar,
                       
)

server <- function(input,output,session){
  set.seed(122)
  histdata <- rnorm(500)
  
  observe({
    if (input$left_sidebar == "plots") {
      shinyjs::addClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
    } else {
      shinyjs::removeClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
    }
  })
  
  output$plot1<- renderPlot({
    data <- histdata[seq_len(input$slider)]
    hist(data)
  })
  
}
shinyApp(ui, server)

3 Likes