Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R function to parse command line arguments

Tags:

r

I have the following function below, which I am using to parse command line arguments, so that I can run R scripts from the command line:

 parseArguments <- function() {
     text1 <- commandArgs(TRUE)
     eval(parse(text=gsub("\\s", ";", gsub("--","", text1))))
     args <- list()
     for( ar in ls()[! ls() %in% c("text1", "args")] ) {args[ar] <- get(ar)}
     return (args)
 }

Here is a CLI session output when I attempted to call an R script that uses the above function to parse CL arguments, using following command line arguments:

./myscript.R --param1='XLIF' --param2='ESX' --param3=5650.0 --param4=5499.2 --param5=0.0027397260274 --param6='Jul' --riskfreerate=0.817284313119 --datafile='/path/to/some/datafile.csv' --imagedir='/path/to/images' --param7=2012 --param8=2
Error in parse(text = gsub("\\s", ";", gsub("--", "", text1))) : 
  8:10: unexpected '/'
7: riskfreerate=0.817284313119
8: datafile=/
            ^
Calls: parseArguments -> eval -> parse
Execution halted

Help?

[[Update]]

I have followed Dirk's advice and installed the optparse library. My code now looks like this:

library(optparse)

# Get the parameters
option_list <- list(
  make_option(c("-m", "--param1"), action="store_false"),
  make_option(c("-t", "--param2"), action="store_false"),
  make_option(c("-a", "--param3"), action="store_false"),
  make_option(c("-s", "--param4"), action="store_false"),
  make_option(c("-x", "--param5"), action="store_false"),
  make_option(c("-o", "--param6"), action="store_false"),
  make_option(c("-y", "--param7"), action="store_false"),  
  make_option(c("-r", "--riskfreerate"), action="store_false"),
  make_option(c("-c", "--param8"), action="store_false"),
  make_option(c("-d", "--datafile"), action="store_false"),
  make_option(c("-i", "--imagedir"), action="store_false")  
)

# get command line options, i
opt <- parse_args(OptionParser(option_list=option_list))

When I run the R script passing it the same command line parameters, I get:

Loading required package: methods
Loading required package: getopt
Error in getopt(spec = spec, opt = args) : 
  long flag "param1" accepts no arguments
Calls: parse_args -> getopt
Execution halted

???

like image 336
Homunculus Reticulli Avatar asked Jul 25 '12 12:07

Homunculus Reticulli


2 Answers

Yes, there are CRAN packages getopt and optparse just for this.

like image 199
Dirk Eddelbuettel Avatar answered Oct 28 '22 03:10

Dirk Eddelbuettel


I am answering your second question, about the error you encounter with optparse:

From the make_option help page (...):

action: A character string that describes the action optparse should take when it encounters an option, either “store”, “store_true”, or “store_false”. The default is "store” which signifies that optparse should store the specified following value if the option is found on the command string. “store_true” stores TRUE if the option is found and “store_false” stores FALSE if the option is found.

In short, you need to use action = "store" (the default) if you want to run something like:

./myscript.R --param1='XLIF' --param2='ESX' [...]
like image 39
flodel Avatar answered Oct 28 '22 03:10

flodel