我正在尝试从 shell 调用 R 脚本。
脚本如下所示:
functionName <- function(file1){
args <- commandArgs(trailingOnly = TRUE)
print(args)
data <- read.table(file(file1),row.names=1,header=FALSE,sep='\t',dec='.')}
functionName(args[1])
我的文件 1 包含几行数字,例如:
1 2 1 2 4
2 4 5 4 2
1 2 3 4 5
但是,从 shell 调用这个脚本:
Rscript scriptName fileName
我收到以下错误:
Error in args[1] : object of type 'closure' is not subsettable
Calls: processGroups -> read.table -> file
Execution halted
谁能解释一下这个错误信息是什么意思?
最佳答案
试试 functionName(commandArgs(trailingOnly = TRUE)[1])
。在 functionName
中定义的 args
对象在函数外部不“可见”。在这里,它指向 args()
内置函数,参见 ?args
。
关于linux - 从 shell 执行 R 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23608334/