将命令行参数传递给R CMD BATCH

时间:2013-01-05 00:26:56

标签: r rscript

我一直在使用终端中的R CMD BATCH my_script.R来执行R脚本。我现在正处于我想向命令传递一个参数的地步,但是我遇到了一些让它工作的问题。如果我R CMD BATCH my_script.R blabla,则blabla成为输出文件,而不是被解释为正在执行的R脚本可用的参数。

我尝试Rscript my_script.R blabla似乎正确地将blabla作为参数传递,但后来我没有得到my_script.Rout R CMD BATCH输出文件(我想要.Rout文件)。虽然我可以将对Rscript的调用输出重定向到我选择的文件名,但我不会像R CMD BATCH那样在{{1}中获取文件中包含的R输入命令文件。

所以,理想情况下,我正在尝试将参数传递给通过.Rout方法执行的R脚本,但如果有办法可以使用R CMD BATCH方法使其生成可比较的Rscript文件。

6 个答案:

答案 0 :(得分:96)

我的印象是R CMD BATCH有点像残骸。在任何情况下,更新的Rscript可执行文件(在所有平台上都可用)与commandArgs()一起使处理命令行参数变得非常简单。

例如,这是一个小脚本 - 称之为"myScript.R"

## myScript.R
args <- commandArgs(trailingOnly = TRUE)
rnorm(n=as.numeric(args[1]), mean=as.numeric(args[2]))

以下是从命令行调用它的内容

> Rscript myScript.R 5 100
[1]  98.46435 100.04626  99.44937  98.52910 100.78853

修改

不是我推荐它,但是......使用source()sink()的组合,您可以Rscript生成.Rout这样的文件由R CMD BATCH制作。一种方法是创建一个小R脚本 - 调用 RscriptEcho.R - 您可以直接使用Rscript调用它。它可能看起来像这样:

## RscriptEcho.R
args <- commandArgs(TRUE)
srcFile <- args[1]
outFile <- paste0(make.names(date()), ".Rout")
args <- args[-1]

sink(outFile, split = TRUE)
source(srcFile, echo = TRUE)

要执行您的实际脚本,您可以执行以下操作:

Rscript RscriptEcho.R myScript.R 5 100
[1]  98.46435 100.04626  99.44937  98.52910 100.78853

将使用提供的参数执行myScript.R,并将交错的输入,输出和消息汇总到唯一名为.Rout的。

<强> EDIT2:
您可以详细运行Rscript并将详细输出放在文件中。

Rscript --verbose myScript.R 5 100 > myScript.Rout

答案 1 :(得分:23)

在尝试了这里描述的选项后,我在r-bloggers中找到了来自Forester的this post。我认为这是一个干净的选择。

我把他的代码放在这里:

从命令行

$ R CMD BATCH --no-save --no-restore '--args a=1 b=c(2,5,6)' test.R test.out &

<强> Test.R

##First read in the arguments listed at the command line
args=(commandArgs(TRUE))

##args is now a list of character vectors
## First check to see if arguments are passed.
## Then cycle through each element of the list and evaluate the expressions.
if(length(args)==0){
    print("No arguments supplied.")
    ##supply default values
    a = 1
    b = c(1,1,1)
}else{
    for(i in 1:length(args)){
      eval(parse(text=args[[i]]))
    }
}

print(a*2)
print(b*3)

在test.out

> print(a*2)
[1] 2
> print(b*3)
[1]  6 15 18

感谢Forester

答案 2 :(得分:9)

在您的R脚本中,名为test.R

args <- commandArgs(trailingOnly = F)
myargument <- args[length(args)]
myargument <- sub("-","",myargument)
print(myargument)
q(save="no")

从命令行运行:

R CMD BATCH -4 test.R

您的输出文件test.Rout将显示参数4已成功传递给R:

cat test.Rout

> args <- commandArgs(trailingOnly = F)
> myargument <- args[length(args)]
> myargument <- sub("-","",myargument)
> print(myargument)
[1] "4"
> q(save="no")
> proc.time()
user  system elapsed 
0.222   0.022   0.236 

答案 3 :(得分:8)

您需要在my_script.R之前放置参数并在参数上使用-,例如

R CMD BATCH -blabla my_script.R
在这种情况下,

commandArgs()将收到-blabla作为字符串。有关详细信息,请参阅帮助:

$ R CMD BATCH --help
Usage: R CMD BATCH [options] infile [outfile]

Run R non-interactively with input from infile and place output (stdout
and stderr) to another file.  If not given, the name of the output file
is the one of the input file, with a possible '.R' extension stripped,
and '.Rout' appended.

Options:
  -h, --help        print short help message and exit
  -v, --version     print version info and exit
  --no-timing           do not report the timings
  --            end processing of options

Further arguments starting with a '-' are considered as options as long
as '--' was not encountered, and are passed on to the R process, which
by default is started with '--restore --save --no-readline'.
See also help('BATCH') inside R.

答案 4 :(得分:3)

我添加了一个答案,因为我认为一线解决方案总是好的! 在myRscript.R文件的顶部,添加以下行:

eval(parse(text=paste(commandArgs(trailingOnly = TRUE), collapse=";")))

然后提交您的脚本,例如:

R CMD BATCH [options] '--args arguments you want to supply' myRscript.R &

例如:

R CMD BATCH --vanilla '--args N=1 l=list(a=2, b="test") name="aname"' myscript.R &

然后:

> ls()
[1] "N"    "l"    "name"

答案 5 :(得分:0)

这是使用R CMD BATCH处理命令行参数的另一种方法。我的方法构建在an earlier answer here之上,允许您在命令行中指定参数,并在R脚本中提供部分或全部默认值。

这是一个R文件,我将其命名为 test.R

defaults <- list(a=1, b=c(1,1,1)) ## default values of any arguments we might pass

## parse each command arg, loading it into global environment
for (arg in commandArgs(TRUE))
  eval(parse(text=arg))

## if any variable named in defaults doesn't exist, then create it
## with value from defaults
for (nm in names(defaults))
  assign(nm, mget(nm, ifnotfound=list(defaults[[nm]]))[[1]])

print(a)
print(b)

在命令行中,如果我输入

R CMD BATCH --no-save --no-restore '--args a=2 b=c(2,5,6)' test.R

然后在R中我们会a = 2b = c(2,5,6)。但我可以省略b,并添加另一个参数c

R CMD BATCH --no-save --no-restore '--args a=2 c="hello"' test.R

然后在R中,我们会a = 2b = c(1,1,1)(默认值),c = {{1 }}

最后,为方便起见,我们可以将R代码包装在一个函数中,只要我们注意环境:

"hello"