Resetting a single reactive Value is simply done by reactiveVal(NULL)
. However, how can I completely reset reactiveValues()
?
The dummy app contains some of my approaches to retain fresh and clean reactive Values yet none of them really do what I would like them to do. Additionally, there seems to be a strange behavior when observing reactiveValues
. They do not trigger reactivity after cleaning unless the Trigger
button is clicked. When I inspect their status, they look fine to me.
library(shiny)
library(magrittr)
# UI ---------------------------------------------------------------------------
ui <- fluidPage(
actionButton("create", "Create"),
actionButton("reset", "Reset"),
actionButton("trigger", "Trigger"),
textOutput("out")
)
# Server -----------------------------------------------------------------------
server <- function(input, output, session) {
vals <- reactiveValues()
ids <- reactiveVal()
display <- reactiveVal()
# insert letter when clicked
observeEvent(input$create, {
id <- as.character(length(ids()))
vals[[id]] <- sample(LETTERS, 1)
ids(c(ids(), id))
})
observeEvent(input$reset, {
# Options to reset reactive Values -------------------------------------
vals <<- reactiveValues()
# vals <- NULL
for(i in names(vals)) vals[[i]] <- NULL # deletes content but not the names
# resetting reactiveVal() is easily done via NULL
ids(NULL)
display(NULL)
})
observe({
if(input$trigger) browser()
text <- reactiveValuesToList(vals) %>% paste(collapse = ", ")
display(text)
})
output$out <- renderText(display())
}
shinyApp(ui, server)
P.S.: the example is not stripped down entirely because I want it to mirror my actual reactive chain.