Run R commands after restarting shiny-server

Hi there,

I'm looking for a (simple) way to run r script when restarting shiny-server.
I need to re-create / consolidate databases into R that will be used/queried in my shiny application.
The problem is that such operations take a long time (probably > 5 minutes), and thus, can not be run too often.

My understanding of global.R is that this script is executed the first time the application is launched after a fresh restart of shiny-server.
In my case, I'd like to find a way to launch such a script when restarting the server, not waiting for a user to connect to the app.
So, some sort of a globaller.R script.

Any idea how to make this without having to rely on crontab or any scheduled way ?

My workflow is that I have a DB built from numerous (and heavy) CSV files. But this DB can sometimes get corrupted, and then, I have to build it again. Considering the DB (MonetDBLite) relies on the shiny-server (need to restart it so that the DB is available again after creating it), it would be quite efficient to have the restart trigger a script, so that I don't have to run anything manually after restarting shiny-server or rebooting the server.

Thanks for any help :slight_smile:

There are lots of ways to think through this. Right off the bat, I would be shooting for using a database that doesn't get corrupted with any degree of regularity. I also would take a look at creating a persistent DB (SQLite or PostgreSQL come to mind) so that you do not have to restart shiny-server to rebuild the database. If you want fresh data, DROP TABLE, TRUNCATE TABLE and friends are always happy to help! The other option would be to store a Rmd on the server and schedule it with a cron to refresh the database, if you need a certain interval of regular refresh. The benefit of this is that you can get a nice, user-readable output of your "ETL" thanks to Rmarkdown! (You can also host Rmarkdown on shiny-server, if you were not aware. See the examples included by default)

In any case, more to the flavor of your question, the simplest work-around I can think of is to build your own custom restart script. Perhaps something like

#!/bin/bash
echo "restart the server"
shiny-server restart

## wait for the server to come up
echo "now doing cool things"

If you name it restart-shiny and throw it in /usr/local/bin, then executing restart-shiny at the command line will do all of the things you want. IMO, this is a cleaner solution than a hidden hook into the starting of the shiny-server (which I am mildly confident does not exist).

Also, I believe global.R is run at the start of the R session. This is different from

the first time the application is launched after a fresh restart of shiny-server

Because after the app goes idle and the R session dies, it will require a new connection to run global.R again. This also becomes quite different when talking about Shiny Server Pro, Connect, or shinyapps.io.

Lastly, I think you would probably benefit from checking out shiny.rstudio.com, if you have not already, and perhaps this page in particular. There is tons of useful information on Shiny app architecture over there!

1 Like