Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^3: Calling R in perl

by frozenwithjoy (Priest)
on Dec 16, 2012 at 18:27 UTC ( [id://1009084]=note: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re^4: Calling R in perl
by blackzero (Acolyte) on Dec 16, 2012 at 19:26 UTC

    No. Can't run the code you posted.

    It returns a simmilar answer:

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

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

    Can't locate object method "run" via package "Statistics::R" at ./ 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.).

        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"


        I tried to install it by hand using the commands "make 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

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-06-16 22:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.