[R] commandArgs usage and --args invokation

Dirk Eddelbuettel edd at debian.org
Sun Apr 1 17:13:46 CEST 2007


Ivo,

For R 'shell scripts', esp. on Unixy system (incl OS X), our littler frontend
can be of help. See

    http://dirk.eddelbuettel.com/code/littler.html

or

    http://biostat.mc.vanderbilt.edu/LittleR

as well as the SVN archive

    svn checkout http://littler.googlecode.com/svn/trunk/ littler

Using littler scripts, you can then do things like

    edd at basebud:~/src/debian/Misc/svn/littler> head -20 examples/pace.r
    #!/usr/bin/env r
    #
    # a simple example to convert miles and times into a pace
    # where the convention is that we write e.g. 37 min 15 secs
    # as 37.15 -- so a call 'pace.r 4.5 37.15' yields a pace of
    # 8.1667, ie 8 mins 16.67 secs per mile
    
    if (is.null(argv) | length(argv)!=2) {
    
      cat("Usage: pace.r miles time\n")
      q()
    
    }
    
    dig <- 5
    
    rundist <- as.numeric(argv[1])
    runtime <- as.numeric(argv[2])

    [...]

which, while not as direct as getopt in bash or C, allows you to pick
arguments off the command-line. E.g.

    edd at basebud:~> pace.r 3 25
    Miles   :  3
    Time    :  25
    Pace    :  8 min 20 sec
    edd at basebud:~>


I use littler as a command-line frontend to most of my (batch) R work for
simulation or recurring data analysis.

As Prof Ripley noted. R 2.5.0 will give you something similar via Rscript.

Hth, Dirk




-- 
Hell, there are no rules here - we're trying to accomplish something. 
                                                  -- Thomas A. Edison



More information about the R-help mailing list