When to use reactiveValuesToList?


Hi, everyone! I just ran across the reactiveValuesToList function and was curious - why and when would we want to use this? Isn't a reactiveValues object already a list?


A reactiveValues object is not a list. It looks like a list from the point of view of accessing it, but its structure is more complex. And it is reactive, we cannot access its values outside of a reactive domain (defined by Shiny).

Imagine a dashboard where there are very many settings for an output. The output does not update each time when you modify one of these settings but only when you press a button (e.g., it is an expensive computation, a PDF report, etc.). When you call the expensive computation function, you can pass the arguments with

    result <- expensive_calculation(a = input$a, b = input$b, ......)

After 7-8 of arguments this is no longer fun. A quicker solution is

    result <- do.call(expensive_calculation, reactiveValuesToList(input))

Plus you do not need to update the code each time you add a new setting on the dashboard.


Ah, okay, got it. I went ahead and tried your example on something simple.

rv <- reactiveValues(a = 1, b = 2)
isolate(do.call("paste", rv))

This gave me the error

Error in as.vector(x, "character") : 
  cannot coerce type 'environment' to vector of type 'character'

All worked well though if I did the following (as you suggest):

isolate(do.call("paste", reactiveValuesToList(rv)))

which gave

[1] "1 2"

P.S. I was using isolate as I was playing in the Console outside of a reactive context.

My original thinking was "if it looks like a duck and acts like a duck, then it's a duck!", where duck = list of course. It appears that a reactiveValues object is indeed not really a duck, er, list.