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

Bry*_*mas 94 r rscript

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

我试过Rscript my_script.R blabla这似乎blabla正确地作为一个参数传递,但后来我没有得到我得到的my_script.Rout输出文件R CMD BATCH(我想要的.Rout文件).虽然我可以调用的输出重定向到Rscript我的选择的文件名,我就不会得到列入方式文件中的R输入命令R CMD BATCH是否在.Rout文件中.

所以,理想情况下,我正在采用一种方法将参数传递给通过该R CMD BATCH方法执行的R脚本,但是Rscript如果有一种方法可以使它生成一个类似的.Rout文件,那么会很满意.

Jos*_*ien 103

我的印象是,R CMD BATCH这有点像残骸.在任何情况下,更新的Rscript可执行文件(在所有平台上都可用),以及commandArgs()处理命令行参数非常容易.

举个例子,这是一个小脚本 - 调用它"myScript.R":

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

这是从命令行调用它的样子

> Rscript myScript.R 5 100
[1]  98.46435 100.04626  99.44937  98.52910 100.78853
Run Code Online (Sandbox Code Playgroud)

编辑:

这并不是说我会推荐它,但使用的组合... 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)
Run Code Online (Sandbox Code Playgroud)

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

Rscript RscriptEcho.R myScript.R 5 100
[1]  98.46435 100.04626  99.44937  98.52910 100.78853
Run Code Online (Sandbox Code Playgroud)

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

Edit2:
您可以详细运行Rscript并将详细输出放在文件中.

Rscript --verbose myScript.R 5 100 > myScript.Rout
Run Code Online (Sandbox Code Playgroud)

  • 只是添加我的0.02,它也很容易从终端使用重定向.一个例子是`Rscript myfile.R> path/to/mylog.Rout`而不是打印到stdout(屏幕),文件的输出保存在`.Rout`文件中. (7认同)
  • 但是我认为你可以用`knitr`比`R CMD BATCH'做得更好,例如'Rscript -e"knitr :: stitch(commandArgs(TRUE)[1])"my_script.R`(你可以用` `stitch_rhtml`或`stitch_rmd`,你需要从[Github](https://github.com/yihui/knitr)安装`knitr`,因为我刚发现`stitch`中的一个bug ...) (5认同)
  • 要添加到@JamesPringle,我经常希望我的输出既可以打印在屏幕上(实时监控)也可以打印到文件(稍后再查看).我做`Rscript myfile.R | tee mylog.Rout` (3认同)
  • 我也给人的印象是“ R CMD BATCH”是遗物。不过,我喜欢它的事情是,它会生成一个.Rout文件,该文件不仅包含脚本输出,而且还交错来自产生该输出的.R脚本文件的输入命令/注释。 (2认同)

Ale*_* A. 25

在尝试了这里描述的选项之后,我在r-bloggers中找到了来自Forester的这篇文章.我认为这是一个干净的选择.

我把他的代码放在这里:

从命令行

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

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)
Run Code Online (Sandbox Code Playgroud)

在test.out中

> print(a*2)
[1] 2
> print(b*3)
[1]  6 15 18
Run Code Online (Sandbox Code Playgroud)

感谢Forester!


use*_*570 9

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

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

从命令行运行:

R CMD BATCH -4 test.R
Run Code Online (Sandbox Code Playgroud)

您的输出文件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 
Run Code Online (Sandbox Code Playgroud)


Yih*_*Xie 8

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

R CMD BATCH -blabla my_script.R
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

  • 我注意到如果我这样做并在脚本中使用`args < - commandArgs(FALSE)`然后打印args,我最终得到所有参数,包括那些不是我的参数,比如`--restore`, `--save`等.如果我使用`commandArgs(TRUE)`我根本就没有参数.有没有办法获得我自己的额外参数?`--args`看起来很有希望,但我还没能让它上班...... (2认同)