【问题标题】:Toggle shinydashboard menuItems based on input parameters根据输入参数切换闪亮仪表板菜单项
【发布时间】:2018-06-15 16:40:55
【问题描述】:

我正在构建一个大型闪亮仪表板应用程序,它可以获取两种数据,每月间隔。从下拉列表中选择“每月”时应显示某些选项卡,而在选择“间隔”时应隐藏某些选项卡(反之亦然)。

我尝试将两个类“OnlyMonthly”和“OnlyInterval”分配给相关的menuItem()s,方法是将它们包装在div()标签中,然后使用shinyJStoggle()命令显示“.OnlyMonthly “选择”每月“并隐藏”.onlyInterval“,但菜单的格式受到影响,它不起作用。

这是一个基本应用程序的代码:

require(shiny)
require(shinydashboard)
require(shinyjs)

ui <- dashboardPage(
  header = dashboardHeader(title = 'Toggle Menu'),
  sidebar = dashboardSidebar(
    sidebarMenu(
      menuItem('Item 1', tabName = 'item1',
        menuSubItem('Item A', tabName = 'item1A'),
        # just hide Item B
        div(menuSubItem('Item B', tabName = 'item1B'), class = 'OnlyMonthly')
      ),

      # hide all of Item 2, including C and D
      div(class = 'OnlyInterval',
        menuItem('Item 2', tabName = 'item2',
          menuSubItem('Item C', tabName = 'item2C'),
          menuSubItem('Item D', tabName = 'item2D')
        )
      )

    )
  ),
  body = dashboardBody(
    useShinyjs(),
    selectInput(inputId = 'monthly_vs_interval', label = 'Data type',choices = c('Monthly','Interval'))
  )
)

server <- shinyServer(function(input, output, session) {
  observe({
    toggle(selector = ".OnlyMonthly", input$monthly_vs_interval == 'Monthly')
    toggle(selector = ".OnlyInterval", input$monthly_vs_interval == 'Interval')
  })
})

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: shiny shinydashboard shinyjs


    【解决方案1】:

    经过测试,我发现conditionalPanel 可以正确显示/隐藏选项卡,但格式仍然受到影响。似乎sidebarMenu 只允许menuItems 作为孩子,menuItemmenuSubItem 也是如此。您可能可以通过id 隐藏menuItem(请参阅?menuItem),但可能无法在不影响格式的情况下显示/隐藏menuSubItems。

    require(shiny)
    require(shinydashboard)
    
    ui <- dashboardPage(
      header = dashboardHeader(title = 'Toggle Menu'),
      sidebar = dashboardSidebar(
        sidebarMenu(
          menuItem('Item 1', tabName = 'item1',
                   menuSubItem('Item A', tabName = 'item1A'),
                   # just hide Item B
                   conditionalPanel(menuSubItem('Item B', tabName = 'item1B'), 
                                    condition = "input.monthly_vs_interval == 'Monthly'")
          ),
    
          # hide all of Item 2, including C and D
          conditionalPanel(condition = "input.monthly_vs_interval == 'Interval'",
              menuItem('Item 2', tabName = 'item2',
                       menuSubItem('Item C', tabName = 'item2C'),
                       menuSubItem('Item D', tabName = 'item2D')
              )
          )
    
        )
      ),
      body = dashboardBody(
        selectInput(inputId = 'monthly_vs_interval', label = 'Data type',
                    choices = c('Monthly', 'Interval'))
      )
    )
    
    server <- function(...){}
    
    shinyApp(ui = ui, server = server)
    

    编辑:实际上,只有 sidebarMenuid 参数。在menuSubItem 中使用名为id 的参数会导致语法错误,而通过id 显示/隐藏menuItems 会导致意外结果。我猜你总是可以通过在sidebarMenu 之外使用conditionalPanel 以“肮脏”的方式对其进行编码。但是请注意,这种方法有点像WET

    require(shiny)
    require(shinydashboard)
    
    ui <- dashboardPage(
      header = dashboardHeader(title = 'Toggle Menu'),
      sidebar = dashboardSidebar(
        conditionalPanel(
          condition = "input.monthly_vs_interval == 'Monthly'",
          sidebarMenu(menuItem(
            'Item 1', tabName = 'item1',
            menuSubItem('Item A', tabName = 'item1A'),
            menuSubItem('Item B', tabName = 'item1B')
          ))
        ),
        conditionalPanel(
          condition = "input.monthly_vs_interval == 'Interval'",
          sidebarMenu(
            menuItem('Item 1', tabName = 'item1',
                     menuSubItem('Item A', tabName = 'item1A')
            ),
            menuItem('Item 2', tabName = 'item2',
                     menuSubItem('Item C', tabName = 'item2C'),
                     menuSubItem('Item D', tabName = 'item2D')
            )
          )
        )
      ),
      body = dashboardBody(
        selectInput(inputId = 'monthly_vs_interval', label = 'Data type',
                    choices = c('Monthly', 'Interval'))
      )
    )
    
    server <- function(...){}
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-23
      • 2016-12-09
      • 2020-12-15
      • 2019-08-19
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多