如何从R脚本读取命令行参数? [英] How can I read command line parameters from an R script?
问题描述
我有一个R脚本,我希望能够提供几个命令行参数(而不是代码本身的硬编码参数值)。该脚本在Windows上运行。
我找不到有关如何将命令行上提供的参数读入我的R脚本的信息。如果不能完成,我会感到惊讶,所以也许我只是没有在我的Google搜索中使用最好的关键字...
任何指针或建议?
Dirk的回答是你需要的一切。这是一个最小的可重现的例子。
我做了两个文件: exmpl.bat
和 exmpl .R
。
-
exmpl.bat
:设置R_Script =C:\Program Files\R- 3.0.2\bin\RScript。 exe
%R_Script%exmpl.R 2010-01-28 example 100> exmpl.batch 2>& 1
或者,使用
Rterm.exe
:设置R_TERM =C:\Program Files\R-3.0.2\ bin \i386\Rterm.exe
%R_TERM%--no-restore --no-save --args 2010-01-28 example 100< exmpl.R> exmpl.batch 2>& 1
-
exmpl.R
:选项(echo = TRUE)#如果你想看到输出文件中的命令
pre>
args < - commandArgs(trailingOnly = TRUE)
print(args)
#trailingOnly = TRUE表示只返回参数,检查:
#print(commandsArgs(trailingOnly = FALSE)
start_date< - as.Date(args [1])$ b $ b name< - args [2]
n< - as.integer(args [3])
rm(args)
#一些计算:
x < - rnorm(n)
png(paste(name,。png,sep =) )
plot(start_date +(1L:n),x)
dev.off()
summary(x)
将这两个文件保存在同一目录中并启动 exmpl.bat
。在结果中,您将得到:
-
example.png
/ li>
-
exmpl.batch
。
您可以使用laso添加环境变量%R_Script%
:
C:\Program Files\R-3.0.2\bin\RScript.exe
并在批处理脚本中使用%R_Script%.......
RScript
与之间的差异
:
-
有更简单的语法
-
Rscript
x64(请参阅 R安装和管理,2.6 Sub -architectures 了解详情) -
Rscript
需要选项(echo = TRUE)$
如果您想将命令写入输出文件,
I've got a R script for which I'd like to be able to supply several command-line parameters (rather than hardcode parameter values in the code itself). The script runs on Windows.
I can't find info on how to read parameters supplied on the command-line into my R script. I'd be surprised if it can't be done, so maybe I'm just not using the best keywords in my Google search...
Any pointers or recommendations?
Dirk's answer here is everything you need. Here's a minimal reproducible example.
I made two files: exmpl.bat
and exmpl.R
.
exmpl.bat
:set R_Script="C:\Program Files\R-3.0.2\bin\RScript.exe" %R_Script% exmpl.R 2010-01-28 example 100 > exmpl.batch 2>&1
Alternatively, using
Rterm.exe
:set R_TERM="C:\Program Files\R-3.0.2\bin\i386\Rterm.exe" %R_TERM% --no-restore --no-save --args 2010-01-28 example 100 < exmpl.R > exmpl.batch 2>&1
exmpl.R
:options(echo=TRUE) # if you want see commands in output file args <- commandArgs(trailingOnly = TRUE) print(args) # trailingOnly=TRUE means that only your arguments are returned, check: # print(commandsArgs(trailingOnly=FALSE)) start_date <- as.Date(args[1]) name <- args[2] n <- as.integer(args[3]) rm(args) # Some computations: x <- rnorm(n) png(paste(name,".png",sep="")) plot(start_date+(1L:n), x) dev.off() summary(x)
Save both files in the same directory and start exmpl.bat
. In the result you'll get:
example.png
with some plotexmpl.batch
with all that was done
You could laso add an environment variable %R_Script%
:
"C:\Program Files\R-3.0.2\bin\RScript.exe"
and use it in your batch scripts as %R_Script% .......
Differences between RScript
and Rterm
:
Rscript
has simpler syntaxRscript
automatically chooses architecture on x64 (see R Installation and Administration, 2.6 Sub-architectures for details)Rscript
needsoptions(echo=TRUE)
in the .R file if you want write the commands to the output file
这篇关于如何从R脚本读取命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!