Monday, February 16, 2015

Perl: Use R commands in Perl



Abstract: How to use R commands in Perl script using the Perl package Statistics::R.


The advantage of Perl as a script language is handling TEXT. Perl is not good at statistical analysis and graphics, which is the goodness of R software. Perl and R combined is a good idea.

Here is the Perl script to show how to pass variables and array into the R environment, and how to retrieve variable, array and data frame from R environment.
#!/usr/bin/perl
use strict;
use warnings;
use Statistics::R;

# Create a communication bridge with R and start R
my $R = Statistics::R->new();

# graphics by R commands
my $output_file = "/home/yuan/mysql_pre/cookbooks/test.png";
$R->run(qq'png("$output_file", width=500, height=500)',
q'plot(1:10, type = "b")',
q'dev.off()',
);

# Pass and retrieve numeric variables
my $value = 1;
$R->set('x', $value);
$R->run(q'y <- x^2');
my $output_value = $R->get('y');
print "Variable = $output_value\n";

# Pass and retrieve array
my @array = qw(1 2 3);
$R->set('arr', \@array);
$R->run(q'y <- arr^2');
my $output_pointer = $R->get('y');
my @output_array=@$output_pointer;
print "Array = @output_array\n";

# retrieve data frame in R into the hash{row}->{col} in Perl
$R->run(q' df <-data.frame(a=c(1,2,3), b=c(4,5,6), c=c(7,8,9)) ',
q' df<-df*10 ',
q' write.csv(df, file= "/home/yuan/mysql_pre/cookbooks/test.df" , quote=T ) ',
);
$output_pointer = $R->get('df');
my @output=@$output_pointer;
my $output_ncol = $R->get('ncol(df)');
my @output_colnames=splice(@output, 0, $output_ncol);

print "Data frame:\n";
my %hash;
while (@output){
my @rows=splice(@output, 0, $output_ncol+1);
my $rowname=shift @rows;
for (my $i=0; $i<@rows; $i++){
my $colname=$output_colnames[$i];
$hash{$rowname}->{$colname}=$rows[$i];
}
print "@rows\n";
}

#stop the communication bridge between Perl and R
$R->stop();

print "ok\n";

Here is the result
Variable = 1
Array = 1 4 9
Data frame:
10 40 70
20 50 80
30 60 90
ok



Writing date: 2012.11.10, 2015.02.16