R - 在后台运行 source() [英] R - Run source() in background

查看:34
本文介绍了R - 在后台运行 source()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 R 控制台在后台执行 R 脚本.

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

从控制台,我通常将 R 脚本作为 source('~/.active-rstudio-document') 运行我必须等到脚本完成才能继续我的其余工作.相反,我希望 R 在后台运行,同时我可以在控制台中继续我的工作.当 R 完成源命令时,我也应该以某种方式得到通知.这在 R 中可能吗?

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 - 我希望源脚本在相同的内存空间而不是新的内存空间中运行.因此, fork 、 system 等解决方案对我不起作用.我正在查看是否可以将 R 脚本作为单独的线程而不是单独的进程运行.

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.

推荐答案

您可以使用 system() 和 Rscript 将您的脚本作为异步后台进程运行:

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

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

在脚本结束时,您可以使用 save.image() 保存对象以便稍后加载它们,并使用 cat():

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

")

希望这会有所帮助!

这篇关于R - 在后台运行 source()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆