16.2 Dynamic UI
Here are options for creating UIs that depend on other UIs, using conditionalPanel
and renderUI
https://shiny.posit.co/r/articles/build/dynamic-ui/.
Another resource: https://mastering-shiny.org/action-dynamic.html
Here is an example that makes a second UI appear if certain selections are made in the first UI. This is also app.R on GitHub.
Code
library(shiny)
ui <- fluidPage(
selectInput("dataset", "Dataset", c("diamonds", "rock", "pressure", "cars")),
conditionalPanel( condition = "output.nrows",
checkboxInput("headonly", "Only use first 1000 rows"))
)
server <- function(input, output, session) {
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
output$nrows <- reactive({
nrow(datasetInput())
})
outputOptions(output, "nrows", suspendWhenHidden = FALSE)
}
shinyApp(ui, server)