Automatic rightSidebar popup when menuItem is clicked in shinydashboardPlus

二次信任 提交于 2019-12-11 17:49:44

问题


I have a shiny 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:

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<- function(input,output,session){
set.seed(122)
histdata <- rnorm(500)

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

}
shinyApp(ui, server)

回答1:


Looking at the HTML, you can see that the css class 'control-sidebar-open' is added to the right sidebar when it is opened.

You can program this in shiny using the shinyjs package to add this class whenever the plots menu item is selected on your left sidebar. First you need to give the left sidebar an id so that shiny then knows what tab is selected, then add/remove the class with shinyjs whenever the 'plots' tab is selected/deselected.

Working code below.

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)


来源:https://stackoverflow.com/questions/52876446/automatic-rightsidebar-popup-when-menuitem-is-clicked-in-shinydashboardplus

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