【发布时间】:2021-10-21 04:45:54
【问题描述】:
我有一个闪亮的应用程序,它使用shinydashboard 侧边栏作为导航。每个命名选项卡都有自己的tabSetPanel。因为每个tabSetPanel 可以有不同数量的选项卡(随着时间的推移,我希望可以添加更多选项卡并且整个应用程序仍然可以运行),我想以编程方式获取选项卡的名称.
有没有办法获得body 对象的选项卡元素的列表输出?例如
tabs
tabs$plots
"iris"
tabs$plots
"mtcars"
代表:
library(shiny)
library(shinydashboard)
library(magrittr)
library(ggplot2)
body <- dashboardBody(
tabItem(
"plots",
tabsetPanel(
tabPanel(
"iris",
tags$h1("iris"),
sidebarLayout(
sidebarPanel(
selectInput("x_var", "Select X variable: ", choices = names(iris)),
selectInput("y_var", "Select Y variable: ", choices = names(iris))
),
mainPanel(
plotOutput("iris_plot"),
textOutput("ui_elements")
)
)
),
tabPanel(
"mtcars",
tags$h1("mtcars"),
sidebarLayout(
sidebarPanel(
selectInput("x_var_cars", "Select X variable: ", choices = names(mtcars)),
selectInput("y_var_cars", "Select Y variable: ", choices = names(mtcars))
),
mainPanel(
plotOutput("mtcars_plot")
)
)
)
)
)
)
ui <- dashboardPage(
dashboardHeader(title = "Test App"),
dashboardSidebar(
sidebarMenu(
id = "sidebar_menu",
menuItem(
"plots", tabName = "plots"
)
)
),
body
)
server <- function(input, output) {
output$iris_plot <- renderPlot({
iris %>%
ggplot() +
aes(x = .data[[input$x_var]], y = .data[[input$y_var]], colour = Species) +
geom_point()
})
output$mtcars_plot <- renderPlot({
mtcars %>%
ggplot() +
aes(x = .data[[input$x_var_cars]], y = .data[[input$y_var_cars]]) +
geom_point()
})
}
shinyApp(ui, server)
【问题讨论】:
标签: r shiny shinydashboard