Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Statistics::R generates blank plot image (jpeg)

Tags:

r

perl

I am currently using ActiveState Perl 5.14 and the R project version 2.13.2. Within Perl I am using Statistics::R version 0.08. According to ActiveState the more recent versions of Statistics::R (through 0.24) failed to pass scrutiny and are therefore not available through the PPM.

History: I have been successfully using Perl to access R for some time to perform analysis. Now I want to generate JPEG images of the results of the analysis for easy visualization.

Here's the problem: I can generate the images successfully from within the R console. However, when I run the same commands through Perl I only get a blank image. My console code includes (simplified, of course):

  x<-c(1,2,3,4,5)
  y<-c(5,4,3,2,1)
  jpeg("C:/temp.jpg")
  plot(x,y)
  dev.off()

And my Perl commands include (also simplified):

   $R = Statistics::R->new();
   $R->start_sharedR
   $R->send("x<-c(1,2,3,4,5)");
   $R->send("y<-c(5,4,3,2,1)");
   $R->send('jpeg("C:/temp.jpg")');
   $R->send("plot(x,y)");
   $R->send("dev.off()");

Any suggestions? I know that there are other plotting options accessible to Perl. I have eliminated some (GD Graph) because X-axis data is not treated as numeric. I'd prefer to keep it in R if at all possible since I'm already interacting in that package for the analysis. Thanks!

like image 336
Ryan Avatar asked Oct 10 '22 02:10

Ryan


1 Answers

Forget Statistics::R. Just use a system call. At least it's what I do!

my $path_to_r = "C:/Program Files/R/bin/Rscript.exe";

my $cmd = "x<-c(1,2,3,4,5);";
$cmd .= "y<-c(5,4,3,2,1);";
$cmd .= 'jpeg("C:/temp.jpg");';
$cmd .= "plot(x,y);";
$cmd .= "dev.off()";

system($path_to_r . " -e '" . $cmd . "'");

If your R script grows up a bit or if it takes input from the parameters, write it in a file and Rscript.exe this file.

like image 67
Calimo Avatar answered Oct 18 '22 14:10

Calimo