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?
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
Related
I have created a shiny app on the backbone of {golem} + {brochure}, both fantastic packages by #ColinFay. When using single R sessions as suggested in Chapter 16, it is better to use inner-session asynchronous code, so that users aren't blocked waiting for a render function.
In my case I have four modules that output a plot on a dashboard. I would like to send this plotting logic elsewhere while the user interacts with the app. Since brochure is natively a multi-session app infrastructure, is cross-session async capable of keeping users seperate being that it is two separate sessions, or will there still be issues between users on the same endpoint (session)?
TL;DR
Is each brochure page session restricted to one user? or as the docs say, is each page opened a new session and unique (thus only needing cross-session async) -- scroll down to design pattern here to see brochure structure
Do I need to implement inner-session async code blocks with promises + future?
I know this isnt the best SO question but I really could use some lexical insight!
Is there a way to keep some action rolling in the background of the app to prevent the phone screen from automatically locking?I mean some way to avoid the automatic screen lock that occurs on cell phones
R works normally in a single thread, meaning that if you have some long task running, the shiny interface will not be updated until the task is completed (wherever the user is on a mobile device or not).
You should probably explore the use of the libraries future and promises in conjunction with shiny in order to split the workload between different threads and keep the user interface responsive.
Using promises with Shiny is a good primer on the subject.
How can I monitor a change to a file system (or other external event) in real time using a Shiny app? For example, suppose I have a dashboard app that provides statistics on files located on the server. I would like to update my statistics in real time as those files change or new ones are added.
My thought is to use Javascript on the client to regularly poll the server. Every 10 seconds, e.g., I might call Shiny.setInputValue("check.fs", 1, {priority: "event"}); and then on the server I'd have observeEvent(input$check.fs, { ... }), which would update reactives.
Any other ideas? Is there any way to do this only on the server without javascript?
shiny has a function called invalidatelater with wich you can invalidate a given reactive context. This makes it possible to reevaluate a function with a certain interval.
https://shiny.rstudio.com/reference/shiny/1.0.5/invalidateLater.html
Hope this helps!
In my multi-user meteor application design I want to enable users to be able to create and store their own reactive dashboards to visualize data that they own within the applications database. For example, a user may have an object in the database representing the real-time disk usage of a processor. I want them to be able to submit/store html say to represent a dynamic dial as their dashboard. Another user may have their own weather station and want a dashboard with a last 24 hours thermometer and pressure trend. When they call up one of their stored dashboards it is rendered and would update as their data changes.
Can anyone point to example code or explain how to accomplish this? Or, authoritatively explain why it cannot be done in the framework. I have come across various dynamic API's but nothing that fits the bill. I.e. UI.renderWithData and Meteor._def_template.
The following topic was very similar to my questions and it got me a good start and I figured it out and posted and answer there.
How to make meteor evaluate user defined template text
I have a flex UI which communicates with server using spring blazeds. I have a very typical requirement of Live data streaming on a panel in UI.
Requirement is like this,
We have an object say, Person and server keeps pushing its data on blazeds message destination where UI panel consumer has subscribed and hence user can see this live data in a panel.
User can open multiple panels in same browser to view data for different Persons at the same time (e.g id=1,2,3 respectively)
There can be multiple UI users who may be viewing the same data for person id=1 at a given time, lets say.
Basically I want to separate data between UI panels. UI panel opened for person id=1 must not display data of that for person id=2. I am doing this using headers-selectors, but apparently I found out that the differentiation to select and display what data happens on UI which is causing performance issues. I learned that using subtopics may improve performance.
But, before proceeding for it, I want to know, will UI browser end up in receiving data irrespective of subtopic assigned to data and then consumer will decide to accept it or not? If so, the problem will remain the same. Is there any good way to achieve the objective.
-Miral
Yes, it is faster to use subtopics, check this article - http://cornelcreanga.com/2010/09/blazeds-message-selectors-vs-subtopics/
The message filtering will be done only on the server, no mater what approach are you going to choose.
As a suggestion you should check this guide and create some scenarios, if you think that you can hit some performance problems.