R - Run source() in background

2019-01-17 17:51发布

I want to execute a R script in the background from the R console.

From the console , i usually run R script as source('~/.active-rstudio-document') I have to wait until the script is completed to go ahead with my rest of work. Instead of this i want R to be running in the background while i can continue with my work in the console. Also i should be somehow notified when R completes the source command. Is this possible in R ?

This might be quite useful as we often sees jobs taking long time.

PS - i want the source script to be running in the same memory space rather than a new one. Hence solutions like fork , system etc wont work for me. I am seeing if i can run the R script as a separate thread and not a separate process.

标签: r background
1条回答
混吃等死
2楼-- · 2019-01-17 18:20

You can use system() and Rscript to run your script as an asynchronous background process:

system("Rscript -e 'source(\"your-script.R\")'", wait=FALSE)

At the end of your script, you may save your objects with save.image() in order to load them later, and notify of its completion with cat():

...
save.image("script-output.RData")
cat("Script completed\n\n")

Hope this helps!

查看更多
登录 后发表回答