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

Perl & R ?

by Jaya (Acolyte)
on Mar 29, 2005 at 05:09 UTC ( #443040=perlquestion: print w/replies, xml ) Need Help??

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

I have written some of my statistic modules in R. It is very fast to produce whole set of graphs i need.

Now I like to call the R script from perl.

what i did is :to invoke R from perl

system("R");
after which i need to run the R script file called "commands.R" from the R prompt. the command from R prompt is

source("commands.R")
does anyone have an idea of how i should call this from my perl program to run in R prompt so that i can completely automate this call.

i tried with

system("source(\"commands.R\"));
but this will not work for it will run this from command prompt, not from R prompt.

Replies are listed 'Best First'.
Re: Perl & R ?
by SciDude (Friar) on Mar 29, 2005 at 05:32 UTC

    I have also found R to be quite powerful, although a bit cryptic at times to use. The qcc package is very useful for generating run charts automatically from captured data.

    Calling R from perl is possible using the R/Splus-Perl Interface. I have not used this interface, and it is dated now but reported to work well.

    Another option is CGI access to R.

    I could not tell from your post if you live in a *nix environment, but your mileage may vary if the above options are attempted while using other forms of operating systems.


    SciDude
    The first dog barks... all other dogs bark at the first dog.
Re: Perl & R ?
by Errto (Vicar) on Mar 29, 2005 at 05:29 UTC
    This is untested since I don't have R, but something like this ought to work:
    open my $proc, "| R" or die "cannot open process R: $!"; autoflush $proc; print $proc "source(\"commands.R\")\n"; close $proc;
    This forks off a subprocess running R and then writes the command to its standard input through a pipe. This assumes that R is able to accept input through a pipe (ie it doesn't presume a terminal), but it will probably work. See perlopentut for more.
      I am working on linux machine and i cannot install the R/Splus-Perl Interface because I need root access for it!!i tried the fork code you gave me. I am getting this error

      Fatal error: you must specify `--save', `--no-save' or `--vanilla' Fatal error: you must specify `--save', `--no-save' or `--vanilla' Fatal error: you must specify `--save', `--no-save' or `--vanilla' Broken pipe
      is this error because of the special shell character > , if so how to solve it

      Thank You

        Just curiously, have you tried:
        system("R commands.r")
Re: Perl & R ?
by tlm (Prior) on Mar 29, 2005 at 10:18 UTC

    Do you know about omegahat's RSPerl? It provides "a bidirectional interface for calling R from Perl and Perl from R."

    the lowliest monk

Re: Perl & R ?
by nerfherder (Monk) on Mar 29, 2005 at 08:14 UTC
    You might be able to use a separate file, say sourcelist.R, to contain the source("commands.R") command, then do the following:

    system("R < sourcelist.R");

    Good luck!
Re: Perl & R ?
by Anonymous Monk on Mar 29, 2005 at 12:27 UTC
      The CPAN search link you provided doesn't seem to locate the module, so here's a link directly to the library.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2021-10-16 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (69 votes). Check out past polls.

    Notices?