rkeshvardoost has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,

I am trying to use R within perl with the following code :

use R; #Allows use of R references called between Perl and R use RReferences; package R; package R::startR; use lib 'R'; use Exporter 'import'; @EXPORT_OK = qw(startR AUTOLOAD); &R::startR("--silent"); R::call("pdf");

but I keep getting this error:

Undefined subroutine &R::startR called at ...

Here is a part of my R.pm code:

package R; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); use Carp; require Exporter; require DynaLoader; require AutoLoader; @ISA = qw(Exporter DynaLoader); # Items to export into callers namespace by default. Note: do not expo +rt # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. @EXPORT = qw(startR AUTOLOAD ); $VERSION = '0.01'; bootstrap R $VERSION; # Preloaded methods go here. sub startR { # If R_HOME is not in the environment table, put in the value from + # the installation probes. initR(@_); return(1); } # This used to be a native routine. This is now here to maintain the A +PI # but we slide in code to set the R_HOME variable if need be. sub initR { if(0) { croak("no support for R embedded within Perl"); } setRHome(); initRSession(@_);

Replies are listed 'Best First'.
Re: Undefined subroutine error while using R within Perl
by kcott (Archbishop) on Jan 13, 2016 at 21:51 UTC

    G'day rkeshvardoost,

    You have some questionable code here. Consider these two adjacent lines:

    package R; package R::startR;

    Are you missing some code that should come after package R;? If not, why did you use package R; in the first place?

    The use lib 'R'; line seems to be in the wrong place in your code. Without knowing your directory structure, I can't even say if it's correct (regardless of its position).

    In this part of your code:

    package R::startR; ... @EXPORT_OK = qw(startR ...

    that refers to R::startR::startR. Was that what you meant? If so, you haven't declared it!

    At this point, I'm starting to have to make many guesses as to your actual intent. The following is how I might have written something that may be close to what you want.

    Firstly, I created a directory (pm_1152700_libs) and populated it like this:

    $ cat pm_1152700_libs/R.pm package R; use Exporter 'import'; our @EXPORT_OK = qw{startR}; sub startR { print 'This is &R::startR' } 1;
    $ cat pm_1152700_libs/R/startR.pm package R::startR; use Exporter 'import'; our @EXPORT_OK = qw{startR}; sub startR { print 'This is &R::startR::startR' } 1;

    I then wrote this test script (but do see the notes I've written afterwards):

    #!/usr/bin/env perl -l use strict; use warnings; use lib 'pm_1152700_libs'; use R qw{startR}; use R::startR qw{startR}; R::startR(); R::startR::startR();

    which outputs

    This is &R::startR This is &R::startR::startR

    You've used startR in both module and subroutine names. This is highly error-prone and I'd recommend you think of better names.

    I've repeated the use of startR in both module and subroutine names; however, note the following:

    • To disambiguate the calls to the two &startR routines, I've needed to use fully qualified names, i.e. R::startR() and R::startR::startR().
    • If you are going to use fully qualified names, there's little point in adding those identical symbols to your import lists (or even to @EXPORT_OK).
    • With better names, the last few lines of my test script could look more like this:
      use R qw{some_startR}; use R::startR qw{other_startR}; some_startR(); other_startR();

    Finally, I strongly advise that you use the strict and warnings pragmata at the start of all your scripts.

    — Ken

      Thank you for your response Ken.

      To clarify: Since the original code had this error, I tried (almost blindly) anything I could find online to address the error. The original code is the following:

      sub pick_wavelets { `touch $SU_DIR/$file_1.picks`; `rm $SU_DIR/$file_1.picks`; `sugain agc=1 wagc=0.01 < $SU_DIR/$file_1.su | suxwigb mpicks=$SU_ +DIR/$file_1.picks`; } BEGIN { unshift @INC, "/usr/local/perl5.16.0/lib/site_perl/5.16.0/Statistics +/"; unshift @INC, "/usr/local/perl5.16.0/lib/site_perl/5.16.0/"; }; use R; sub setup_R { use lib "/usr/lib64/R/"; use lib "/usr/local/src/RSPerl/src/"; use R; use RReferences; &R::startR("--silent");

      I also tried to define the subroutine by replacing the last part of the code with the following

      sub setup_R { use lib "/usr/lib64/R/"; use lib "/usr/local/src/RSPerl/src/"; use R; use RReferences; use R qw{startR}; R::startR(); &R::startR("--silent");

      However, it still has the same problem. The reason I have not used strict at the beginning is that it makes my program to have more errors. Thank you very much again,

      Ru

Re: Undefined subroutine error while using R within Perl
by Mr. Muskrat (Canon) on Jan 14, 2016 at 16:04 UTC

    I assume that you downloaded RSPerl from here https://github.com/sboehringer/RSPerl since it has been updated to work with modern versions of Perl.

    I have never installed or even tried to install RSPerl but given your problems I thought you might not have seen the documentation. The (old) RSPerl main page has some information on how to install it and use it from Perl. The Calling R From Perl page is probably what you need to read though. It has more detailed information on installation, run-time configuration and usage from within Perl. After reading this, it sounds you didn't run the RSPerl.csh or RSPerl.bsh script (as appropriate) that sets up your environment to find RSPerl.

    Oh! I almost forgot. Be sure to read the FAQ because it tells you how to install the modules alongside the rest of the Perl modules instead of in a separate location which should make things easier for you in the long run.