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).
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With