我有一个bash脚本,做一些事情,然后调用Rscript。 这里举一个简单的例子来说明:
test.sh:
Rscript test.r
test.r:
args <- commandArgs() print(args)
如何在命令行中使用./test.sh hello
导致R打印hello?
你可以让bash把所有的参数传递给R脚本,使用类似于bash脚本的方式:
#!/bin/bash Rscript /path/to/R/script --args "$*" exit 0
然后你可以选择从$ *中有多少个参数需要在R内部丢弃。
我注意到了解决这个问题的方法是:
test.sh:
Rscript test.r $1
test.r:
args <- commandArgs(TRUE) print(args)
$1
表示传递给bash脚本的第一个参数。
当调用commandArgs()
而不是commandArgs(TRUE)
,它不会从bash传递,而是会打印其他在内部调用的参数。
关于asb的回答:
在bash脚本中有“–args”不起作用,“–args”被当作我想要传入我的R脚本的真实参数的文字。 拿出来的作品,即“Rscript /path/to/my/rfile.R arg1 arg2”
bash版本:GNU bash,版本3.2.25(1)-release(x86_64-redhat-linux-gnu)Rscript版本:R scripting前端版本3.0.1(2013-05-16)