[R] calling R from Perl

Dirk Eddelbuettel edd at debian.org
Wed Aug 18 23:47:41 CEST 2004


On Wed, Aug 18, 2004 at 03:44:04PM -0400, Sean Davis wrote:
> Clive,
> 
> Have a look at Statistics::R 
> (http://search.cpan.org/~gmpassos/Statistics-R-0.02).  I'm not sure if 
> it works well with Windows, but it is the only other option that I know 
> of to work directly with the R-interpreter.  However, you can always 
> create a batch file and write it to a file and then call R.

Or you can pipe to R from Perl, and collect the results from the same
(two-way) pipe.  Tedious to write, but reliable once setup.  Should work on
Windoze too.

For a concrete example, here is a part of something I did last month:

my $pid = open2(*READ_R, *WRITE_R, 'R', '--slave', '--silent');
print WRITE_R "
stopifnot(require(reposTools, quiet=TRUE))
[... more R commands ...]
q('no')
";
while ($line = <READ_R>) {
  chomp $line;
  print "[$line]\n" if $debug;
  [... more Perl commands ...]  
}
close(WRITE_R);
close(READ_R);


You need to be careful about quoting: I used outer "" and inner '' for
strings which works. You also need to escape the $ used to access data.frame
elements, or Perl will try to expand it.

Data transfer to and fro is a bit an issue -- here I simply printed as
csv-style file to stdout, and read it in Perl.  Quick and dirty.

Statistics::R is much closer to this paradigm than to RSPerl.  RSPerl is
promising, but, alas, like so many things on omegahat not quite finished.

Dirk

> 
> Sean
> 
> On Aug 18, 2004, at 3:09 PM, Clive Glover wrote:
> 
> >Hello,
> >
> >I am trying to call R from Perl running on Windows 2000.  I have looked
> >through the previous posts regarding running R from Perl and all have
> >referred to the RSPerl package at Omegahat.  Unfortunately the
> >documentation for this package specifically states that it only works 
> >in
> >Unix at the moment.  Does anyone else have any suggestions about the 
> >best
> >way to do this in the Windows environment?
> >
> >Thanks
> >
> >Clive
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide! 
> >http://www.R-project.org/posting-guide.html
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 

-- 
Those are my principles, and if you don't like them... well, I have others.
                                                -- Groucho Marx




More information about the R-help mailing list