Statistics::R - Perl interface to control the R statistical program
This module controls the R interpreter (R project for statistical computing: http://www.r-project.org/). Multiple architectures and OS are supported and a single instance of R can be accessed by several Perl processes.
use Statistics::R; # Create a communication bridge with R and start R my $R = Statistics::R->new(); # Run simple R commands $R->run(q`postscript("file.ps" , horizontal=FALSE , width=500 , height=500 , pointsize=1)`); $R->run(q`plot(c(1, 5, 10), type = "l")`); $R->run(q`dev.off()`); # Pass and retrieve data my $input_value = 1; $R->set('x', $input_value); $R->run(q`y <- x^2`); my $output_value = $R->get('y'); print "y = $output_value\n"; $R->stop();
Create a Statistics::R bridge object between Perl and R and start() R. Available options are:
Start a shared bridge. See start().
A directory where temporary files necessary for the bridge between R and Perl will be stored.
R and Perl need to have read and write access to the directory!
By default this will be a folder called Statistics-R and placed in a temporary directory of the system
The path to the R binary. See INSTALLATION.
Set the value of an R variable, e.g.
$R->set( 'x', "apple" );
or
$R->set( 'y', [1, 2, 3] );
Get the value of an R variable, e.g.
my $x = $R->get( 'x' ); # $y is an scalar
my $y = $R->get( 'y' ); # $x is an arrayref
Start R and set the communication bridge between Perl and R. Pass the option shared => 1 to use an already running bridge.
Stop R and stop the bridge.
stop() and start() R.
Return the path to the R binary (executable).
Send some command to be executed inside R. Note that $CMD will be loaded by R with source(). Prefer the run() command.
Get the output of R for the last group of commands sent to R by send(). Prefer the run() command.
Lock the bridge for your PID.
Unlock the bridge if your PID have locked it.
Return TRUE if the bridge is locked for your PID.
In other words, returns TRUE if other process has lock()ed the bridge.
Return TRUE if the R interpreter is started, or still started.
Clean up the environment, removing all the objects.
To install this package you need to install R on your system first, since Statistics::R need to find R path to work. If R is in your PATH environment variable, then it should be available from a terminal and be detected automatically by Statistics::R. This means that you do not have to do anything on Linux systems to get Statistics::R working. On Windows systems, in addition to the folders described in PATH, the usual suspects will be checked for the presence of the R binary, e.g. C:\Program Files\R. Your last recourse if Statistics::R does not find R is to specify its full path when calling new():
my $R = Statistics::R->new( r_bin => $fullpath );
Download page of R: http://cran.r-project.org/banner.shtml
Or go to the R web site: http://www.r-project.org/
You also need to have the following CPAN Perl modules installed:
The main purpose of Statistics::R is to start a single R interpreter that listens to multiple Perl processes.
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() option in shared mode:
use Statistics::R; my $R = Statistics::R->new(); $R->start( shared => 1 ); $R->run('x = 123'); exit;
Note that in the example above the method stop() wasn't called, since it will close the bridge.
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
Graciliano M. P. <gm@virtuasites.com.br>
Brian Cassidy <bricas@cpan.org>
Florent Angly <florent.angly@gmail.com>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
All complex software has bugs lurking in it, and this program is no exception. If you find a bug, please report it on the CPAN Tracker of Statistics::R: http://rt.cpan.org/Dist/Display.html?Name=Statistics-R
Bug reports, suggestions and patches are welcome. The Statistics::R code is developed on Github (http://github.com/bricas/statistics-r) and is under Git revision control. To get the latest revision, run:
git clone git@github.com:bricas/statistics-r.git
To install Statistics::R, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Statistics::R
CPAN shell
perl -MCPAN -e shell install Statistics::R
For more information on module installation, please visit the detailed CPAN module installation guide.