Best practice for session-specific variables?


Inside a Shiny server function, you can create a variable, say S, that is specific to that session. However, to store things in S from inside a function like render..., observe..., reactive... etc, you have to use <<- rather than <- or =. When you forget that detail you have a bug that is really hard to find.

On the other hand, you can use session$userData instead of S. You can store things in session$userData from inside a function using either <<- or <- or =. The disadvantage is just that it’s a lot of characters to put in front of every variable.

The third option is to use a reactiveValue, say RV = reactiveValues(). The name is short and you can store things in it from inside functions with <<-, <-, or =. However, if the items you’re storing are just global variables and don’t need to be reactive, there is probably some kind of performance hit to using this method.

Does anyone know how bad the performance hit is or have any other suggestions for the best way to store session-specific data?


After thinking about this a few days, I think what I’m looking for is staticVal() and staticValues() functions to complement reactiveVal() and reactiveValues().

The static versions would not be reactive, obviously, but they would be available inside functions for storage with <- or =, just like reactive values are.

The big problem with using <<- is that you don’t get an error message about the variable not existing if you accidentally use <- or = instead.

Since <- and = actually do save the variable in the function’s environment, and since the defined variable isn’t in the function’s environment, you’d expect an error message. But when you accidentally use <- by mistake inistead of <<-, everything seems cool, but nothing useful actually happens (the variable is created in the context of the function and then disappears when the function ends). There are full details about this R bug/feature on

Is anyone else interested in something like this or is it just me?