Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Calling R in perl

by blackzero (Acolyte)
on Dec 16, 2012 at 12:52 UTC ( #1009059=note: print w/ replies, xml ) Need Help??


in reply to Re: Calling R in perl
in thread Calling R in perl

Ok, thank you very much to all for the replies. Statistics::R seens to be what I want.

I've installed Statistics::R

But I have some issues.

My code is this:

#!/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; } save_array_output(@lines, ".out"); print "\@lines = \n @lines "; my $R = Statistics::R->new() ; $R->startR ; $R -> send('library(genetics);'); $R -> send('table <- read.table("/home/user/my_path/data_freq.txt.out" +, header=T, row.names=2);'); $R -> send('g1 <- table[, 2];'); $R -> send('meang1 <- mean(g1)'); $R -> send('print(meang1);'); $R -> send('write.table(g1, "/home/user/my_path/g1.txt");'); $R -> send('write.table(meang1, "/home/user/my_path/meang1.txt");'); #my @results1 = get('g1'); print "\@results1 = ", @results1, "\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 is this:

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

I run my program with "./perl-R3.pl data_freq.txt". I am able to call R, activate the genetics package, make it run the file data_freq.txt.out, do the calculations, save the results in "g1.txt" and "meang1.txt".

OK.

But I cant get back g1 to perl.

If I uncomment the line "#my @results1 = get('g1');", I get this error:

Undefined subroutine &main::get called at ./perl-R3.pl line 35.

If I try to pass values from perl to R directly with a command like "$R -> set('table', \@lines);", I also receive an error:

Can't locate object method "set" via package "Statistics::R" at ./perl-R3.pl line ##

Whats the problem here?

Also, if I try to set the work directory to R with a command like "$R -> send('setwd("/home/user/my_path/");');", the program hangs.


Comment on Re^2: Calling R in perl
Select or Download Code
Re^3: Calling R in perl
by frozenwithjoy (Curate) on Dec 16, 2012 at 18:27 UTC

    OK, I think a couple changes will fix everything.

    First, the method for running a line of R code is run(), not send(), so change $R->send(...) to $R->run(...). (The method send() still works, but seems to have been deprecated for a long time. I'd avoid its use in case it is ever completely dropped.)

    Second, to retrieve values from R, you need to change my @results1 = get('g1'); to my $results = $R->get('g1'). Notice that there are two changes here: (1) the addition of $R-> and (2) $results is an array reference rather than an array.

    Regarding your problem with $R->set(), can you show a few lines of code where you are using it in context? I don't see any obvious problems with what you wrote there.

    One more thing: you are putting your R commands in single quotes, but the Statistics::R docs recommend quoting with q`...` (if you aren't interpolating Perl variables) or qq`...` (if you are interpolating). Those are backticks, by the way (but, really, any character not used in typical R code should suffice). The reason for this is that if you have single or double quotes in your R commands, you won't run into troubles. Another thing to watch out for is if you are wrapping your R code in double quotes because you need to interpolate Perl variables and are using $ in your R code to specify a column of data, for example, you need to escape it (e.g., dataframe\$sample1).

    EDIT: I'm trying to understand why you are having issues with $R->set(). Are you able to run the following code successfully? Also, I don't think it is causing any errors, but you should remove the spaces between your objects and methods (e.g., change $R -> set(...) to $R->set(...))

    #!/usr/bin/env perl use strict; use warnings; use Statistics::R; my @numbers = ( 1 .. 10 ); my $R = Statistics::R->new(); $R->set( 'x', \@numbers ); $R->run( q`x = x ^ 2` ); my $squares = $R->get('x'); print "@$squares"; __END__ 1 4 9 16 25 36 49 64 81 100

      No. Can't run the code you posted.

      It returns a simmilar answer:

      Can't locate object method "set" via package "Statistics::R" at ./monks_test.pl line 9.

      Also, using 'run' in place of 'send' returns the same error:

      Can't locate object method "run" via package "Statistics::R" at ./monks_test.pl line #.

      May I be running an old version of Statistics::R ?

      Whats the right place to download the latest version? Can you put a link?

        Ya, I think you must have an old version. You should update it via cpan (or cpanm, etc.).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-12-23 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (133 votes), past polls