我制作了一个闪亮的应用程序(在闪亮的仪表板中),其中包含许多菜单和子菜单项。就像下面的树结构
sidebarMenu(
menuItem("Item 1", tabName = "item1", icon = icon("dashboard")),
menuItem("Item 2", tabName = "item2", icon = icon("th"),
menuSubItem("Subitem1", tabName = "subitem1"),
menuSubItem("Subitem2", tabName = "subitem2")),
menuItem("Item 3", tabName = "item3", icon = icon("bolt"),
menuSubItem("Subitem3", tabName = "subitem3"),
menuSubItem("Subitem4", tabName = "subitem4")
)
)
在第一页我有四个操作按钮。我希望当单击任何操作按钮时,可以打开相应的“subitem#”页面。我无法做到这一点shinydashboard::updateTabItems
。请帮忙。示例代码/应用程序是
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic Tab Example"),
dashboardSidebar(
sidebarMenu(
menuItem("Item 1", tabName = "item1", icon = icon("dashboard")),
menuItem("Item 2", tabName = "item2", icon = icon("th"),
menuSubItem("Subitem1", tabName = "subitem1"),
menuSubItem("Subitem2", tabName = "subitem2")),
menuItem("Item 3", tabName = "item3", icon = icon("bolt"),
menuSubItem("Subitem3", tabName = "subitem3"),
menuSubItem("Subitem4", tabName = "subitem4")
)
)
),
dashboardBody(
tabItems(
tabItem(
"item1",
actionButton("button1", "navigate to subitem1"),
actionButton("button2", "navigate to subitem2"),
actionButton("button3", "navigate to subitem3"),
actionButton("button4", "navigate to subitem4")
),
tabItem("subitem1",
box(
title = "Item 1",
actionButton("button1", "Button 1")
)
),
tabItem("subitem2",
box(
title = "Item 2 Content",
"This is the content for Item 2."
)
),
tabItem("subitem3",
box(
title = "Item 3 Content",
"This is the content for Item 3."
)
),
tabItem("subitem4",
box(
title = "Item 4 Content",
"This is the content for Item 4."
)
)
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
这里有一个建议:删除 subitem1 下的 actionButton“button1”,以避免两个具有相同 id 的按钮/将 id 添加到菜单中,此处为“选项卡”/使用 updateTabItems 来检测单击按钮的 id