Last active
August 6, 2020 02:21
-
-
Save beader/bbf2221ab6f84a819c0f to your computer and use it in GitHub Desktop.
ShinyApp - Dynamically Add/Remove UI Elements
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
Title: Dynamically Remove UI Elements | |
Author: [email protected] | |
DisplayMode: Showcase | |
Type: Shiny |
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) | |
removeMeButton <- function(id, label = paste0("Remove Me, I am ", id)) { | |
actionButton(paste0("rmBtn", id), label = label) | |
} | |
shinyServer(function(input, output) { | |
container <- reactiveValues(uiComponents = list()) | |
output$container <- renderUI({ | |
container$uiComponents | |
}) | |
observe({ | |
if(is.null(input$addBtn) || input$addBtn == 0) return() | |
isolate({ | |
uiComponentCount <- length(container$uiComponents) | |
container$uiComponents <- append( | |
container$uiComponents, | |
list( | |
list( | |
"removeBtn" = removeMeButton(id = as.integer(runif(1,1e5,1e6-1))), | |
br(),br() | |
) | |
) | |
) | |
}) | |
}) | |
observe({ | |
if(length(container$uiComponents) == 0) return() | |
rmBtns <- lapply(container$uiComponents, `[[`, "removeBtn") | |
rmBtnIds <- sapply(container$uiComponents, function(uiCom) { | |
uiCom$removeBtn$attribs$id | |
}) | |
rmBtnVals <- sapply(rmBtnIds, function(btnId) input[[btnId]]) | |
if(any(sapply(rmBtnVals, is.null))) return() | |
if(all(rmBtnVals == 0)) return() | |
isolate({ | |
container$uiComponents[[which(rmBtnVals > 0)]] <- NULL | |
}) | |
}) | |
}) | |
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) | |
shinyUI(fluidPage( | |
titlePanel("Dynamically Add/Remove UI Elements"), | |
hr(), | |
actionButton("addBtn", "Add"), | |
br(),br(), | |
uiOutput("container") | |
)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @jameswcraig
I think I have totally forgot what I have written almost 6 years ago.
BTW, I think Python Voila project can do a better job for writing "tiny" data analysis web applications.
I am sorry I can't help.