I have a shiny app, which uses a shinydashboard sidebar as navigation. Each named tab is its own tabSetPanel. Because each tabSetPanel can have a different number of tabs (and over time, I'm hoping it to be possible for more to be added and the whole app still function), I'd like to get the names of the tabs programmatically.
Is there a way I can get a list output for the tab elements of the body object? e.g.
tabs
tabs$plots
"iris"
tabs$plots
"mtcars"
reprex:
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)