-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: allow deleting an input that was removed using removeUI()
#2374
Comments
removeUI()
removeUI()
For anyone looking for a workaround until this gets implemented, my solution meanwhile it to set the value of any removed inputs to |
Here's a reprex of this issue:
I definitely second the idea of removing the value from the input list if the input is not there anymore. The |
Just dropping a thought here. If you're managing the lifecycle of modules using objects from a bespoke class system, for example, R6, you can register your takedown code as the finalizer function of the module object. The finalizer function will run as the object is garbage collected. |
Thank you so much for solution. Is this way to update all variable in 'input' as null ? Could I just update one variable as null? Thanks in advance. |
@ColinFay thank you for the clear example. I'm surprised at how few examples I can find of removing the input / server data along with the UI. I've taken the example above and built out a working example that shows a method of removing both the UI and the server elements. My approach wraps the In the below Very happy to take advice on the approach. Hopefully this helps others in the future! library(shiny)
library(purrr)
## Text Module ----
textUI <- function(id) {
div(
id = NS(id, "ui"),
actionButton(NS(id, "remove"), "Remove UI"),
textInput(NS(id, "text"), "This is no longer useful", "plop")
)
}
textServer <- function(id) {
moduleServer(id, function(input, output, session) {
list(
text = reactive(input$text),
delete = reactive(input$remove)
)
})
}
## Main App ----
# utility to hide away the mess of updating the reactiveVal(list())
update_values <- function(values, name, value) {
vals <- values()
vals[[name]] <- value
values(vals)
}
add_module <- function(values, name, server, delete_hook = NULL, remove_selector = NULL) {
# add module server's return to values list
update_values(values, name, server)
# if module has a reactive we should monitor for deleting, do so
if (!is.null(delete_hook)) {
observeEvent(
server[[delete_hook]](), {
removeUI(selector = remove_selector) # remove the ui
update_values(values, name, NULL) # remove the server from our values list
},
once = TRUE
)
}
}
ui <- fluidPage(
actionButton("addModule", "Add Module"),
verbatimTextOutput("wordsOutput")
)
server <- function(input, output, session){
# can't use reactiveValues as can't remove items from them
module_values <- reactiveVal(list())
observeEvent(input$addModule, {
id <- paste("module", input$addModule, sep = "-")
insertUI(selector = "#addModule", where = "afterEnd", ui = textUI(id))
add_module(
module_values,
name = id,
server = textServer(id),
delete_hook = "delete",
remove_selector = paste0("#", NS(id, "ui"))
)
})
words <- reactive({
map(module_values(), ~.x$text())
})
output$wordsOutput <- renderPrint(words())
}
shinyApp(ui, server) |
I guess this only works with reactive value as a middle layer whilst the input is still there? |
Have we got any updates regarding this issue? It would be indeed quite useful to have a |
Hi everyone, I have been struggling for a few days now regarding this issue. A user can create a cage (for storing animals) in my app. The cage has an ID given by the user, say - 22818. This ID will be used to generate all following dependencies dynamically (buttons to open cage, modify, add animals etc..) each of the inputs created will contain "22818" (modify22818, addAnimal22818 etc..). When deleting that cage, destroying all inputs that come up from a grep of pattern "22818" would solve the issue, but I am aware that there could be better options. I am open to any suggestions/workaround, thank you very much in advance. |
I wouldn’t use the real ID to make those input IDs unique, I’d make them unique using a random value that has no relation to anything. Back to the larger issue in the thread, it seems like there are related features of 1) removal of input values from the |
Just assign the moduleServer call to a variable:
Then remove server with Along with the removeUI functions and remove_inputs shown above, this should remove the module completely, right ? Context: I am creating dynamic modules and updating modules by calling the moduleServer function repeatedly. So each observeEvent were triggered n number of times the moduleServer was called. |
I think item 2 is needed as well. It would be very helpful to have a For modules that are inserted and removed based on user input, developers could follow a pattern like # when module is added
insertUI(my_module_ui('id'))
my_module_server('id')
# when removed
removeUI('id')
removeServer('id') |
Currently, when an input is removed using
removeUI()
, its UI is deleted from the DOM but the input still lives from the server's perspective. It would be great if it was possible to clear that input completely.Personal opinion: I would want this to be the default behaviour, because an input that does not exist on the page should not exist in the server - it's a bit of a logical discrepancy - but that breaking change might cause a lot of trouble so perhaps adding a parameter to
removeUI()
to do this would be helpful.The text was updated successfully, but these errors were encountered: