Skip to content

Instantly share code, notes, and snippets.

@skohari
Created October 4, 2018 21:53
Show Gist options
  • Save skohari/bfc9c6f11ea6c2d497af1b0aea5d064e to your computer and use it in GitHub Desktop.
Save skohari/bfc9c6f11ea6c2d497af1b0aea5d064e to your computer and use it in GitHub Desktop.
renderUI as a alternative
library (shiny)
server <- function(input, output) {
rv <- reactiveValues(numFields = 1)
#
# start with one input box
#
output$fieldsUI <- renderUI(textInput("textInput1", "Input #1", 67))
#
# each time the button is clicked, increase the reactive value and add a new text input
fields_fn <- function(i){
latest_n = paste0('sliderInput', i)
tagList(
textInput(paste0("textInput", i), paste0("Input #", i), i + 10),
sliderInput(paste0("sliderInput", i), paste0("Slide #", i), 1, 10, i),
input[[latest_n]] * 2
)
}
observeEvent(input$addField,{
rv$numFields <- rv$numFields + 1
latest_n <- paste0('fieldsUI', '')
output[[latest_n]] <- renderUI({
# output$fieldsUI <- renderUI({
lapply(1:rv$numFields,
fields_fn
)
})
})
}
ui <- fluidPage(sidebarLayout(
sidebarPanel(
actionButton("addField", "Add text input box")
),
mainPanel(
uiOutput("fieldsUI")
)
))
shinyApp(ui, server)
# eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment