Session object questions

24 views
Skip to first unread message

3D0G

unread,
May 20, 2017, 1:33:03 PM5/20/17
to Shiny - Web Framework for R
Is the session object a list? Can data items be stored in the session object for the duration of a session? Every user of a shiny instance has his or her own session object, right?

If so, what are the pros and cons compared to global variables? Are globals shared by all users of a shiny instance?

Tom

Joe Cheng

unread,
May 20, 2017, 2:00:56 PM5/20/17
to 3D0G, Shiny - Web Framework for R
Generally the session object should not be used to store per-user things (with one exception I'll mention in a sec). Rather, declare variables inside the server function; since the server function is run once per session, each session will have its own copy. See more info here: https://shiny.rstudio.com/articles/scoping.html

The exception is for package authors who want to offer functions that need to track their own session-specific but internal state. For example I believe shinyjs may use this to ensure it's been initialized before it sends commands to the client. In these cases you don't store things directly on the session object (which you should think of being more like a reference class or R6 object) but in a special environment called session$userData.
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/f0fecc9a-f81d-44d7-8e46-788c95bfec5f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

3D0G

unread,
May 20, 2017, 3:03:39 PM5/20/17
to Shiny - Web Framework for R
Joe - thanks for the insight about declaring session-specific variables inside the server function. That makes great sense. 

I've been developing and using an application on my own machine and am trying to figure out what I need to do to move it to a server and make it multi-user. If I put shared data on a MySQL server and user data in the server function, do I even need globals for anything? - Tom

Joe Cheng

unread,
May 22, 2017, 9:28:33 PM5/22/17
to 3D0G, Shiny - Web Framework for R
It's hard to answer that question without more context. But certainly, many apps do not require global data in the R process.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

3D0G

unread,
May 22, 2017, 11:12:34 PM5/22/17
to Shiny - Web Framework for R
Joe - ​The scoping rules article (https://shiny.rstudio.com/articles/scoping.html) you referred me to in your​ first response explains it all really well; I know I read that when I started using Shiny, but I learned a lot reading it again now. There are certainly things like non-reactive functions that only need to be loaded once per instance for use by all sessions. And that article explains where those kinds of things go. Thanks for your help in thinking through my next steps with this project. - Tom


Reply all
Reply to author
Forward
0 new messages