tic-toc-choc
tic-toc-choc

Reputation: 960

Use of the session parameter in shiny apps

As described in RStudio reference docs, shiny server functions can optionally include session as a parameter (e.g. function(input, output, session)). The session object is an environment that can be used to access information and functionality relating to the session.

I never use this parameter in my apps and could be missing something.

What are the practical uses of the session parameter?

Upvotes: 2

Views: 3939

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17719

Here is my attempt for an overview:

List of use cases:

  • Customised user interfaces
  • chat room
  • games
  • Javascript communication
  • Trigger a function when session is ended
  • global reactive values for modularised shiny apps
  • updating inputs

Trigger a function when session is ended

E.g. close a database connection, see How to implement a cleanup routine in R Shiny?.

Stopping a shiny app when browser / session is closed:

How to stop running shiny app by closing the browser window?

Customising the user interface

The user interface will be dependent on the device from which its called. Is it a mobile or pc, which screen resolution is used etc.

Using fluid pages in the ui will help, but certainly has there limits as well.

With session$clientData$output_{OUTPUT_ID}_height and session$clientData$output_{OUTPUT_ID}_width you can track how your outputs are rendered for your user. You could make live adjustments (dont insert my huge title if the plot is too small). Or you can track the data and adjust your ui after finding the most common ui settings of your users.

User interactions

You can create a local/secret reactiveValue() within that session / for that user and you can set reactiveValues() outside the server function for "global infos" that are accessible across users/sessions.

That way you can share infos across sessions, but also hide specific values for certain users.

Use Case example: Chat room https://shiny.rstudio.com/gallery/chat-room.html

Use Case example: Games Can R Shiny display different views for two simultaneous users, interacting with one another?

Share data between modules

As mentioned in the comment, there is a current open bounty where it is asked to have global reactive values for modularised shiny apps. See Shiny modules: Destroy module ui if server-function fails and

https://appsilon.com/super-solutions-for-shiny-architecture-1-of-5-using-session-data/?nabe=4634331497365504:0.

Finally, there is Some functionality you indirectly use, since there are good wrapper functions / packages for that.

Sending messages to Javascript

(There are good wrappers for this. E.g. shinyjs package).

If you want to integrate javascript in your app and send a message from shiny to javascript you can do it with session:sendCustomMessage().

See, e.g. http://www.blog.rdata.lu/post/2017-09-16-communication-between-r-and-d3js/ and https://shiny.rstudio.com/articles/js-send-message.html.

Updating inputs

You could use session$sendInputMessage(inputId, message) to update inputs. But again there are already more convenient wrapper functions for this, e.g. updateTextInput().

But it´s interesting to keep in mind for inputs that dont have a helper function.

General overview:

https://shiny.rstudio.com/reference/shiny/latest/session.html

Upvotes: 8

Related Questions