Daisuke Maki > R-Writer > R::Writer

Download:
R-Writer-0.00001.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.00001   Source  

NAME ^

R::Writer - Generate R Scripts From Perl

SYNOPSIS ^

  use R::Writer;

  {
    # x <- 1;
    # y <- x + 1;
    # cat(y);

    my $R = R::Writer->new();
    $R->var(x => 1);
    $R->var(y => 'x + 1');
    $R->call('cat' => $R->expr('a * x ^ 2 + 1') );

    print $R->as_string;
    # or save to a file
    $R->save('file');
  }

DISCLAIMER ^

** THIS SOFTWARE IS IN ALPHA ** Patches, comments, and contributions are very much welcome. I'm not really a statistics guy. I just happen to write Perl code to do it.

I'm sure there are bunch of bugs lurking, but I'd like this module to be useful, so please let me know if there are problems or missing features.

DESCRIPTION ^

R::Writer is a tool to generate R scripts for the "R" Statistical Computing Tool from within Perl.

It is intended to be a builder tool -- for example, you have a lot of data in your database, and you want to feed it to R -- and not necessarily a "sexy" interface to build R scripts like JavaScript::Writer.

Each call constitutes a statement. Unlike JavaScript::Writer (from which this module was originally based off), you should not be using call chaining to chain statement calls.

EXAAMPLE ^

DECLARING A VARIABLE

If you simply want to declare a variable and set the value to a particular value, you can use the var() method:

  my $value = 1;
  $R->var(x => $value);

This will yield to 'x <- 1;'.

If you want to assign result of an arithmetic expression, you need to specify the actual string:

  $R->var( y => 'x + 1' );

This will yield to 'y <- x+ 1;'

You can assign the result of a function call this way:

  $R->var( y => $R->call('func', 100, 100) );

Which will yield to 'y <- func(100, 100);'

CALLING ARBITRARY FUNCTIONS

To call functions, you can use the call() method:

  $R->call( demo => 'plotmath' );

Which will yield to 'demo("plotmath");'.

You can of course use call() to feed the result of a function call to a function call to a... You get the idea:

  $R->call( func1 => $R->call( func2 => $R->call( func3 => 3 ) ) );

Which will yield to 'func1(func2(func3(3)));'

The call() method can cover most function use cases, including oft-used functions such as c() and expr(). For convenience, the following methods are provided as shortcust to equivalent call() invocations:

expression

c

SPECIFYING A RANGE

R allows you to specify a number range. This is achieved via range() function:

  $R->var(x => $R->c( $R->range(0, 9) ));

Which will yield to 'x <- c(0:9);'

METHODS ^

new()

Creates a new instance of R::Writer

R()

Shortcut for the constructor call.

  use R::Writer qw(R);
  my $R = R();

call($funcname [, $arg1, $arg2, ...])

Calls a function with specified arguments

var($name [, $value])

Declares a variable.

range($start, $end)

Creates a range of values

reset()

Resets the accumulated R code, and resets R::Writer state.

as_string()

Returns the string representation of accumulated R statements in the given R::Writer instance.

save($filename | $fh)

Saves the result of calling as_string() to the specified filename, or a handle.

TODO ^

Missing Features

Need way to declare functions, execute loops. Probably need way to handle datasets.

Remove JavaScript-ness

JSON and what not are probably not needed.

Document Way To Feed The Script To "R"

AUTHOR ^

Copyright (c) 2008 Daisuke Maki <daisuke@endeworks.jp>

A lot of the concepts and some code is based on JavaScript::Writer, which is by Kang-min Liu <gugod@gugod.org>

LICENSE ^

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

See http://www.perl.com/perl/misc/Artistic.html

syntax highlighting: