Using InsertUI to create data filters

I've been doing some work based off this thread

Why in the app below when you add a second or subsequent filter do any previously selected variable values get reset to null, and how I prevent that happening?

Edit: Impressively, I think I've managed to link to a post that answers my own question! Seem to have taken functioning code a while back, broken it, and then asked how to make it work again. Defining a new observeEvent, and using updateSelectInput rather than the coditionalPanel and renderUI to give the value choices does the trick. I still don't understand why the approach below doesn't work...

Is it better generally to use observeEvent with updateSelectInput, than renderUI to do this kind of thing? Even for simpler cases without the insertUI? I do this with renderUI all over the place in my apps.


ui <- shinyUI(fluidPage(
  actionButton('addFilter', 'Add filter'),
  tags$div(id = 'placeholderFilter')

server <- shinyServer(function(input, output, session) {
  counter <- 0
  observeEvent(input$addFilter, {
    counter <<- counter + 1
    filterId <- paste0('Filter_', counter)
    colfilterId <- paste0('Col_Filter_', counter)
    valuefilterId <- paste0('Value_Filter_', counter)
    valuefilterIdUI <- paste0(valuefilterId, "UI")
    condition =  paste0 ("input.", colfilterId, "!= '' ") 
    filterTag <- fluidRow (
             selectizeInput( colfilterId, label = "Select Variable", choices = names(mtcars),
                             options = list(
                               placeholder = 'Please select an option below',
                               onInitialize = I('function() { this.setValue(""); }')
      conditionalPanel (condition, uiOutput (valuefilterIdUI))
    output[[valuefilterIdUI]] <- renderUI ({
      x <- input[[colfilterId]]
      choices <- sort(unique(mtcars[[x]]))
      column(3, selectInput(valuefilterId, label = "Variable Value(s)", choices = choices, multiple = TRUE))
      selector = '#placeholderFilter',
      ui = tags$div(id = filterId, filterTag)

shinyApp(ui = ui, server = server)