Passing parameters to a R file (.r file)

While dealing with a R script, i came across a issue where i needed to send parameters to a R script and use inside my script. With perl its very easy to access the parameters, however in R its a bit tricky.

Okay lets discuss how we handle parameters in both perl and R.

In Perl, you can send parameters like:

On command line do:

~$ perl filename.pl argument1 argument2 ...

in perl script you can use these arguments like
$ARGV[0] for argument1, $ARGV[1] for argument2 and so on....

This is how we do in R:

On command line do:

~$ R --vanilla argument1 argument2 < filename.r

In R script you handle parameters this way:

cat("-- reading arguments\n", sep = "")
Args <- commandArgs();

x <- Args[3]
y <- Args[4]

Args[0], Args[1] and Args[2] have some environment variables.

Display variables:

cat (x,"\n",y,"\n",sep="")

Run R script inside perl with arguments:

system("R --vanilla argument1 argument1 < filename.r");
Tags: , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*