根据输入参数切换shinydashboard菜单项

根据输入参数切换shinydashboard菜单项,shiny,shinydashboard,shinyjs,Shiny,Shinydashboard,Shinyjs,我正在构建一个大型ShinydaShashboard应用程序,可以获取两种数据,每月或每隔一段时间。当从下拉列表中选择“每月”时,应显示一些选项卡,当选择“间隔”时,应隐藏一些选项卡(反之亦然) 我尝试将两个类“OnlyMonthly”和“OnlyInterval”分配给相关的menuItem()s,方法是将它们包装在div()标记中,然后使用shinyJS的toggle()命令在选择“Monthly”时显示“.OnlyMonthly”并隐藏“.OnlyInterval,”但是菜单的格式会受到影

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

我尝试将两个类“OnlyMonthly”和“OnlyInterval”分配给相关的
menuItem()
s,方法是将它们包装在
div()
标记中,然后使用
shinyJS
toggle()
命令在选择“Monthly”时显示“.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)
require(闪亮)
要求(仪表板)
需要(shinyjs)

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

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)

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

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)