[R] invoking R scripts from a linux shell ?

vfasciani@micron.com vfasciani at micron.com
Wed Jan 21 15:53:34 CET 2004


Ciao Enrico,

I think you can skip to use a shell as you described.
There are some ways to include R script in a perl script.
I included below two examples I wrote for better understand.


###### prova1.pl #######

#!/usr/local/bin/perl
open (FILE, ">test.txt");
print FILE "a,b,c,d,e\n1,2,3,4,5";
close FILE;

####### Start R code ####
open (R_FH, "|/usr/local/bin/R --no-save --slave") or die "$!";

print R_FH qq{
data<-read.csv("test.txt")
datamean<-mean(as.numeric(as.character(data[1,])), na.rm=TRUE)
write(datamean, file="out.txt")
quit(save='no',status=0)
};

close R_FH;
##### end R ####

open(FILE, "<out.txt");
while (<FILE>){
$mean= $_;
}
close FILE;

print "Mean= ".$mean;



or

######## prova2.pl ######

#!/usr/local/bin/perl
open (FILE, ">test.txt");
print FILE "a,b,c,d,e\n1,2,3,4,5";
close FILE;

####### Start R code ####
system("/usr/local/bin/R -q --vanilla < rscript.r > debug.out");
##### end R ####

open(FILE, "<out.txt");
while (<FILE>){
$mean= $_;
}
close FILE;

print "Mean= ".$mean;

... in this way you can call the perl script only.

Regards,
Vittorio



-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Enrico Curiotto
Sent: Friday, January 16, 2004 12:02 AM
To: r-help at stat.math.ethz.ch
Subject: [R] invoking R scripts from a linux shell ?


Hello,

I have written perl programs that extract data from a
text file, process them, and create other text files,
which I'd like to apply some statistics too (for
example with R).

I'd like to do it all in once , with a single script.
I'm not familiar with R, I'd like to know if this task
could be accomplished by creating a linux shells that
launches the perl scripts and then "R functions" that
maybe pass back some results to the system like in
this schema:

S ---> Perl
H <-----
E ---> R functions
L <-----
L

Is it possible ?
Where can I get information to do that? (to call R
from a shell, in background)

Are other better way to do that?

Thank you very much!

Enrico.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list