Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: Passing parameters to R script

by Anonymous Monk
on Jun 08, 2010 at 09:39 UTC ( [id://843631]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Passing parameters to R script
by Anonymous Monk on Jan 09, 2011 at 14:28 UTC
    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).

Re^4: Passing parameters to R script
by Anonymous Monk on Jan 06, 2011 at 03:53 UTC

    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

Log In?
Username:
Password:

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

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

    No recent polls found