0

I am trying to dynamically add an input to a Shiny page. I want the user to select from a dropdown a choice and then have the option to add a numeric input corresponding to that choice.

Once I run the app I have two fields "Choice" and "Number". I want the user to click the "Add UI" button and be presented with another "Choice 2" and a "Number 2", when they click again they get "Choice 3" and "Number 3" etc.

Currently all I can manage to add is just another dropdown box. When I try to add numericInput(inputId = "numericWhichGoesWithIndicatorChoice", label = "Number", value = 3) to the insertUI part of the code I get errors.

Additionally, I would like to make it dynamic in the sense that the user can add as many inputs as they want and the inputs will be stored. i.e, inputId = "indicatorChoice", inputId = "indicatorChoice2", inputId = "indicatorChoice3" etc.

Code:

        library(shiny)

ui <- fluidPage(
    actionButton("add", "Add UI"),
    wellPanel(
        selectInput(inputId = "indicatorChoice", label = "Choice", choices = c("choice1", "choice2", "choice3")),
        numericInput(inputId = "numericWhichGoesWithIndicatorChoice", label = "Number", value = 3)
    )
)

server <- function(input, output, session) {
    observeEvent(input$add, {
        insertUI(
            selector = "#add",
            where = "afterEnd",
            ui = selectInput(inputId = "indicatorChoice", label = "Choice 2", choices = c("choice1", "choice2", "choice3"))
        )
    })
}

shinyApp(ui, server)
user113156
  • 6,761
  • 5
  • 35
  • 81
  • You are looking for [modules](https://shiny.rstudio.com/articles/modules.html). See [this answer](https://stackoverflow.com/questions/60818727/shiny-dynamically-add-input-fields-and-data-without-getting-re-rendered/60821741) for example – bretauv Jul 06 '20 at 13:33
  • Thanks! will take a look at it now. – user113156 Jul 06 '20 at 13:40

0 Answers0