pages tagged r programming languageENETDOWNhttp://enetdown.org//tag/r_programming_language/ENETDOWNikiwiki2016-06-13T07:17:08ZA fast and natural interface to R from Perlhttp://enetdown.org//dot-plan/posts/2014/12/24/a_fast_and_natural_interface_to_R_from_Perl/zaki2014-12-24T01:58:39Z2014-12-24T01:43:15Z
<p>Love <a href="http://www.r-project.org/">R</a>? Love <a href="http://www.perl.org/">Perl</a>? Well,
I've got a nice little present for you this <code>echo $(calendar)</code> <a href="http://enetdown.org//tag/r_programming_language/#fn:calendar-unix" id="fnref:calendar-unix" class="footnote">1</a>
season! Now you can pass data in and out of R as easily as</p>
<div class="highlight-perl"><pre class="hl"><span class="hl kwa">use</span> v5<span class="hl opt">.</span>16<span class="hl opt">;</span>
<span class="hl kwa">use</span> Statistics<span class="hl opt">::</span>NiceR<span class="hl opt">;</span>
<span class="hl kwa">use</span> Data<span class="hl opt">::</span>Frame<span class="hl opt">::</span>Rlike<span class="hl opt">;</span>
Moo<span class="hl opt">::</span>Role<span class="hl opt">-></span><span class="hl kwd">apply_roles_to_package</span><span class="hl opt">(</span> q<span class="hl opt">|</span>Data<span class="hl opt">::</span>Frame<span class="hl opt">|,</span> <span class="hl str">qw(Data::Frame::Role::Rlike)</span> <span class="hl opt">);</span>
<span class="hl kwc">my</span> <span class="hl kwb">$r</span> <span class="hl opt">=</span> Statistics<span class="hl opt">::</span>NiceR<span class="hl opt">-></span><span class="hl kwd">new</span><span class="hl opt">;</span>
<span class="hl kwc">my</span> <span class="hl kwb">$iris</span> <span class="hl opt">=</span> <span class="hl kwb">$r</span><span class="hl opt">-></span><span class="hl kwd">get</span><span class="hl opt">(</span><span class="hl str">'iris'</span><span class="hl opt">);</span>
<span class="hl kwc">say</span> <span class="hl str">"Subset of Iris data set"</span><span class="hl opt">;</span>
<span class="hl kwc">say</span> <span class="hl kwb">$iris</span><span class="hl opt">-></span><span class="hl kwd">subset</span><span class="hl opt">(</span> <span class="hl kwa">sub</span> <span class="hl opt">{</span> <span class="hl slc"># like a SQL WHERE clause</span>
<span class="hl opt">(</span> <span class="hl kwb">$_</span><span class="hl opt">->(</span><span class="hl str">'Sepal.Length'</span><span class="hl opt">) ></span> <span class="hl num">6.0</span> <span class="hl opt">)</span>
<span class="hl opt">& (</span> <span class="hl kwb">$_</span><span class="hl opt">->(</span><span class="hl str">'Petal.Width'</span><span class="hl opt">) <</span> <span class="hl num">2</span> <span class="hl opt">)</span>
<span class="hl opt">})-></span><span class="hl kwd">select_rows</span><span class="hl opt">(</span><span class="hl num">0</span><span class="hl opt">,</span> <span class="hl num">34</span><span class="hl opt">);</span> <span class="hl slc"># grab the first and last rows</span>
</pre></div>
<p>which outputs</p>
<div class="highlight-txt"><pre class="hl">Subset of Iris data set
-----------------------------------------------------------------------
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
-----------------------------------------------------------------------
51 7 3.2 4.7 1.4 versicolor
147 6.3 2.5 5 1.9 virginica
-----------------------------------------------------------------------
</pre></div>
<p>This is possible due to Statistics::NiceR and Data::Frame.</p>
<p><a href="http://p3rl.org/Statistics::NiceR">Statistics::NiceR</a> is a C-level binding to
the R interpreter that exposes all of R's functions as if they were Perl
functions. It handles all the magic data conversion in the background so that
you don't have to think about it.</p>
<p><a href="http://p3rl.org/Data::Frame">Data::Frame</a> is a container for
<a href="http://pdl.perl.org/">PDL</a> typed arrays that lets you think in terms of
tabular data just like R's <code>data.frame</code>s. It even prints out a table using
<a href="http://p3rl.org/Text::Table::Tiny">Text::Table::Tiny</a>. To support categorical
data just like R's <code>factor</code> variables, it has a PDL subclass that keeps track
of the levels of the data.</p>
<p>It's still an early release, so there may still be some kinks to figure out,
but give it a try and be sure to ping me if there is something wrong.</p>
<p>Much thanks to the folks in <code>#inline</code> for <a href="http://inline.ouistreet.com/page/inline-grant-weekly-report-8.html">helping out</a>
with very cool <a href="http://p3rl.org/Inline::Module">Inline::Module</a> so that this
code could hit CPAN (ingy++, ether++). You should definitely check it out as an
alternative to writing XS.</p>
<p>There are already several interfaces to R on CPAN, but this is the
first one that embeds R and provides a flexible data conversion mechanism. Hope
you enjoy using it!</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn:calendar-unix"><p><a href="http://www.unix.com/man-page/freebsd/1/calendar/">calendar(1)</a><a href="http://enetdown.org//tag/r_programming_language/#fnref:calendar-unix" class="reversefootnote"> ↩</a></p></li>
</ol>
</div>