Home » R » The joys of Rscript and parsing commandArgs

The joys of Rscript and parsing commandArgs

I’ve had a hacky, but workable, setup using a mixture of m4 and R CMD BATCH to run jobs on our local grid varying the parameters each time. Today I switched to Rscript, and it’s been a breeze.

At the top of my .R file I put

#!/usr/bin/Rscript
args <- getArgs()

and I put the getArgs() function below in my ~/.Rprofile.

Then I can run the script using

./myfile.R --args myarg=something

and in the .R file refer to args$myargs. –args is used to separate arguments you want to pass to R from those you want to be available in your script.

Defaults

I can set default options by doing

args <- getArgs(defaults=list(myarg1=default1))

then args$myarg1 will be “default1” unless it gets explicitly set when Rscript is run.

Logical flags

Sometimes I just want to set a logical flag. Something like

./myfile.R --args myarg=something myflag

will result in

> str(args)
List of 3
 $ file : chr "myfile.R"
 $ myarg : chr "something"
 $ myflag: logi TRUE

In such cases, it is probably useful to supply a default FALSE value in the script.

Notes

Note that if using Rscript, the name of the .R file is itself a an argument, named “file”, so do not pass –file=blah to avoid confusion.

Note also that myargs is a list of objects of class character. If you explicitly want a numeric, use as.numeric() etc.

getArgs() function

See this version for one with a little more functionality.

##' commandArgs parsing
##' 
##' return a named list of command line arguments
##'
##' Usage:
##' call the R script thus
##'   ./myfile.R --args myarg=something
##' or
##'   R CMD BATCH --args myarg=something myfile.R
##'
##' Then in R do
##'   myargs <- getArgs()
##' and myargs will be a named list
##' > str(myargs)
##' List of 2
##' $ file : chr "myfile.R"
##' $ myarg: chr "something"
##'
##' @title getArgs
##' @param verbose print verbage to screen 
##' @param defaults a named list of defaults, optional
##' @return a named list
##' @author Chris Wallace
getArgs <- function(verbose=FALSE, defaults=NULL) {
  myargs <- gsub("^--","",commandArgs(TRUE))
  setopts <- !grepl("=",myargs)
  if(any(setopts))
    myargs[setopts] <- paste(myargs[setopts],"=notset",sep="")
  myargs.list <- strsplit(myargs,"=")
  myargs <- lapply(myargs.list,"[[",2 )
  names(myargs) <- lapply(myargs.list, "[[", 1)

  ## logicals
  if(any(setopts))
    myargs[setopts] <- TRUE

  ## defaults
  if(!is.null(defaults)) {
    defs.needed <- setdiff(names(defaults), names(myargs))
    if(length(defs.needed)) {
      myargs[ defs.needed ] <- defaults[ defs.needed ]
    }
  }

  ## verbage
  if(verbose) {
    cat("read",length(myargs),"named args:\n")
    print(myargs)
  }
  myargs
}

Advertisements

One thought on “The joys of Rscript and parsing commandArgs

  1. Pingback: Command Line Arguments in R Scripts | Applied Math Bytes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s