我正在构建一个大型的shinydashboard应用程序,它可以采用两种数据,每月或间隔。当"每月"时,应显示一些标签。从下拉列表中选择并在" Interval"被选中(反之亦然)。
我尝试分配两个课程," OnlyMonthly"和" OnlyInterval,"通过将menuItem()
标记包装在相关的div()
中,然后使用shinyJS
的{{1}}命令来显示"。仅限每月"什么时候"每月"被选中并隐藏" .OnlyInterval,"但是菜单的格式会受到影响而且无法正常工作。
这是基本应用的代码:
toggle()
答案 0 :(得分:2)
经过测试,我发现conditionalPanel
正确显示/隐藏了标签,但格式仍然受到影响。似乎sidebarMenu
仅允许menuItem
作为孩子,menuItem
和menuSubItem
也是如此。您可以通过menuItem
隐藏id
(请参阅?menuItem
),但可能无法在不影响格式的情况下显示/隐藏menuSubItem
。
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)
修改:实际上,只有sidebarMenu
有一个id
参数。在id
中使用名为menuSubItem
的参数会导致语法错误,并通过id
对menuItem
使用show / hide会导致意外结果。我想你总是可以在conditionalPanel
之外使用sidebarMenu
来编码“脏”方式。但请注意,此方法有点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)