【发布时间】:2018-06-15 16:40:55
【问题描述】:
我正在构建一个大型闪亮仪表板应用程序,它可以获取两种数据,每月或间隔。从下拉列表中选择“每月”时应显示某些选项卡,而在选择“间隔”时应隐藏某些选项卡(反之亦然)。
我尝试将两个类“OnlyMonthly”和“OnlyInterval”分配给相关的menuItem()s,方法是将它们包装在div()标签中,然后使用shinyJS的toggle()命令显示“.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