Skip Menu |

This queue is for tickets about the Statistics-R CPAN distribution.

Report information
The Basics
Id: 70361
Status: resolved
Priority: 0/
Queue: Statistics-R

People
Owner: Nobody in particular
Requestors: florent.angly [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.08
Fixed in: (no value)



Subject: Convenient utilities
Hi, I have written some code that makes my life with Statistics::R easier and I thought I'd share (attached). First, I added a run() method that does the following: i/ use send() to send a command to R ii/ use read() to read the output of R iii/ checks that R processed the command successfully or die() iv/ return the R output The other method is value(), which, given an R variable name queries R and return its value (without decoration, column names, ...). This works for scalars, and an obvious extension would be to support R lists and to return Perl arrays. Any chance these methods can be officially added to Statistics::R? Thanks, Florent
Subject: R.pm
package Statistics::R; sub run { my ($self, $cmd) = @_; # Execute command $self->send($cmd); # Read command output my $output = $self->read; # Check for errors if ($output =~ m/^<.*>$/) { die "Error: There was a problem running the R command\n". " $cmd\n". "Because\n". " $output\n"; } return $output; } sub value { # Get the value of a variable through a Statistics::R object my ($self, $varname) = @_; $self->send(qq`print($varname)`); my $string = $self->read; my $value; if ($string eq 'NULL') { $value = undef; } elsif ($string =~ m/^\s*\[\d+\]/) { # String look like: ' [1] 6.4 13.3 4.1 1.3 14.1 10.6 9.9 9.6 15.3 # [16] 5.2 10.9 14.4' my @lines = split /\n/, $string; for (my $i = 0; $i < scalar @lines; $i++) { $lines[$i] =~ s/^\s*\[\d+\] //; } $value = join ' ', @lines; # may need to split array and remove quotes $value =~ s/^"(.*)"$/$1/; } else { my @lines = split /\n/, $string; if (scalar @lines == 2) { # String looks like: ' mean # 10.41111 ' # Extract value from second line $value = $lines[1]; $value =~ s/^\s*(\S+)\s*$/$1/; } else { die "Error: Don't know how to handle this R output\n$string\n"; } } return $value; } 1;
Added to Statistics::R version 0.09