How to trigger Shiny App to preload the data


#1

Hi:

I have a shiny app, which has a chuck of code to preload required data. This takes long time but it only needs to run once everyday. The following is the structure of my codes in server.R file. The problem is the shiny_preload_data() function only gets triggered when the first user access the app and this user has to wait for a long time for the data to be ready. My question is: is there a way to trigger the shiny_preload_data() without the first user opens a browser to access this app? Basically I want to have the data ready before the first user access the app.

In side my server.R function, the code structure looks like this:

shiny_preload_data()

shinyServer(function(input, output, clientData, session) {

})


#2

What format is your data in? Are you querying it from a database every morning? Or is it in something like an rds file and still takes a long time to load?


#3

Inside the shiny_preload_data() function, I am querying SQL database every morning, then return a data frame for the Shiny App to consume.
One possible solution is to put this shiny_preload_data() function in the global.R file so that it will run every time my App restart. But that requires restarting my App every morning.

Is there a configuration variable I can turn on to force Shiny Server to trigger the run of shiny_preload_data() function? Essentially I just need a simulation of opening the shiny app URL in a browser. This would trigger this function to run. (I tried curl -X GET . It did not work as this command does not have a javascript engine).

— Actually put shiny_preload_data() in global.R, then restart the shiny server, won’t work. Whatever in global.R still expect the first user to trigger the logic inside. So it not a solution.


#4

The way I have solved this in the past is to put all my “data gathering” code into a script that runs at some time interval (e.g. every morning) by a cron job. The output of this script would be an RDS file with all the objects the shiny application requires. When the shiny application loads it just reads in the RDS file (which is really fast) and it has all the data it needs.

However, I would definitely be interested to hear if people have approached this in a different way.


#5

Would this work with rstudio connect, can you set up a cron job and have the shiny app access its output?