【问题标题】:Executing R script from shell从 shell 执行 R 脚本
【发布时间】:2014-05-12 11:53:49
【问题描述】:

我正在尝试从 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

谁能解释这个错误信息是什么意思?

【问题讨论】:

  • 如果您知道“闭包”是 R 中的一种函数(参见 help("closure")),即不是数据对象,则有助于理解此错误消息。

标签: linux r


【解决方案1】:

试试functionName(commandArgs(trailingOnly = TRUE)[1])。在functionName 中定义的args 对象在函数之外是不“可见的”。这里,它指向了args() 内置函数,参见?args。

【讨论】:

    猜你喜欢
    • 2015-12-30
    • 2013-06-13
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    相关资源
    最近更新 更多