Last active
February 10, 2018 10:08
-
-
Save cecilialee/80fabe4561bcefda2edacb1ca6830905 to your computer and use it in GitHub Desktop.
Insert dynamic number of UI in Shiny. #r #shiny
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using for loop | |
library(shiny) | |
ui = basicPage( | |
numericInput("n", "Number", 1, min = 1), | |
actionButton("add", "Add UI") | |
) | |
server = function(input, output, session) { | |
observeEvent(input$add, { | |
for (var in seq(input$n)) { | |
insertUI( | |
selector = "#add", | |
where = "afterEnd", | |
ui = textInput(paste0("txt", input$add), | |
"Insert some text") | |
) | |
} | |
}) | |
} | |
shinyApp(ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using lapply | |
# Separate insertUI function and lapply function | |
library(shiny) | |
ui = basicPage( | |
numericInput("n", "Number", 1, min = 1), | |
actionButton("add", "Add UI") | |
) | |
server = function(input, output, session) { | |
insert_ui <- function() { | |
insertUI( | |
selector = "#add", | |
where = "afterEnd", | |
ui = textInput(paste0("txt", input$add), | |
"Insert some text") | |
) | |
} | |
observeEvent(input$add, { | |
lapply(seq(input$n), function(x) insert_ui()) | |
}) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment