The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Statistics::NiceR - interface to the R programming language

VERSION

version 0.03

SYNOPSIS

    use Statistics::NiceR;

    my $r = Statistics::NiceR->new();

    say $r->pnorm( [ 0 .. 3 ] )
    # [0.5 0.84134475 0.97724987  0.9986501]

DESCRIPTION

This module provides an interface to the R programming language for statistics by embedding the R interpreter using its C API. This allows direct access to R's functions and allows sending and receiving data efficiently.

CONVERSION

In order to give the module a hassle-free interface, there is a mechanism to convert Perl types[^1] to R types[^2] and vice-versa for the values sent to and from the R interpreter.

Currently, the conversion is handled by the modules under the Statistics::NiceR::DataConvert namespace. It is currently undocumented how to extend this to more types or how to change the default behaviour, but this will be addressed in future versions.

[^1]: Such as strings, numbers, and arrays.

[^2]: Such as integers, numerics, data frames, and matrices.

METHODS

new

    new()

Creates a new instance of a wrapper around the R interpreter.

Example

    use Statistics::NiceR;

    my $r = Statistics::NiceR->new();

eval_parse

    eval_parse( Str $r_code )

A convenience function that allows for evaluating arbitrary R code.

The return value is the last line of the code in $r_code.

Example:

    use Statistics::NiceR;

    my $r = Statistics::NiceR->new();
    my $dataframe = $r->eval_parse( q< iris[1:20,1:4] > );

CALLING R FUNCTIONS

R functions can be called by using the name of the function as a method call. For example, to call the pnorm function (PDF of the normal distribution), which has the R function signature

    pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)

one could run

    use Statistics::NiceR;
    my $r = Statistics::NiceR->new();

    say $r->pnorm( 0 ) # N( μ = 0, σ² = 1) at x = 0
    # 0.5

    say $r->pnorm( 5, 1, 2 ) # N( μ = 1, σ² = 2) at x = 5
    # 0.977249868051821

Since R can have identifiers that contain a period (".") in their name and Perl can not, Statistics::NiceR maps

  • a single underscore in the Perl function name ("_") to a period in the R function name (".")

  • two consecutive underscores in the Perl function name ("__") to a single underscore in the R function name ("_").

So in order to call R's as.Date function, one could run:

    use Statistics::NiceR;
    my $r = Statistics::NiceR->new();

    say $r->as_Date( "02/27/92", "%m/%d/%y" ); # one underscore
    # [1] "1992-02-27"

or to call R's l10n_info function, one could run:

    use Statistics::NiceR;
    my $r = Statistics::NiceR->new();

    say $r->l10n__info(); # two underscores
    # $MBCS
    # [1] TRUE
    #
    # $`UTF-8`
    # [1] TRUE
    #
    # $`Latin-1`
    # [1] FALSE

INSTALLATION

On a system with a package manager, it would be best to install the R base package using the package manager.

Windows

For Windows, both the R base and Rtools are necessary.

SEE ALSO

For developers:

AUTHOR

Zakariyya Mughal <zmughal@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Zakariyya Mughal.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.