What is better practice in an R Shiny app: Multiple datasets within one reactiveValues(), or multiple reactiveVal()?

Viewed 90

Shiny offers reactiveVal() and reactiveValues(). The former holds a single value, the latter behaves like a list. In an app with multiple tables and various variables is it better practice to use a single reactiveValues() object as a container, or to try to split things up between multiplereactiveValues() and reactiveVal objects where possible?

More generally are there any downstream effects from using one as opposed to the other, such as use cases where one would work but the other may lead to errors or be more difficult to work with, or is it purely a personal style preference?

1 Answers

Looking at this book written by Hadley Wickham and collaborators and this chapter in particular, it seems like reactiveVal() and reactiveValues() are indeed very similar with the latter (as you state) behaving more as a list. There doesn't seem to be any noticeable downstream effects and it would seem that if you have a lot of reactive values to keep track of, possibly having them all in a reactiveValues() list would be easier in terms of code readability/organization. As the documentation to reactiveVal states:

For a one-off reactive value, it's more natural to use reactiveVal

That documentation also features an example which illustrates in parallel how reactiveVal() and reactiveValues() can be used to achieve the same goal.

In the end, it depends on how many reactive values you need to work with (and being critical about do they all really need to be reactive).

Interestingly, you can see the rationale behind introducing reactiveVal here and here

Related