Last active
February 24, 2018 07:26
-
-
Save cecilialee/6c6c1dcf0a039dc0007a90a3f754b9e3 to your computer and use it in GitHub Desktop.
Render dynamic number of UI in Shiny (all codes in server side). #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
library(shiny) | |
ui = basicPage( | |
# inputs | |
textInput("txt", "Text"), | |
numericInput("n", "Number", 1), | |
actionButton("submit", "Submit"), | |
# outputs | |
uiOutput("ui") | |
) | |
server = function(input, output) { | |
observeEvent(input$submit, { | |
n <- input$n | |
# construct single ui outputs | |
lapply(seq(n), function(i) { | |
output[[paste0("ptxt", i)]] <- renderText({ | |
paste0(input$txt, i) | |
}) | |
}) | |
# concat ui outputs | |
output$ui <- renderUI({ | |
lapply(seq(n), function(i) { | |
textOutput(paste0("ptxt", i)) | |
}) | |
}) | |
}) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment