编写代码:启动 R 会话、运行 R 脚本、终止会话、重复 [英] Writing code to: start an R session, run R script, terminate session, repeat

查看:43
本文介绍了编写代码:启动 R 会话、运行 R 脚本、终止会话、重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的设置并忘记它"的方式,无论是作为终端中的单个参数字符串还是一个简单的 Java 程序,来自动执行以下操作:

I am looking for a simple "set it and forget it" way, either as a single string of arguments in terminal or a simple Java program, to automate the following:

1) 启动 R 会话

2) 告诉 R 获取包含冗长并行模拟代码的 .R 文件

2) tell R to source .R files that contain code for lengthy, parallelized simulations

3) 完成后终止 R 会话

3) terminate the R session upon completion

4) 开始一个新的 R 会话

4) start a new R session

5) 告诉 R 获取其他 .R 文件

5) tell R to source other .R files

6) 完成后终止 R 会话

6) terminate the R session upon completion

7) 起泡、冲洗、重复

7) lather, rinse, repeat

我的 .R 脚本总共需要几天才能运行,在此期间我将出城无法检查它们,如果我在同一个会话中运行它们,我将无法避免用尽我的可用内存.

My .R scripts will take a total of several days to run, during which I will be out of town and unable to check on them, and if I run them all in the same session there is no possible way for me to avoid maxing out my available RAM.

谢谢!

我在 Ubuntu 12.04 LTS 上运行 R 2.15.3,内存为 16GB

I am running R 2.15.3 on Ubuntu 12.04 LTS, with 16GB RAM

推荐答案

使用 Rscript 处理启动和终止 R 会话的过程.所以写你的脚本这样称呼它们:

The process of starting and terminating R sessions is handled by using Rscript. So write your scripts call them like this:

Rscript script_1.R
Rscript script_2.R
...
Rscript script_Inf.R

剩下第 2 点和第 5 点......这是一个简单的问题:

That leaves points 2 and 5... which is a simple matter of putting:

source('/home/sc_evans/script_abc.R')

...在任何脚本的开头.

...at the head of whatever script(s).

每个脚本都有自己的 R 会话,该会话将在完成时终止.将这些命令放在批处理脚本中并运行它.

Each script will get its own R session that will terminate upon completion. Put those commands in a batch script and run it.

编辑

如果我自己做这件事,我会忘记使用单独的脚本.只要您正确管理内存,运行单个进程就应该没问题.将您的流程划分为适当的功能:

If I were to do this myself I would forget about using separate scripts though. As long as you're managing memory properly running a single process should work out alright. Divide your processes into appropriate functions:

massive_process_1 <- function() {
  x <- do_something()
  saveRDS(x, '/home/sc_evans/results/first_result.rds')      
}

massive_process_2 <- function() {
  x <- do_something()
  saveRDS(x, '/home/sc_evans/results/second_result.rds')      
}

massive_process_1()
massive_process_2()

等等.在第一个函数完成之前,下一个函数不会运行,并且您的对象应该在函数中死亡,因此您不应该耗尽内存.

And so on. The next function won't run until the first is completed, and your objects should die in the functions so you shouldn't run out of memory.

这篇关于编写代码:启动 R 会话、运行 R 脚本、终止会话、重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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