【问题标题】:Automatic rightSidebar popup when menuItem is clicked in shinydashboardPlus在shinydashboardPlus中单击menuItem时自动弹出右侧边栏
【发布时间】:2018-10-18 14:35:02
【问题描述】:

我有一个使用 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)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!你是最棒的!
    猜你喜欢
    • 2020-03-04
    • 2020-12-28
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多