Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

rstudio

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

like image 263
uday Avatar asked Dec 15 '13 16:12

uday


People also ask

Can R run in the background?

RStudio 1.2 introduced the ability to send long running R scripts to local and remote background jobs. This functionality can dramatically improve the productivity of data scientists and analysts using R since they can continue working in RStudio while jobs are running in the background.

How do I run code in RStudio script?

The RStudio interface is simple. You type R code into the bottom line of the RStudio console pane and then click Enter to run it. The code you type is called a command, because it will command your computer to do something for you.

How do I run a local job in R?

You can run any R script in a separate session by pulling down the Source menu and choosing Source as Local Job. This will give you some options for running your job. By default, the job will run in a clean R session, and its temporary workspace will be discarded when the job is complete.

How do I run a full script in R?

In addition, in Rstudio you can run the entire script by pressing Ctrl + Shift + Enter without selecting any code. In addition, there is a shortcut to source the current script file ( Ctrl + Shift + s ), which runs the script without echoing each line.


1 Answers

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

like image 96
HenrikB Avatar answered Sep 20 '22 13:09

HenrikB