简体   繁体   中英

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

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!

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.

It works fine for me with Statistics R::0.27, but not with 0.08, the only version I could find in Active perl's package manager. In order to install 0.27, I had to use cpan command line. Make test fails but make install was fine. Bit of a life-saver.

(By the way I'm a relative noob. Using cpan command line was pretty easy however.
Type i /Statistics-R/ from cpan command line, then install FANGLY/Statistics-R-0.27.tar.gz (or whatever the relevant file is. I'm using a windows system so RSPerl annoyingly not an option for me. I note that latest Statistics::R version is dated March 2012 so perhaps some of the previously documented (piping?) problems have been solved. You may also need to install a 'maker'; in my case it was 'dmake', not 'nmake'. Pretty easy, you can get a version of make from M$ website and copy that + .err file into PERL\\bin dir. But help on this is available elsewhere. Hope this helps!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM