Chapter 3 Interaction

Insert and remove tables

library(shiny)
ui <- fluidPage(
  textInput("divID", "Enter an ID for the custom area:", ""),
  helpText("Leave the text input blank for automatically unique IDs."),
  actionButton("isrt", "Add a datatable"),
  tags$div(id = "placeholder")
)

server <- function(input, output, session) {
  rv <- reactiveValues()

  # take a dependency on `isrt` button
  observeEvent(input$isrt, {

    # handle the case when user does not provide ID
    divID <- if (input$divID == "") gsub("\\.", "", format(Sys.time(), "%H%M%OS3"))
    else input$divID
    dtID <- paste0(divID, "DT")
    btnID <- paste0(divID, "rmv")

    # only create button if there is none
    if (is.null(rv[[divID]])) {

      insertUI(
        selector = "#placeholder",
        ui = tags$div(id = divID,
                      DT::dataTableOutput(dtID, width = "60%"),
                      actionButton(btnID, "Remove this table", class = "pull-left btn btn-danger"),
                      hr()
        )
      )

      output[[dtID]] <- DT::renderDataTable(
        {head(iris)}
      )

      # make a note of the ID of this section, so that it is not repeated accidentally
      rv[[divID]] <- TRUE

      # create a listener on the newly-created button that will
      # remove it from the app when clicked
      observeEvent(input[[btnID]], {
        removeUI(selector = paste0("#", divID))

        rv[[divID]] <- NULL

      }, ignoreInit = TRUE, once = TRUE)

      # otherwise, print a message to the console
    } else {
      message("The button has already been created!")
    }
  })
}

shinyApp(ui = ui, server = server)