Last active
June 7, 2023 12:07
-
-
Save bborgesr/48c4453bb83137815a53d8639b282fef to your computer and use it in GitHub Desktop.
Uses Shiny's insertUI to create elements (in this example, datatables) at the user's discretion; each of these comes with a button that will remove it from the app (using removeUI).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
actionButton(btnID, "Remove this table", class = "pull-right btn btn-danger"), | |
DT::dataTableOutput(dtID), | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment