NAME

R::YapRI::Interpreter.pm A module to transform perl variables into R command lines to define simple objs.

SYNOPSIS

use R::YapRI::Base;
use R::YapRI::Interpreter::Perl qw/r_var/;

my $perl_var = [1, 2, 3];
my $r_var = r_var($perl_var);  

DESCRIPTION

A interpreter to translate Perl variables into R commands.

 +==================+==============+===============================+
 |  PERL VARIABLE   |  R VARIABLE  | Example                       |
 +==================+==============+===============+===============+
 | undef            | NULL         | $px = undef   | rx <- NULL    |
 +------------------+--------------+---------------+---------------+
 | empty ('' or "") | NA           | $px = ''      | rx <- NA      |
 +------------------+--------------+---------------+---------------+
 | integer          | numeric      | $px = 12      | rx <- 12      |
 +------------------+--------------+---------------+---------------+
 | bigint,bigfloat  | numeric      | $px = '-1.2'  | rx <- -1.2    |
 +------------------+--------------+---------------+---------------+
 | word 'TRUE'      | TRUE         | $px = 'TRUE'  | rx <- TRUE    |
 +------------------+--------------+---------------+---------------+
 | word 'FALSE'     | FALSE        | $px = 'FALSE' | rx <- FALSE   |
 +------------------+--------------+---------------+---------------+
 | any other word   | character    | $px = "sun"   | rx <- "sun"   |
 +------------------+--------------+---------------+---------------+
 | ARRAY REF.       | vector       | $px = [1, 2]  | rx <- c(1, 2) |
 +------------------+--------------+---------------+---------------+
 | HASH REF.        | object       | see below (*)                 |
 +------------------+--------------+-------------------------------+
       
   * R object or R function without arguments

     $px = { a => undef }, will be just 'a'  
     $px = { mass => '' }, will be just 'mass'

   * R simple object with arguments

     $px = { '' => { x => 2 }}, will be 'x = 2'
     $px = { '' => { x => [2, 4] }}, will be 'x = c(2, 4)

   * R functions with arguments 

     $px = { log  => 2  }, will be 'log(2)'
     $px = { log  => [2, { base => 10 }] }, will be 'log(2, base = 10 )'
     $px = { t    => {x => ''} }, will be 't(x)'
     $px = { plot => [{ x => ''}, { main => "TEST"} ]}, will be:
             plot(x, main = "TEST")

     Use array ref. to order the arguments in a function.
     Use hash ref keys to define an argument in an R function 

     For more complex data structures, use R::YapRI::Data::Matrix.
    

AUTHOR

Aureliano Bombarely <ab782@cornell.edu>

CLASS METHODS

The following class methods are implemented:

_rvar_noref

Usage: my $r_string = _r_var_noref($perl_var); 

Desc: Internal function to parse a single non-reference perl variable
      (scalar). Equivalence table:
      
      +==================+==============+=============================+
      |  PERL VARIABLE   |  R VARIABLE  | Example                     |
      +==================+==============+===============+=============+
      | undef            | NULL         | $px = undef   | rx <- NULL  |
      +------------------+--------------+---------------+-------------+
      | empty ('' or "") | NA           | $px = ''      | rx <- NA    |
      +------------------+--------------+---------------+-------------+
      | integer          | numeric      | $px = 12      | rx <- 12    |
      +------------------+--------------+---------------+-------------+
      | bigint,bigfloat  | numeric      | $px = '-1.2'  | rx <- -1.2  |
      +------------------+--------------+---------------+-------------+
      | word 'TRUE'      | TRUE         | $px = 'TRUE'  | rx <- TRUE  |
      +------------------+--------------+---------------+-------------+
      | word 'FALSE'     | FALSE        | $px = 'FALSE' | rx <- FALSE |
      +------------------+--------------+---------------+-------------+
      | any other word   | character    | $px = "sun"   | rx <- "sun" |
      +------------------+--------------+---------------+-------------+

Ret: $r_string, a scalar with the perl2R variable translation

Args: $perl_var, could be, a scalar or an array reference

Side_Effects: Die if is used a perl reference.

Example: my $rvar = _rvar_noref(12);

_rvar_vector

Usage: my $r_arg = _rvar_vector($arrayref); 

Desc: Internal function to convert an perl array into a R vector

Ret: $r_arg, a scalar with the perl2R variable translation

Args: $arrayref, with the argument list

Side_Effects: Die if the argument is not an arrayref.

Example: my $r_vector = _rvar_vector($arrayref);

_rvar_arg

Usage: my $r_arg = _rvar_arg($hashref); 

Desc: Internal function to convert an argument in a function in the following
      way:
       2                              ===> '2'
       'YES'                          ===> '"YES"'
       [2, 3]                         ===> 'c(2, 3)'
       { x      => undef }            ===> 'x'
       { type   => "p"   }            ===> 'type = "p"'
       { col    => ["blue", "green"]} ===> 'col = c("blue", "green")'
       { labels => { x => undef } }   ===> 'labels = x'

      Something different from that, will die.

Ret: $r_arg, a scalar with the perl2R variable translation

Args: $hashref, with the argument list

Side_Effects: Die if the argument is not: scalar, array ref or a hash 
              reference.

Example: my $arg = _rvar_arg({ type => "p" });

r_var

Usage: my $r_string = r_var($perl_var); 

Desc: Parse a perl variable and return a string with the r variable format, 
      For perl-non reference variables, see _rvar_noref

      +==================+=================+==============================+
      |  PERL VARIABLE   |  R VARIABLE     | Example                      |
      +==================+=================+==============+===============+
      | ARRAY REF.       | vector          | $px = [1, 2] | rx <- c(1, 2) |
      +------------------+-----------------+--------------+---------------+
      | HASH REF.        | object/function | see below                    |
      +------------------+-----------------+------------------------------+
      
      * R object or R function without arguments

      $px = { a => undef }, will be just 'a'  
      $px = { mass => '' }, will be just 'mass'

      * R simple object with arguments

      $px = { '' => { x => 2 }}, will be 'x = 2'
      $px = { '' => { x => [2, 4] }}, will be 'x = c(2, 4)

      * R functions with arguments 

      $px = { log  => 2  }, will be 'log(2)'
      $px = { log  => [2, { base => 10 }] }, will be 'log(2, base = 10 )'
      $px = { t    => {x => ''} }, will be 't(x)'
      $px = { plot => [{ x => ''}, { main => "TEST"} ]}, will be:
              plot(x, main = "TEST")

      Use array ref. to order the arguments in a function.
      Use hash ref keys to define an argument in an R function      


Ret: $r_string, a scalar with the perl2R variable translation

Args: $perl_var, could be, a scalar or an array reference

Side_Effects: Die if the reference used is not a ARRAY REF or HASH REF.

Example: my $rvar = r_var([1, 2, 3, "TRUE", "last word"]);