That's a convenient idea for keeping track of the number of buttons. However, there is still the problem that any plus button generates a new filter row, which also has a plus button at its end (as commonly done on web forms for booking sites). The typical example of dynamic UIs on the RStudio examples website is where the dynamically generated element does not have the capability to generate other dynamic elements from it.
This problem could be conveniently avoided if it were possible to create observers that listened to a class of inputs rather than a single element ID. For example, a pseudocode representation is:
ui <- (textInput, selectInput, plusButton1(class = "insert-ui-button"), minusButton1(class = "remove-ui-button")) # first row of filters.
server <- function{
reactivesTotal <- reactiveValues(total = 1)
}
observeEvent(input[[".insert-ui-button"]] { # Catch all events triggered by a particular class of HTML element.
triggerID <- getElementIDofTrigger()
insertUI(selector = triggerID,
ui = div(textInput, selectInput, plusButton2(class = "insert-ui-button"), minusButton2(class = "remove-ui-button")))
reactivesTotal[["total"]] = reactivesTotal[["total"]] + 1
})
observeEvent(input[[".insert-remove-button"]] {
triggerID <- getElementIDofTrigger()
removeUI(selector = triggerID)
reactivesTotal[["total"]] = reactivesTotal[["total"]] - 1
})
This would only require one listener for the plus buttons and one for the minus buttons, regardless of how many such buttons there are in the user interface at any time.