blackzero has asked for the wisdom of the Perl Monks concerning the following question:
Hi.
I want to write a perl program that calls R software (http://www.r-project.org/), pass it some objects (vector and arrays), use some of the R functions and then return a array to perl.
I saw there is a software to integrate both languages called RSperl (http://www.omegahat.org/RSPerl/RFromPerl.html), but I wondering if there's a way to do that without use any other program than perl and R itself.
Anyone knows how to do that?
If so, please show me an example.
Thanks
Re: Calling R in perl
by frozenwithjoy (Priest) on Dec 16, 2012 at 01:12 UTC
|
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);
| [reply] [d/l] [select] |
|
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. | [reply] [d/l] [select] |
|
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
| [reply] [d/l] [select] |
|
|
|
Re: Calling R in perl
by ww (Archbishop) on Dec 15, 2012 at 23:58 UTC
|
| [reply] |
Re: Calling R in perl
by davido (Cardinal) on Dec 16, 2012 at 01:06 UTC
|
Surprisingly, http://search.cpan.org mostly DWIM when I enter the single search term of "R". I was expecting a lot of unrelated clutter, but it seems to work well in this case.
| [reply] |
Re: Calling R in perl
by CountZero (Bishop) on Dec 16, 2012 at 09:27 UTC
|
I have several Perl scripts that simply write an R-script (hey, it is just text) and then call R through the system command or backticks.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] |
Re: Calling R in perl
by erix (Prior) on Dec 16, 2012 at 13:08 UTC
|
| [reply] |
Re: Calling R in perl
by blackzero (Acolyte) on Dec 17, 2012 at 00:58 UTC
|
Can you help me here?
I never really used CPAN, so it may be a silly question. Sorry. But when I try to use it with the command "install Statistics::R" it return the following error:
Failed during this command: FANGLY/Statistics-R-0.30.tar.gz : make_test NO
Any advices about that?
NOTE: I'm running a Fedora 17 and perl 4:5.14.3-218.fc17, and I ran cpan with "sudo perl -MCPAN -e shell"
EDIT:
I tried to install it by hand using the commands "perl Makefile.PL", "make", "make test", "make install".
But when I rum "make test". It fails and returns this error message:
Test Summary Report
-------------------
t/08-errors.t (Wstat: 256 Tests: 10 Failed: 1)
Failed test: 10
Non-zero exit status: 1
Files=9, Tests=10195, 64 wallclock secs ( 8.93 usr 0.48 sys + 54.43 c
+usr 4.36 csys = 68.20 CPU)
Result: FAIL
Failed 1/9 test programs. 1/10195 subtests failed.
make: ** [test_dynamic] Erro 1
| [reply] [d/l] [select] |
|
| [reply] |
|
| [reply] |
|
Re: Calling R in perl
by blackzero (Acolyte) on Dec 16, 2012 at 15:43 UTC
|
Erix, have you saw the problem I replied to frozenwithjoy answer?
If you know how to get a object from R back to perl with Statistics::R, can you please send me an example?
| [reply] |
Re: Calling R in perl
by blackzero (Acolyte) on Dec 17, 2012 at 13:24 UTC
|
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
| [reply] [d/l] [select] |
Re: Calling R in perl
by blackzero (Acolyte) on Dec 18, 2012 at 13:39 UTC
|
| [reply] |
|
|