Statistics::R - Controls the R (R-project) interpreter through Perl.


Statistics-R documentation Contained in the Statistics-R distribution.

Index


Code Index:

NAME

Top

Statistics::R - Controls the R (R-project) interpreter through Perl.

DESCRIPTION

Top

This will permit the control of the the R (R-project) interpreter through Perl in different architectures and OS.

You can for example, start only one instance of the R interpreter and have different Perl process accessing it.

SYNOPSIS

Top

  use Statistics::R;

  my $R = Statistics::R->new();

  $R->startR;

  $R->send(q`postscript("file.ps" , horizontal=FALSE , width=500 , height=500 , pointsize=1)`);
  $R->send(q`plot(c(1, 5, 10), type = "l")`);
  $R->send(q`dev.off()`);
  $R->send(qq`x = 123 \n print(x)`);

  my $ret = $R->read;
  print "\$ret : $ret\n";

  $R->stopR();

NEW

Top

When creating the R bridje object (Statistics::R), you can set some options:

log_dir

The directory where the bridge between R and Perl will be created.

R and Perl need to have read and write access to the directory!

By dafault it will be created at %TMP_DIR%/Statistics-R.

r_bin

The path to the R binary.

By default the path will be searched in the default installation path of R in the OS.

r_dir

The directory of R.

tmp_dir

A temporary directory.

By default the temporary directory of the OS will be used/searched.

METHODS

Top

startR

Start R and the communication bridge.

start_sharedR

Start R or use an already running communication bridge.

stopR

Stop R and the bridge.

restartR

stop() and start() R.

Rbin

Return the path to the R binary (executable).

send ($CMD)

Send some command to be executed inside R. Note that $CMD will be loaded by R with source()

read ($TIMEOUT)

Read the output of R for the last group of commands sent to R by send().

lock

Lock the bridge for your PID.

unlock

Unlock the bridge if your PID have locked it.

is_blocked

Return TRUE if the bridge is blocked for your PID.

In other words, returns TRUE if other process has lock()ed the bridge.

is_started

Return TRUE if the R interpreter is started, or still started.

clean_up

Clean up the enverioment, removing all the objects.

error

Return the last error message.

INSTALL

Top

To install this package you need to install R in your OS first, since Statistics::R need to find R path to work fine.

A standart installation of R on Win32 and Linux will work fine and detected automatically by Statistics::R.

Download page of R: http://cran.r-project.org/banner.shtml

Or go to the R web site: http://www.r-project.org/

EXECUTION FOR MULTIPLE PROCESS

Top

The main pourpose of Statistics::R is to start a single R interpreter that hear multiple Perl process.

Note that to do that R and Perl need to be running with the same user/group level.

To start the Statistics::R bridge you can use the script statistics-r.pl:

  $> statistics-r.pl start

From your script you need to use the start_sharedR() option:

  use Statistics::R;

  my $R = Statistics::R->new();

  $R->start_sharedR;

  $R->send('x = 123');

  exit;

Note that in the example above the method stopR() wasn't called, sine it will close the bridge.

SEE ALSO

Top

* Statistics::R::Bridge
* The R-project web site: http://www.r-project.org/
* Statistics:: modules for Perl: http://search.cpan.org/search?query=Statistics&mode=module

AUTHOR

Top

Graciliano M. P. <gm@virtuasites.com.br>

MAINTAINER

Top

Brian Cassidy <bricas@cpan.org>

COPYRIGHT & LICENSE

Top


Statistics-R documentation Contained in the Statistics-R distribution.

package Statistics::R;

use strict;
use warnings;

use Statistics::R::Bridge;

our $VERSION = '0.08';

my( $this, @ERROR );

sub new {
    my $class = shift;

    if( !defined $this ) {
        $this  = bless( {}, $class );
        $this->R( @_ );

        return unless $this->{ BRIDGE };
    }

    return $this;
}

sub R {
    my $this = shift;
    $this->{ BRIDGE } = Statistics::R::Bridge->new( @_ );
}

sub error {
    my $this = shift;

    if( @_ ) {
        my $e = shift;
        push @ERROR, $e;
        warn $e;
    }

    splice( @ERROR, 0, ( $#ERROR - 10 ) ) if @ERROR > 10;

    return @ERROR if wantarray;
    return $ERROR[ -1 ];
}

sub startR {
    my $this = shift;
    delete $this->{ BRIDGE }->{ START_SHARED };
    $this->{ BRIDGE }->start;
}

sub start_sharedR {
    shift->{ BRIDGE }->start_shared;
}

sub stopR {
    my $this = shift;
    delete $this->{ BRIDGE }->{ START_SHARED };
    $this->{ BRIDGE }->stop;
}

sub restartR {
    shift->{ BRIDGE }->restart;
}

sub Rbin {
    shift->{ BRIDGE }->bin;
}

sub lock {
    my $this = shift;
    $this->{ BRIDGE }->lock( @_ );
}

sub unlock {
    my $this = shift;
    shift->{ BRIDGE }->unlock( @_ );
}

sub is_blocked {
    my $this = shift;
    $this->{ BRIDGE }->is_blocked( @_ );
}

sub is_started {
    my $this = shift;
    $this->{ BRIDGE }->is_started;
}

sub send {
    my $this = shift;
    $this->{ BRIDGE }->send( @_ );
}

sub read {
    my $this = shift;
    $this->{ BRIDGE }->read( @_ );
}

sub clean_up {
    shift->send( 'rm(list = ls(all = TRUE))' );
}

1;

__END__