I have one code in which I add and remove objects by using insertUI and removeUI as it can be seen below:
library(shiny)
# Define the UI
ui <- fluidPage(
actionButton("adder", "Add"),
tags$div(id = 'placeholder')
)
# Define the server code
server <- function(input, output) {
rv <- reactiveValues()
rv$counter <- 0
observeEvent(input$adder,{
rv$counter <- rv$counter + 1
add <- sprintf("%03d",rv$counter)
filterId <- paste0('adder_', add)
divId <- paste0('adder_div_', add)
elementFilterId <- paste0('adder_object_', add)
removeFilterId <- paste0('remover_', add)
insertUI(
selector = '#placeholder',
ui = tags$div(
id = divId,
actionButton(removeFilterId, label = "Remove filter", style = "float: right;"),
textInput(elementFilterId, label = "Introduce text", value = "")
)
)
# Observer that removes a filter
observeEvent(input[[removeFilterId]],{
rv$counter <- rv$counter - 1
removeUI(selector = paste0("#", divId))
})
})
}
# Return a Shiny app object
shinyApp(ui = ui, server = server, options = list(launch.browser = T))
The problem that I'm experiencing is that if I add one UI (click on Add), then remove it (click on Remove filter) and then add a new one (click on Add again), the first time I click on it, doesn't work.
I know that it's due to the fact that I'm using an ID I've used previously but, theoretically, I've completely removed it with the removeUI instruction.
What am I missing in here?