After reading this it feels like it shouldn't be necessary but, here is another solution avoiding removeUI
, which requires less typing:
library(shiny)
ui <- fluidPage(
column(12,
actionButton("add", "Add a selector"),
actionButton("resetButton", "Reset")
),
column(12, uiOutput("selectors"))
)
server <- function(input, output, session) {
nSelectors <- reactiveVal(1L)
output$selectors <- renderUI({
Map(
selectInput,
inputId = paste0("selector", seq_len(nSelectors())),
label = paste("Selector", seq_len(nSelectors())),
choices = list(c("a", "b", "c")),
selected = "a"
)
})
observeEvent(input$add, {
nSelectors(nSelectors() + 1L)
print(paste("nSelectors:", nSelectors()))
# print(names(input))
})
observeEvent(input$resetButton, {
nSelectors(1L)
print(paste("nSelectors:", nSelectors()))
# print(names(input))
})
}
shinyApp(ui, server)