in reply to Re: Passing parameters to R script
in thread Passing parameters to R script

Thank you all for your input! I did eventually work it out and my solution looks like so:

# test.R # Parse and use command line arguments # Invoke % R --slave --args 100 < test.R Args <- commandArgs(); # retrieve args x <- c(1:as.real(Args[4])); # get the 4th argument y <- c(x^2); # work out square png(filename="image.png"); # create image file plot(x,y); # plot image
This creates the image file image.png which contains a plot of an exponential curve =)

The variable Args contains the following:

[1] "/usr/lib/R/bin/exec/R" [2] "--slave" [3] "--args" [4] "100"
We only require the 4th element for our graph. You can add more arguments and just pick them off accordingly =)

I hope someone else finds this useful.


Smoothie, smoothie, hundre prosent naturlig!

Replies are listed 'Best First'.
Re^3: Passing parameters to R script
by Anonymous Monk on Apr 22, 2009 at 14:37 UTC
    Yes, this was very useful and worked right away! Thanks a lot!
Re^3: Passing parameters to R script
by Anonymous Monk on Jun 08, 2010 at 09:39 UTC
    That works fine, but there's a slightly neater way to do it; if you provide a TRUE value to the commandArgs function, it ignores any commandline parameters before (and including) --args.
    So with Args <- commandArgs(TRUE), Args[1] is "100", and is the only element of the vector.

    It's also possible to use a simpler syntax to call the script on the command line, which implicitly assumes that any arguments are given after the script name:
    Rscript test.R 100

    Cheers,
    Julio
      In Bash shell
      # a=1 # b=10 # Rscript -e "args<-commandArgs(TRUE);x=args[1]:args[2];x;mean(x);sd(x +)" $a $b [1] 1 2 3 4 5 6 7 8 9 10 [1] 5.5 [1] 3.027650

      So you can control variables on the command line R script and you can feed these variables into R itself using commandArgs(TRUE), and manipulate the variables as args1 and args2. So if I imagine if you are doing a system call in perl, you can also construct a similar Rscript on the command line, and get the output accordingly, which you can manipulate further within perl (or any other script).

      Here is simple function to parse unix way of parsing argument from R.

      # # # EXAMPLE: # R --test=mydata.csv #> getopt('test') #[1] "mydata.csv" # getting numeric argument # R --N=123 #> getopt('N',numeric=T) #[1] 123 # getopt <- function(argname, numeric = F) { args <- commandArgs() argexp = sprintf('^--%s=',argname) idx <- regexpr(argexp, args); ret <- NA if ( any(idx > 0) ) { # found matching argument found <- args[idx > 0] value <- strsplit(found,"=") ret <- unlist(value)[2] } if ( numeric ) ret <- as.numeric(ret) ret }

      cheers, c.okugami

Re^3: Passing parameters to R script
by Anonymous Monk on Feb 01, 2012 at 19:25 UTC
    In Rscript/R
    require(plyr) ## get and parse any commandline options/args cmd.args <- commandArgs(trailingOnly=TRUE) ## this assumes trailingOnly in commandArgs() mk.cmd.args.list <- function(args) { if (length(args)>1) { split.list <- strsplit( args[-1], '\\=' ) ## exclude "--args" values <- llply( split.list, '[[', 2 ) names(values) <- laply( split.list, '[[', 1 ) values } else list() ## the possible returns } cmd.args.list <- mk.cmd.args.list(cmd.args)