Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Calling R in perl

by frozenwithjoy (Curate)
on Dec 16, 2012 at 01:12 UTC ( #1009032=note: print w/ replies, xml ) Need Help??


in reply to Calling R in perl

I regularly use Statistics::R and it works great. You can put all of your R commands within your Perl script, or if you have a lot of R stuff to run, you can use it to specify variables and then run an entire external R script. The examples on the linked page are clear and helpful. Take a look at them and if you have any more questions, let me know.

EDIT: Here is a snippet from one of my Perl scripts which demonstrates setting variables, running R commands from within Perl, and running R scripts from within Perl. The only thing it is really missing is an example of retrieving a value from an R variable (e.g., $value_returned_from_R = $R->get('some.r.variable') ):

my $R = Statistics::R->new(); $R->set( 'filenames', \@filenames ); $R->set( 'id', $id ); $R->set( 'par1', $par1 ); $R->set( 'par2', $par2 ); $R->run_from_file( "genoplot_by_id.build_df.R" ); $R->run_from_file( "genoplot_by_id.build_plot.R" ); $R->run( qq`setwd("$plot_dir")` ); $R->run( qq`ggsave( filename = paste("$plot_path", "$plot_format", sep = "."), plot = geno.plot, width = $plot_width, height = $plot_height)` );

EDIT #2: A couple more examples... Using an array of R commands or a heredoc with multiple R commands (example taken from Statistics::R):

# Array of R commands: my $out1 = $R->run( q`a <- 2`, q`b <- 5`, q`c <- a * b`, q`print("ok")` ); # Here-doc with multiple R commands: my $cmds = <<EOF; a <- 2 b <- 5 c <- a * b print('ok') EOF my $out2 = $R->run($cmds);


Comment on Re: Calling R in perl
Select or Download Code
Re^2: Calling R in perl
by blackzero (Acolyte) on Dec 16, 2012 at 12:52 UTC

    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.

      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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2014-08-29 15:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (281 votes), past polls