Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@christopherlovell
Last active September 7, 2023 11:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save christopherlovell/b7ecdf8b0aa82c20fa46 to your computer and use it in GitHub Desktop.
Save christopherlovell/b7ecdf8b0aa82c20fa46 to your computer and use it in GitHub Desktop.
Shiny dynamic UI elements
library(shiny)
server <- function(input, output) {
output$input_ui <- renderUI({
num <- as.integer(input$num)
lapply(1:num, function(i) {
numericInput(paste0("n_input_", i), label = paste0("n_input", i), value = 0)
})
})
output$table <- renderTable({
num <- as.integer(input$num)
data.frame(lapply(1:num, function(i) {
input[[paste0("n_input_", i)]]
}))
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("num", "select number of inputs", choices = seq(1,10,1))
),
mainPanel(
uiOutput("input_ui"),
tableOutput("table")
)
)
)
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment