http://www.perlmonks.org?node_id=902026

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Revered Monks,

I am trying to use Statistics::R to run a simple t-test, but I am having a hard time feeding the data to R from my perl script.

Below is a minimal script. $data is created by the first 100 lines of my perl script, which I removed here.

use strict; use warnings; use Statistics::R; my $data = "Subj Cond Count subj1 high 13 subj1 low 15 subj2 high 12 subj2 low 15 subj3 high 16 subj3 low 17 subj4 high 11 subj4 low 18 "; my $R = Statistics::R->new(); $R->startR; #$R->send(qq`data = scan()\n$data\n\n`); #my $r_output = $R->read; #print $r_output . "\n"; $R->stopR();

Using "scan" does not work. The example in the documentation is too simple, it only contains one number. Also, I do not understand what the qq, q and backticks are in the argument of "send" in the documentation.

Normally in R I would run the following commands, but I would like to avoid writing the data out to a file and reading it back in.

example=read.table("file.txt", header = TRUE) t.test(Count~Cond, data = example) wilcox.test(Count~Cond, data = example)

If per chance someone uses the package, I would be grateful for any help. I am an R beginner so there may be ways of reading in data I do not know about.

Replies are listed 'Best First'.
Re: Feed data to R using Statistics::R
by toolic (Bishop) on Apr 29, 2011 at 16:14 UTC
    Also, I do not understand what the qq, q and backticks are in the argument of "send" in the documentation.
    qq is the double-quote operator in Perl which allows interpolation of variables, such as your $data, and escape characters, such as \n.

    q is the single-quote operator which prevents interpolation.

    The backticks are merely delimiters for q and qq. Do not confuse them with qx. In my opinion, they are a poor choice of delimiters; I would prefer something like curlies instead: qq{}.

    Update: For more help, you could try a Super Search where any text contains all of "statistics::r", "send": ?node_id=3989;BIT=statistics%3A%3Ar%20send

    Also, StackOverflow has a dedicated R tag. You could try getting help there.

Re: Feed data to R using Statistics::R
by ww (Archbishop) on Apr 29, 2011 at 16:16 UTC
    Can't help with R and related modules, but the answers to your your question re q (and qq, qx & qw) can be found at Quote Like Operators

    For information on an arguably "more appropriate use of" backticks, exec, etc, see perldoc -q exec or use perldoc -q backtick from your command line to see a (non-canonical, but very clear) explanation. (Alternately, of course, you can find the same, on-line.)

Re: Feed data to R using Statistics::R
by Khen1950fx (Canon) on Apr 30, 2011 at 08:14 UTC
    I've always wanted to give R a try, so here's my take on it:
    #!/usr/bin/perl use strict; use warnings; use Statistics::R; my(@data) = <DATA>; my $R = Statistics::R->new(); $R->start_sharedR; $R->send(qq`x = @data \n print(x)`); my $r_output = $R->read; print "r_output : $r_output\n"; $R->stopR; __DATA__ "Subj Cond Count subj1 high 13 subj1 low 15 subj2 high 12 subj2 low 15 subj3 high 16 subj3 low 17 subj4 high 11 subj4 low 18"
Re: Feed data to R using Statistics::R
by igelkott (Priest) on Feb 24, 2013 at 00:19 UTC

    As far as I can tell, Statistics::R can only transfer scalars and refs to simple arrays (ie, no AoA, etc).

    One reasonable solution would be to convert your data into a single vector or string that you can reconstitute in R. Besides files, another possibility is to get data from a database (which is what I did since that's where my data was anyway). Maybe Statistics::R could be extended to emulate R's MySQL driver so that a "data.frame" could be transferred to/from Perl. I'll suggest this to the author or might even attempt it myself ... once I've learned R's weird data structures well enough.

    Perhaps obvious but yet another possibility is to skip R completely since it's quite possible that the statistical functions you need might already be available directly.

    Update: This feature has already been requested at CPAN.

Re: Feed data to R using Statistics::R
by pvaldes (Chaplain) on Feb 24, 2013 at 23:36 UTC

    data = scan()$data

    Not, this is not how R works, try something like

    data <- scan(file =$filehandle)

    And you need also avoit to mix the headers with the data

    my $data = "subj1 high 13 subj1 low 15 subj2 high 12 subj2 low 15 subj3 high 16 subj3 low 17 subj4 high 11 subj4 low 18 "; my $R = Statistics::R->new(); $R->startR; R->send(qq`data <- read.table($data, header = false), sep = "")`); R->send(qq`names(data) <- c("Subj","Cond","Count")`); R->send(qq`attach(data)`); R->send(qq`$Cond <- as.factor($Cond)`);