reactiveValues vs reactive and eventReactive: a general question

Couple questions in here that I'll give my thoughts on.

Is reactiveValues function completely useless?

  • No. inputs and clientData area actually reactiveValues objects. They are helpful when sets (with possibly unknown names) of reactive information is necessary.
  • Of the objects that I create, they are 95% reactive objects.

Can all reactivity needs for assigning values in a shiny app be satistfied using reactive() and eventReactive()?

  • No. That being said, a lot of user input handling will only be using reactive() and eventReactive().
  • Example: Let's say you want to make a "deduplication" reactive. "It should react to an expression, but it should not update the value unless it's a new value." This would require a reactive expression, an observer, and a reactiveVal.
dedupe <- function(expr) {
  r <- reactive(expr)
  deduped <- reactiveVal(NULL)
  observe({
    deduped(r())
  })
  return(deduped)
}

Is reactiveValues() bad code smell?

  • No. I think of reactiveValues() as a reactive list(), rather than a reactive object. Since it is a list, there are a few extra methods that work with a reactiveValues() list object. Example: var <- reactiveValues(); r <- reactive()
    • names(var): If this was done with a reactive, it'd have to be: names(r()), assuming r produces a list object.
    • as.list(var): will produce a list object with all key value pairs.

tldr;

  • Use reactiveValues if you'd like a reactive() list.
  • 95% of my code is reactive() or observeEvent()
  • Use observe() only if side effects are necessary.
2 Likes