Reputation: 301
I have a shiny application that collects some parameters from users, which will then be used to run a simulation, which takes a long time ~ 1-3 days. So, I want the ability to tell the user to check back when the simulation is solved, as well as provide the capability to submit more input scenarios. So I want to spawn a subprocess to perform the simulation in the background, while my shiny app runs.
I tried the package 'subprocess' and 'processx', which allows one to spawn external processes and interact with them. However, I want the ability to run an entire R script with the spawned process. i.e. run source('simulation_script.R')
. Also, I do not care if I am not able to share any data with my parent shiny app. I read inputs from files and write outputs to files, which can then be shown to the shiny app user. Any pointers to achieve this would be helpful?
Upvotes: 0
Views: 1052
Reputation: 5776
When you computation process takes much longer than the user's interactive session is expected to last, you should split the entire process into 3 parts: Data input, computation, presentation of result.
Now there are several reasons for this:
So, you handle your simulation in 3 parts:
The queue and computation part are basically a database (as simple as SQLite or MySQL or MS SQL, whatever you have lying around), as long as it supports multiple processes. The computation part is then a script that loops endlessly, asks for a task and does it. This allows you to scale it (simply launching several instances of the script), moving it to more powerful calculation nodes, etc., without affecting the presentation in the Shiny apps.
Upvotes: 3
Reputation: 33580
To use r_bg
just wrap your source()
call in a function (which should be self-contained) like this:
library(callr)
# create dummy script
writeLines('writeLines(as.character(Sys.time()), "myResult.csv")', 'myRScript.R')
# execute dummy script in background R process
r_bg(function(){source('myRScript.R')})
# read results
read.csv('myResult.csv')
Upvotes: 2