r语言 - 在闪亮的仪表板Plus中单击菜单项时自动弹出右侧边栏



我有一个闪亮的应用程序,我用闪亮的仪表板Plus和闪亮的仪表板制作了它,我希望当用户单击带有我的绘图的菜单项时,右边栏自动打开。

几个小时以来,我一直在试图找到答案,但我什么也没找到。我不确定这是否可能,但我想我会在这里问一下是否有人对如何做到这一点有任何见解(如果可能的话(。

示例应用:

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)

查看 HTML,您可以看到 css 类 'control-sidebar-open' 在打开时被添加到右侧边栏中。

您可以使用 shinyjs 包在 shiny 中对此进行编程,以便在左侧边栏上选择绘图菜单项时添加此类。首先,您需要给左侧边栏一个id,以便 shiny 然后知道选择了哪个选项卡,然后在选择/取消选择"plots"选项卡时使用 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)

最新更新