我一直在使用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输入命令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)
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!
在您的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)
您需要在参数之前放置参数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)
| 归档时间: |
|
| 查看次数: |
112838 次 |
| 最近记录: |