Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Perl-R integration

by daverave (Scribe)
on Sep 25, 2010 at 14:46 UTC ( [id://861964]=perlquestion: print w/replies, xml ) Need Help??

daverave has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for an advice on how to integrate between perl and R (mostly in the direction of querying R or running R-like functions from within Perl).

Too often I find myself writing in Perl wishing I could now switch to R. What I usually end up doing is to write an R script that does what I like, then tell my Perl script to write down the data to a file and call the R script with this input file (simple system). The R scripts parses its input file, does some R magic and writes down the results into a file. The perl script then reads this new file, parses it and continues... not fun at all: a lot of parsing overhead, always need to keep the two in sync when changing anything, etc..

I've done some searching here and ran into some old posts mentioning RSPerl http://www.omegahat.org/RSPerl/index.html but it seems to have been last updated more than 3 years ago. I wonder if this is indeed the right choice, whether there any alternatives, and more generally - what do you commonly do when you need R?

Thanks!

Replies are listed 'Best First'.
Re: Perl-R integration
by lima1 (Curate) on Sep 25, 2010 at 19:21 UTC
    Not exactly an answer to your question, but if I have to do some statistical analyses, I often use Perl just for data preparation. Mostly combining datasets from Spreadsheets and databases etc. Then I read and work with the data in R. I wrote Text::CSV::R for that.

    If you need to switch back and forth between the languages, it's as you said still a PITA.

Re: Perl-R integration
by Khen1950fx (Canon) on Sep 25, 2010 at 16:17 UTC
      I guess the simplest thing I'd like to do is to pass a Perl array to R as a vector. Can I do that using Writer?
        I think that these examples of simple initialization will get you going. I hope they help.

        To start:

        #!/usr/bin/perl use strict; use warnings; use R::Writer; my $R = R::Writer->new(); $R->var(y => $R->expression( 'a * x ^ 2' )); print $R->as_string(), "\n";
        And a little more:
        #!/usr/bin/perl use strict; use warnings; use R::Writer; my $R = R::Writer->new(); $R->var(x => $R->expression( 'a * x ^ 2' )); $R->var(y => $R->expression( 'a * x ^ 2' )); $R->call(c => \("x", "y")); print $R->as_string(), "\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found