[R] commandArgs return value

Ben Tupper ben.bighair at gmail.com
Sun Jan 10 01:18:30 CET 2010


Hello,

I plan to use a script with R CMD BATCH on different platforms Linux,  
Windows and Mac OSX.  I use the commandArgs(trailing = FALSE) function  
to retrieve the name of the script and the arguments (example below  
with output.)  The online docs for "Introduction to R" (sections B.1  
Invoking R from the command line and  B.4 Scripting with R) and  
help("commandArgs") leave my head spinning regarding the contents of  
the arguments when trailing = FALSE.


(1) What should I expect from commandArgs() when used with R CMD BATCH?

The docs for commandArgs() states, "These arguments are captured  
before the standard R command line processing takes place. This means  
that they are the unmodified values."  However, if I call a script  
like this ...

R CMD BATCH --slave --vanilla '--args dogs cats verbose=TRUE' /Users/ 
Shared/code/R/hbb/pam/script.r my.out

... the actual arguments are as shown in the example output below.    
You can see that "--restore --save --no-readline" were picked up which  
seems to contradict the documentation.  In the online docs (http://cran.r-project.org/doc/manuals/R-intro.html#Invoking-R 
) the --vanilla argument is described as a combination of --no-save, -- 
no-environ, --no-site-file, --no-init-file and --no-restore.  So now I  
am wondering if I am confounding the arguments to invoke R with the  
arguments to invoke R via CMD BATCH.

(2) Does the name of the script being called always fall in the third  
element of the commandArgs() value?  If so, does it always include the  
full path?

(3) Are "--arg" and the supplied arguments always last in the  
commandArgs() value?  The docs for commandArgs() states, "This is  
especially useful with the --args command-line flag to R, as all of  
the command line after that flag is skipped," so I feel pretty  
confident that they will be last.  But given my confusion in (1)  
above ....


Thanks,
Ben


###### script begin
args = commandArgs()

cat("All the arguments...\n\n")
cat(args, sep = "\n")
cat(" \n")
cat(paste("nargs=", length(args)), "\n")
cat(" \n")
cat(paste("source is", basename(args[3]), sep = " "), sep = "\n")

ix = grep("--arg", args, fixed = TRUE)
if (ix < length(args)){
    for (i in (ix+1):length(args)) {
     cat(paste("argument #",i-ix,"-->",args[i], sep=" "), sep = "\n")
    }
}

ecode = 0
q(runLast = FALSE, save = "no", status = ecode)
###### script end

######  output file begin
All the arguments...

/Library/Frameworks/R.framework/Resources/bin/exec/i386/R
-f
/Users/Shared/code/R/hbb/pam/script.r
--restore
--save
--no-readline
--slave
--vanilla
--args
dogs
cats
verbose=TRUE

nargs= 12

source is script.r
argument # 1 --> dogs
argument # 2 --> cats
argument # 3 --> verbose=TRUE
#### output file end


##### session info
 > sessionInfo()
R version 2.10.1 (2009-12-14)
i386-apple-darwin9.8.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.10.1
#######



More information about the R-help mailing list