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.
For anyone looking for a workaround until this gets implemented, my solution meanwhile it to set the value of any removed inputs to NULL
using javascript, with Shiny.onInputChange("input", 'null')
Here's a reprex of this issue:
library(shiny)
ui <- fluidPage(
actionButton("rmv", "Remove UI"),
textInput("txt", "This is no longer useful", "plop")
)
# Server logic
server <- function(input, output, session) {
observeEvent(input$rmv, {
removeUI(
selector = "#txt"
)
})
observe({
invalidateLater(1000)
print(input$txt)
})
}
shinyApp(ui, server)
I definitely second the idea of removing the value from the input list if the input is not there anymore.
The removeUI()
function which can remove any part of the UI has to be kept, but there could be a removeInput()
function, to be more specific to target the inputs.
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.
Most helpful comment
For anyone looking for a workaround until this gets implemented, my solution meanwhile it to set the value of any removed inputs to
NULL
using javascript, withShiny.onInputChange("input", 'null')