Perl @array data in R

very simple doubt, but I don't know how to do it.

I want to plot a histogram for all data in 'datos.txt'.

a) using R:

datos<-scan("datos.txt")
pdf("xh.pdf")
hist(datos)
dev.off()

      

b) How can I call R inside Perl to do the same?

#!/usr/bin/perl
open(DAT,"datos.txt");
while (<DAT>) {
 chomp;
 push(@datos,$_);
}
#now I want a histogram of values in @datos

      

Thank!!

+3


source to share


3 answers


Perl is not a statistics-oriented language like R, so chart functions are unlikely to be found in the kernel. But because Perl is a general-purpose language, it can do whatever it can, and you'll often find what you want by choosing CPAN . A quick glance reveals several promising candidates:



+3


source


You can also try the perl module

Statistics of the R :: .

It looks like support for windows and linux. However, I haven't used it. So I don't know if it's easy to install it (or if the installer uses a lot of dependencies or how much manual configuration is required).



It seems to be pipe based and OS checking on win32 based systems is very simple, so I think it works better on Linux than Windows.

But the module seems to be actively developing (since 2012). And for your use case, sending some simple commands from perl to R should be interesting.

+2


source


At some point, I decided that I needed a really simple command line barcode (easily adaptable to a bar or markup chart, etc.) that I could stick to at the end of the pipeline. At the time, I didn't know much R and didn't know about littler (maybe it didn't exist yet), so I would do a hacky nesting of R into perl. It works. I wouldn't write it like that again, as I now know a little more R, but it was useful to me as it is. The only major issue is that, since there is no event loop, the program must be artificially saved to prevent the window from disappearing. You will need the RSPerl package and scripts as described here http://www.omegahat.org/RSPerl/


#!/usr/bin/perl -w
use strict;
use R;
use RReferences;

&R::startR("--no-save", "--silent");

my $header = <>;
chomp $header;
my @header = split(/,/, $header);
my @x;
my @y;

while(<>){
    chomp;
    my @fields = split(/,/);
    push(@x, $fields[0]);
    push(@y, $fields[1]+0);
}

R::callWithNames("barplot", {"height",\@y, "data",\@x, "xlab",$header[0], "ylab",$header[1] });

print "Ctrl-C to exit\n";
while(sleep(60)){}


      

0


source







All Articles