rstudio - 是否可以在后台运行代码 [英] rstudio - is it possible to run a code in the background

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

问题描述

关于 RStudio 的问题.假设我在控制台中运行代码:

<代码>>代码 1()

假设 code1() 在控制台上没有打印任何内容,但是上面的 code1() 需要一个小时才能完成.我想在等待 code1() 的同时处理其他事情.是否可以?是否有像 runInBackground 这样的函数,我可以按如下方式使用

<代码>>runInBackground(code1())>代码2()

替代方案是运行两个 RStudios 或编写一个使用 Rscript 运行 code1() 的批处理文件,但我想知道是否有更简单的方法无需离开 RStudio 控制台即可完成.我试图浏览 R 的帮助文档,但没有想出任何东西(或者可能是我没有使用正确的关键字).

解决方案

未来包(我是作者)提供了这个:

图书馆(未来")计划(多会话)未来(代码1())代码2()

仅供参考,如果您使用

plan(clu​​ster, workers = c("n1", "n3", "remote.server.org"))

然后在其中一台机器上解析未来表达式.使用

plan(future.BatchJobs::batchjobs_slurm)

将导致它通过 Slurm 作业调度程序队列解决.

这个问题与在R中运行异步函数>

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

解决方案

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

这篇关于rstudio - 是否可以在后台运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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