Sharing reactive data sets between user sessions in Shiny

I have a fairly large reactive data set that is derived from polling a file and then reading that file on a predefined interval. The data is updated frequently and requires constant reloading. Admittedly, the reloading could be done incrementally and appended to the existing object in R, but isn't. However currently, this action is done for each user of the shiny app although the data is the same across sessions.

The only way I have come up with a round about solution was to determine if the session is the first, and have that session be the master for updating the data through polling. The subsequent sessions don't poll provided there is still a master.

Is there an easier way to accomplish the same result without handling this master relationship?

It's almost like a need a reactive call inside of the global function that works on behalf of all clients/sessions.


Solution 1:

I hope I understood the question. I am assuming that the data set is same for all sessions and all connections. I.e.: if one user updates the data set, it should be updated for all other sessions as well? If that is the case, I would just simply read from a database and write to that database every time some user updates it. If you have a MySQL database, you can use that. Alternatively you can use an app-specific SQLite database file in your application and you can keep it together with your server.r and ui.r files and all other app-specific files.

You can still read from the database on a predefined interval. This way you don't keep a copy of the data in RAM and avoid loading it multiple times for each Shiny session.

Another option might be this. Furthermore, in your server.r file, anything you put INside your shinyServer() function will run each time a session is created (i.e. each time a user points their browser to your app). Anything you put OUTside your shinyServer() function (e.g. at the very beginning) will only run once at the initialization of your app on runApp() command. So if you load your data set once OUTside of your shinyServer() function and save it to a global environment variable, than it should be accessible by all sessions. Each session would need to re-save it to global environment as well.

assign("main_df", df, envir = .GlobalEnv)