简体   繁体   English

perl @array数据到R中

[英]perl @array data into R

a very simple doubt, but I do not know how to manage this. 一个非常简单的疑问,但我不知道如何管理这个。

I want to plot a histogram for all data in 'datos.txt'. 我想为'datos.txt'中的所有数据绘制直方图。

a) by using R: a)使用R:

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

b) How could I invoke R inside Perl to do the same?? b)如何在Perl中调用R来做同样的事情?

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

Thanks!! 谢谢!!

Perl is not a statistics-focused language like R, so charting functions are not likely to be found in the core. Perl不是像R这样的以统计为中心的语言,因此在核心中不太可能找到图表功能。 But with Perl being a general purpose language, it can do anything R can, and you'll often find what you want by searching CPAN . 但是Perl是一种通用语言,它可以做任何事情,你可以通过搜索CPAN找到你想要的东西。 A quick glance yields some promising candidates: 快速浏览产生一些有希望的候选人:

You can also try the perl module 您也可以尝试使用perl模块

Statistics::R . 统计:: R.

This seems to be supported for windows and linux. 这似乎支持Windows和Linux。 I haven't really used it though. 我还没有真正使用它。 Thus, I don't know if it is easy to install (or if the installer pulls in a whole lot of dependencies, or how much manual configuration is required). 因此,我不知道它是否易于安装(或者如果安装程序引入了大量依赖项,或者需要多少手动配置)。

It seems to be pipe-based, and the OS-check for win32-based systems is really simple, so I'd think it works better on linux than on windows. 它似乎是基于管道的,基于win32的系统的OS检查非常简单,所以我认为它在Linux上比在windows上更好。

But the module seems to be actively developed (as of 2012). 但该模块似乎正在积极开发(截至2012年)。 And for your use case, sending a few simple commands from perl to R, it should be worth a look. 对于你的用例,从perl向R发送一些简单的命令,值得一看。

At one point I decided I wanted a really simple command line barplot (easily adaptable to histogram or scatter plot etc) maker that I could stick at the end of a pipeline. 有一次,我觉得我想要一个非常简单的命令行条形图(很容易适应直方图或散点图等)制作者,我可以坚持在管道的末端。 I didn't know a lot of R at the time, nor did I know about littler (it might not have even existed yet) so I would up doing a hacky embedding of R in perl. 我不知道当时有很多的R,也没有我知道的利特勒 (它可能根本不会存在尚未),所以我想了做R的哈克嵌入在Perl。 It works, though. 但它确实有效。 I wouldn't write it like this again since I know a heck of a lot more R now, but it has been useful to me as is. 我不会再这样写了,因为我现在知道更多的R,但它对我来说是有用的。 The only major problem is that since there is no event loop, the program has to be keep alive artificially to keep the window from disappearing. 唯一的主要问题是,由于没有事件循环,程序必须人为地保持活动以防止窗口消失。 You will need the RSPerl package and scripts as explained here http://www.omegahat.org/RSPerl/ 您将需要RSPerl包和脚本,如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)){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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