29

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).

Mehrad Mahmoudian
  • 3,466
  • 32
  • 36
uday
  • 6,453
  • 13
  • 56
  • 94
  • 5
    This is not possible. R is single-threaded. Run two separate R processes instead. – Thomas Dec 15 '13 at 17:28
  • 3
    Also, please don't conflate RStudio with R. RStudio is simply an IDE. RStudio launches an instance of R itself, which is what is actually running your code. – joran Dec 15 '13 at 19:16
  • Which means that running a batch file should succeed. – IRTFM Dec 16 '13 at 01:45
  • 5
    @Thomas That R is single threaded doesn't make it not possible to do. It just means that the hypothetical `runinBackground` function would fork a new separate R thread while leaving the "main" thread unencumbered to continue to tinker. – Dean MacGregor Jun 12 '14 at 14:04

4 Answers4

26

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

HenrikB
  • 6,132
  • 31
  • 34
11

You can always do this, which is not ideal but works for most purposes:

shell(cmd = 'Rscript.exe some_script.R', wait=FALSE)
Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61
11

RStudio as of version 1.2 provides this feature. To run a script in the background select "Start Job" in the "Jobs" panel. You also have the option of copying the background job result into the working environment.

bensentropy
  • 1,293
  • 13
  • 17
8

The mcparallel() function in the parallel package will do the trick, if you are on Linux, that is ...

library(parallel)
Job1 = mcparallel(code1())
JobResult1 = mccollect(Job1)
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Knarpie
  • 258
  • 4
  • 7
  • This is great - is there any way to print to console from the second task to notify the user when it's finished? – Ben Jones Feb 13 '19 at 15:23
  • 1
    @BenJones You could simply wrap your code into a function that prints to screen: `wrapper <- function(){ code1(); print("done") }; Job1 <- mcparallel(wrapper()); JobResult1 = mccollect(Job1)` – ssanch Dec 03 '19 at 21:45