mnist
mnist

Reputation: 6954

How to reset reactiveValues?

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.

Upvotes: 3

Views: 4087

Answers (1)

DS_UNI
DS_UNI

Reputation: 2650

Apparently you can't nullify reactiveValues entirely

Take a look at :

Shiny: How to initialize empty reactiveValues with an actionButton?

shiny: How to update a reactiveValues object?

based on the answers in these two question, one can - as a workaround - save the values in a list in a reactiveValues object, with your code it would be something like this:

library(shiny)
library(magrittr)

# UI ---------------------------------------------------------------------------
ui <- fluidPage(
  actionButton("create", "Create"),
  actionButton("reset", "Reset"),
  actionButton("trigger", "Trigger"),
  textOutput("out")
)

# Server -----------------------------------------------------------------------
server <- function(input, output, session) {
  # initialize vals with a list
  vals <- reactiveValues('foo' = list())
  ids <- reactiveVal()
  display <- reactiveVal()

  observeEvent(input$create, {
    id <- as.character(length(ids()))
    # add values to the list
    vals$foo[[id]] <- sample(LETTERS, 1)
    ids(c(ids(), id))
  })

  observeEvent(input$reset, {
    # reset the list
    vals$foo <- NULL
    ids(NULL)
  })

  observe({
    # if(input$trigger) browser()
    text <- reactiveValuesToList(vals) %>% paste(collapse = ", ")
    display(text)
  })

  output$out <- renderText({
    text <- vals$foo %>% paste(collapse = ", ")
    display(text)
    display()
  })
}

shinyApp(ui, server)

Upvotes: 6

Related Questions