Statistics::useR - Embed R to let Perl speak statistics.


        use Statistics::useR;


The Statistics::useR is intended to integrate statistical power of R and comprehensive power of Perl. It defines a set of methods that make it possible to communicate easily between Perl and R. Furthermore, it offers a parallel environment for users to do multiple tasks at same time.

Please keep in mind that Statistics::useR is currently just a layer of glue. User is assumed to know basic ideas of R.

Statistics::useR can start an embedded R and evaluate R commands. It also offers an object-based interface to R data. The R data can be output into Perl (integer vector, real number vector and string vector, also lists containing such vectors) as a hash structure which is called RData. The hash contains array refs to data. Keys in hash are names of list in R. For vector data, though, hash with only value will be put back into Perl. It can also introduce a hash into R list data. In one word, useR exchanges data with R through Perl-hash/R-list. Please see details below.

Please open the "readme" file before installation.


Statistics::useR exports five methods to run embedded R.


When this module is used, an R interpreter will be started automatically. It will also close by itself when the program ends. Following codes show how to evaluate R commands and how the RData looks like. It is important to understand the structure of RData. It is strongly suggested that you run the following two example codes.

The first example could demonstrate how RData looks like.

        use Statistics::useR;
        use Data::Dumper;

        my $cmd = 'l<-list(a=1:3,b=2:6,c=list(d=c(2,3.4,5.6),e=c(23.3,43.445),f=c(98.8,42.1,"df")));l';
        my $list = eval_R($cmd);
        print $list->getType(), "\n"; #get data type of cmd result
        print $list->getLen(), "\n"; #get number of members of cmd result
        print join "\t", @{$list->getNames()}; print "\n"; #get names(like 'names' in R)
        print Dumper($list->getValue()), "\n"; #Dumped value will help understand how value is returned to Perl.

        my $data = {'val',[1,4,3],'attr',['hello', 'world','universe']};
        my $rvar = Statistics::RData->new('data'=>$data, 'name'=>'test'); #Set a new R data named 'test' in R.
        $cmd='test2<-test;test2$val <- test2$val+5;pdf("pic.pdf");plot(test2$val);;test2';
        my $res=eval_R($cmd);
        print Dumper($res->getValue()), "\n";

The second example shows how to share data and to run analysis in parallel way.

        use Statistics::useR;

The following assignment creates a shared data.

        my $res = eval_R('a<-rnorm(5600*5600);dim(a)<-c(5600,5600);b<-t(a);colnames(a)<-1:5600;');

Following codes create four tasks to calculate the matrix product of 'b' and 'a'.

        my $th1 = openTask(' %*% a[,1:1400])');
        my $th2 = openTask(' %*% a[,1401:2800])');
        my $th3 = openTask(' %*% a[,2801:4200])');
        my $th4 = openTask(' %*% a[,4201:5600])');

Then close all the tasks. Please note that the tasks are opened together before get closed. Since closeTask may block the main program, closing a task right after opening it may actually equal to doing tasks sequentially.

        my $o1 = closeTask($th1); #closeTask returns a hash ref.
        my $o2 = closeTask($th2);
        my $o3 = closeTask($th3);
        my $o4 = closeTask($th4);
        my %res = (%{$o1},%{$o2},%{$o3},%{$o4}); #collect all returned RData.



Evaluate R expression and return an object of RData. It is one way to share data between different tasks to evaluate an R command before creating any task by openTask.

 eval_R('a <- 1:100;');
 my $task1 = openTask(...);
 my $task2 = openTask(...);
 #task1 and task2 both know data a.

Turn on a process to evaluate R commands. Given a scalar containing command string, a process object will be returned.

 my $cmd = 'm <- rnomr(3*3); dim(m) <- c(3,3); crossprod(m)';
 my $process = openTask($cmd);

Turn off a process. Since it will block the main program, it is good to close tasks only after opening all needed tasks. Return a hash ref pointing to the value of returned RData of the last evaluated expression in the R command.

 my $res = closeTask($process);

Read in a text file containing a table-style data. The data is assumed to have column names, yet without row names.

 my $delim = "\t";
 my $newdata = readTable 'filename.txt', $delim;

Put a RData into a text file in table format.

 writeTable $RData, $filename, $delim;

Following is Object-oriented methods for RData.


Create a new R list - 'data' with 'name'. Return an object of RData. 'data' is a hash ref, which points to hash containing array refs. Created R list takes hash keys as names, and takes every array content as component data. Refer to usage. And this is another way to share data between multiple tasks.


Get R data type.


Get number of members in R data.


Just like 'names' in R.


Return a hash ref which contains array refs and takes names of R list components as hash keys. The arrays map to components of R list.


R project website:


Xin Zheng, <> Any feedback will be greatly appreciated. It may help if you go to .


Copyright (C) 2011 by LIB/SAIC-Frederick @ NCI-Frederick

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.