Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Calling R in perl

by blackzero (Acolyte)
on Dec 17, 2012 at 13:24 UTC ( #1009164=note: print w/ replies, xml ) Need Help??


in reply to Calling R in perl

OK. Statistics::R seens to be working fine now

I just ignores the error message in the test and went ahead with the installation, just as was told me in here: http://www.perlmonks.org/?node_id=1009156

But still I have two problems (that I hope are the last ones).

First of all, when I try to pass my table from perl to R with the comand "$R -> set( 'table', \@lines );", it returns this error:

Problem running this R command: g1 <- table[, 3]; Got the error: wrong number of dimmensions

I scaped that creating an output file from perl (named "data_freq.txt.out" and telling R to import from that. But this doesnt seens the right way to do it.

Second. When I try to return a table from R to perl, importing it to an array (in "my @results1 = $R->get('g1');"), I would like all the values in the table to be passed to this array (@results1). So I could shift and pop values from that array to use in other perl things (Mostly to create an nice output file)

Instead, what I get is an array with a single element that is some kind of octodecimal value (I guess)

How can I fix that?

Someone already told me to use "$results1" instead of "@results1". But that gives me the same output. Besides, it will create an scalar and that is not what I want

NOTE: "my $media = $R->get('meang1');" do work! I guess thats is because its a single value.

HERE are my codes, wich I call with "./perl-R4.pl data_freq.txt"

perl-R4.pl

#!/usr/bin/perl use strict; use warnings; use Statistics::R; my @lines; my $line; my $csv_check = shift @ARGV; #Mark for csv check----- @ARGV = $csv_check; while ( defined( $line = <> ) ) { push @lines, $line; } shift @lines; save_array_output(@lines, ".out"); print "\@lines = \n @lines "; my $R = Statistics::R->new() ; $R->startR ; $R->run('library(genetics);'); $R->run('setwd("/home/user/tests/");'); #$R->set( 'table', \@lines ); $R->run('table <- read.table("/home/user/tests/data_freq.txt.out");'); $R->run('g1 <- table[, 3];'); $R->run('meang1 <- mean(g1)'); $R->run('print(meang1);'); $R->run('write.table(g1, "g1.txt");'); $R->run('write.table(meang1, "meang1.txt");'); my $media = $R->get('meang1'); my $results1 = $R->get('g1'); print "\$results1 = ", $results1, "\n"; print "\$media = ", $media, "\n"; $R->stopR() ; #----------------------------------------------- sub save_array_output { @lines = @_; my $mid_name = pop @lines; my $output_name = $csv_check.$mid_name; open(MYOUTFILE, ">:utf8", $output_name); # Open output file print MYOUTFILE "@lines \n"; close(MYOUTFILE); # Close output file }

data_freq.txt

Locus Alleles frequencies Bet01 230 0.166666666666666657415 Bet01 238 0.500000000000000000000 Bet01 244 0.333333333333333314830 Bet05 101 0.285714285714285698425 Bet05 103 0.500000000000000000000 Bet05 105 0.142857142857142849213 Bet05 109 0.071428571428571424606 Bet06 138 0.055555555555555552472 Bet06 140 0.111111111111111104943 Bet06 146 0.833333333333333370341 Bet12 108 0.111111111111111104943 Bet12 110 0.055555555555555552472 Bet12 112 0.444444444444444419773 Bet12 114 0.277777777777777790114 Bet12 116 0.055555555555555552472 Bet12 118 0.055555555555555552472


Comment on Re: Calling R in perl
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009164]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-28 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls