Automating R scripts on the R Studio Connect Server

I have a shiny app hosted on an r studio connect server. The app consists of an app.r file and data.r file. The data.r file is sourced at the top of the app.r file using the source() function. My question is does the data.r file run again every time the app is accessed?

If not, could I schedule that data.r file from the rstudio connect ui to run periodically?

1 Like

Hello! This is a fantastic question!

When users visit your application on RStudio Connect, they are connected to a running R process. Each R process, when it starts, will run the app.R file and thus source the data.R script. However, each R process can live for a while, depending on your settings inside of RStudio Connect. I.e. if there is a steady stream of visitors or Minimum Processes > 0 (in Scaling Settings), then an R process could stick around for a long time.

Further, it is worth noting that many R processes can be run at once. RStudio Connect will start new R processes as needed if many visitors visit the application (based on the "Scaling Settings" in the Connect UI).

I'm curious what your thoughts are regarding the above! Does that seem to work naturally for your use case?

Thank you @cole for your reply. For my situtation, I need the data.r file to run periodically. I would rather be able to schedule it like I can with a markdown report, but without rendering an html file. The data.r file pulls from a database and needs to update frequently to reflect changes in the data. What would you suggest?

1 Like

One pattern we've seen be successful is for the data prep to be pulled out of the shiny app and into a scheduled R Markdown document. Then the shiny app can refer to the output of the RMD to get access to the latest data, normally using a reactiveFileReader or reactivePoll function.

More details:

We are also working on a new experimental feature in RStudio Connect that should make this process of connecting a RMD and a shiny app even easier! Please keep your eye out for the next RStudio Connect blogpost which will have more details, or shoot me an email if you want to see a preview: sean@rstudio.com.

1 Like