如何在 Shiny 应用程序的侧边栏中的 menuSubItem 下包含一个控件小部件?
这是我的试用:
library(shiny)
library(shinyWidgets)
library(shinydashboard)
ui <- dashboardPage(
### Header -----
dashboardHeader(title = "Example App"),
### Sidebar -----
dashboardSidebar(
sidebarMenu(
menuItem(
text = "A",
tabName = "analytics",
icon = icon("signal"),
startExpanded = TRUE,
menuSubItem(text = "a",
icon = NULL,
pickerInput(
inputId = "Id086",
label = "Placeholder",
choices = c("a", "b", "c", "d"),
options = list(
title = "This is a placeholder")
)
),
menuSubItem(text = "b",
icon = NULL)
)
)
),
dashboardBody(
)
)
## Server-function -----
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
但是,所需的输入不会出现在应用程序中。我设法将输入小部件包含在任何菜单项下,但未能使其显示在 menuSubItems 下。
最佳答案
menuSubItem
旨在导航正文中的 tabItem
- 这就是它们不能拥有子元素的原因。
您可以使用嵌套的 menuItem
在侧边栏中放置其他输入(请参阅下面的示例),"behave" differently当有 child /没有 child 时。
当menuItem
是childfull时,它接受参数expandedName
和startExpanded
。
当 menuItem
没有子项时,它接受参数 tabName
和 selected
。
menuSubItem
始终没有子项。
library(shiny)
library(shinyWidgets)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Example App"),
dashboardSidebar(sidebarMenu(
menuItem(
text = "A",
# tabName = "analytics", # childfull menuItems ignore the tabName parameter they use expandedName instead
icon = icon("signal"),
startExpanded = TRUE,
menuItem(
text = "a",
menuSubItem(
text = "a1",
tabName = "tab_a1",
icon = NULL
),
pickerInput(
inputId = "Id086",
label = "Placeholder",
choices = c("a", "b", "c", "d"),
options = list(title = "This is a placeholder")
),
icon = NULL,
startExpanded = TRUE
),
menuSubItem(
text = "b",
tabName = "tab_b",
icon = NULL
)
)
)),
dashboardBody(tabItems(
tabItem(tabName = "tab_a1",
h2("tab_a1 content")),
tabItem(tabName = "tab_b",
h2("tab_b content"))
))
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)
Here您可以找到相关问题。
但是,我个人建议使用shinydashboardPlus的right sidebar用于输入,仅使用左侧边栏进行导航。
关于r - 如何在 Shiny 的仪表板侧边栏中的 menuSubItem 下包含输入小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74313416/