title: Shiny: dynamically add/ remove textInput rows based on index
body: <p>Based on <a href="https://gallery.shinyapps.io/111-insert-ui/" rel="nofollow noreferrer">this article</a> I changed my script:</p>
  
  <pre><code>library(shiny)
  
  ui = fluidPage( 
    actionButton("add", "Add new Row", icon=icon("plus", class=NULL, lib="font-awesome")),
    actionButton("remove", "Remove last Row", icon=icon("times", class = NULL, lib = "font-awesome")),
    tags$div(id = 'placeholder')
  )
  
  server = function(input, output) {
  
    ## keep track of elements inserted and not yet removed
    inserted &lt;- c()
  
    observeEvent(input$add, {
      id &lt;- ''
      insertUI(
        selector = "#placeholder",
        where = "beforeBegin",
        ui =tags$div(
          id = id,
          fluidRow(
            column(2,
                   textInput("alias", label = h5("first"))
            ),
            column(2,
                   textInput("pop", label = h5("second"))
            ),
            column(2,
                   textInput("parent", label = h5("third"))
            ),
            column(2,
                   textInput("dims", label = h5("fifth"))
            ),
            column(2,
                   textInput("method", label = h5("fourth"))
            ),
            column(2,
                   textInput("args", label = h5("sixth"))
            )
          ) 
        )
      )
  
      inserted &lt;&lt;- c(inserted, id)
  
    })
  
    observeEvent(input$remove, {
      removeUI(
        ## pass in appropriate div id
        selector = paste0('#', inserted[length(inserted)])
      )
  
      inserted &lt;&lt;- inserted[-length(inserted)]
    })
  
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>I looked into <a href="https://stackoverflow.com/questions/37504933/dynamically-add-and-remove-uioutput-elements-based-on-index-using-actionbuttons">this question</a> but am more confused than before.The <code>add</code> button works fine, but the rows do not disappear when hitting the <code>remove</code> button. What am I doing wrong?</p>
  
  <p>Thanks!</p>