Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Passing parameters to R script

by moklevat (Priest)
on Oct 16, 2007 at 17:55 UTC ( [id://645234]=note: print w/replies, xml ) Need Help??


in reply to Passing parameters to R script

Since you appear to be working under Unix, how about:

##In a file called test_args.R args <- commandArgs() print (args)

which you can run from the shell

cat test_args.R | R --slave --args all your base

which prints

[1] "usr/lib64/R/bin/exec/R" "--slave" [3] "--args" "all" [5] "your" "base"

Replies are listed 'Best First'.
Re^2: Passing parameters to R script
by j1n3l0 (Friar) on Oct 17, 2007 at 19:09 UTC
    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!
      Yes, this was very useful and worked right away! Thanks a lot!
      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

      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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://645234]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found