rstudio - is it possible to run a code in the background

uday picture uday · Dec 15, 2013 · Viewed 10.4k times · Source

Question regarding RStudio. Suppose I am running a code in the console:

> code1()

assume that code1() prints nothing on the console, but code1() above takes an hour to complete. I want to work on something else while I wait for code1(). is it possible? Is there a function like runInBackground which I can use as follows

> runInBackground(code1())
> code2()

The alternatives are running two RStudios or writing a batch file that uses Rscript to run code1(), but I wanted to know if there is something easier that I can do without leaving the RStudio console. I tried to browse through R's help documentation but didn't come up with anything (or may be I didn't use the proper keywords).

Answer

HenrikB picture HenrikB · Mar 6, 2017

The future package (I'm the author) provides this:

library("future")
plan(multisession)

future(code1())
code2()

FYI, if you use

plan(cluster, workers = c("n1", "n3", "remote.server.org"))

then the future expression is resolved on one of those machines. Using

plan(future.BatchJobs::batchjobs_slurm)

will cause it to be resolved via a Slurm job scheduler queue.

This question is closely related to Run asynchronous function in R