R::Writer - Generate R Scripts From Perl
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'); }
** 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.
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.
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);'
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:
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);'
Creates a new instance of R::Writer
Shortcut for the constructor call.
use R::Writer qw(R); my $R = R();
Calls a function with specified arguments
Declares a variable.
Creates a range of values
Resets the accumulated R code, and resets R::Writer state.
Returns the string representation of accumulated R statements in the given R::Writer instance.
Saves the result of calling as_string() to the specified filename, or a handle.
Need way to declare functions, execute loops. Probably need way to handle datasets.
JSON and what not are probably not needed.
Copyright (c) 2008 Daisuke Maki <daisuke@endeworks.jp>
<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>
<gugod@gugod.org>
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
To install R::Writer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm R::Writer
CPAN shell
perl -MCPAN -e shell install R::Writer
For more information on module installation, please visit the detailed CPAN module installation guide.