在ShinydashboardPlus中单击menuItem时,会自动弹出rightSidebar

时间:2018-10-18 14:35:03

标签: r shiny shinydashboard

我有一个我使用ShinydashboardPlus和Shinydashboard制作的闪亮应用程序,当用户单击带有情节的menuItem时,我想自动打开rightSidebar。

两个小时以来,我一直在努力寻找答案,但没有发现任何问题。我不确定这是否可行,但我想我想问一下是否有人对此有任何见识(如果可能)。

示例应用程序:

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 个答案:

答案 0 :(得分:2)

看一下HTML,您可以看到打开时,css类“ control-sidebar-open”已添加到右侧边栏中。

每当在左侧栏中选择了plots菜单项时,就可以使用Shinyjs包在Shiny中进行编程,以添加此类。首先,您需要为左侧栏添加id,以便Shiny然后知道选择了哪个选项卡,然后每当选中/取消选择“绘图”选项卡时,就使用Shinyjs添加/删除该类。

下面的工作代码。

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)