teaLeef
teaLeef

Reputation: 1989

Executing R script from shell

I am trying to call an R script from a shell.

The script looks like this:

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])

my file1 contains rows of numbers like:

1 2 1 2 4
2 4 5 4 2
1 2 3 4 5

However, calling this script from the shell:

Rscript scriptName fileName

I get the following error:

Error in args[1] : object of type 'closure' is not subsettable
Calls: processGroups -> read.table -> file
Execution halted

Can anyone explain what this error message means?

Upvotes: 1

Views: 374

Answers (1)

gagolews
gagolews

Reputation: 13046

Try functionName(commandArgs(trailingOnly = TRUE)[1]). args object defined in functionName is not "visible" outside the function. Here, it points to the args() builtin function, see ?args.

Upvotes: 3

Related Questions