[R] embedding R?

Duncan Temple Lang duncan at research.bell-labs.com
Thu Feb 15 13:34:20 CET 2001

As Luke once put it "This is a long one; you know what key to press if
you aren't interested...."

Apologies for coming into the R within Perl discussion late. (I have
been away from e-mail for several days.) 

The RSPerl package (http:://www.omegahat.org/RSPerl) is designed to be
symmetric in allowing the embedding in either direction. That is
embedding Perl within an R session or running R within a Perl session.
Either can be "in charge". The embedding R within Perl is less well
documented, to say the least.  I plan to release a version of the
package soon with more information about this and also more user-level
support for specifying converter functions. The very same model that
is used in the Java, CORBA, Python, JavaScript, etc. works for Perl
also. The Python package (of which there will be a new release within
the next day or two) might provide a useful place to see the setup

The approach of using Expect perl works fine. However, using strings
can be very limiting for certain types of functionality and places a
lot of work on each programmer using it. They have to construct the R
command as a string and then send it to be evaluated.  Parsing R
output is very fragile and difficult to do in a flexible way (what if
the page width changes, etc.). It is hard to come up with general and
reusable ways to handle reading the results back in and hence one is
frequently coding context-specific approaches. Very time consuming.
(If one really wanted to do this, one might use XML to represent the R
values being returned and then read them into Perl. That is general
and extensible. Some of us (Vincent Carey, Robert Gentleman and I
currently) are working on this at present.) And perhaps the biggest
drawback to using strings to effect the inter-system interface is that
one end up programming in both languages and that also means debugging
the code for each but not necessarily in the easiest place. 

Basically, the overall problem is that the syntax of the other
language is being used and that breaks encapsulation. Suppose you are
using Perl from within R to read the contents of URLs. Then, you may
decide that Java or Python is better (say it handles firewalls or
SSL). Then you would have to rewrite your R code to generate
expressions in Omegahat or Python.

The approach we have been using in all the Omegahat inter-system
interface packages (the CORBA, Java, ....) is to allow one to call
functions and create objects and call their methods in the other
language. The functions look as if they are S functions. And so, it
fits naturally with the programming model, encapsulating what is
really happening.

Whatever approach one uses is good if it works for the problem at
hand. The important thing is to try to reduce the number of times one
has to hand-craft solutions. 

 I hope this helps explain what we are doing and how the S-Perl
connection is intended to work. 

  All the best.


> Date: Tue, 13 Feb 2001 17:28:31 -0500 (EST)
> From: "John D. Barnett" <jdb at young39.wi.mit.edu>
> cc: r-help at stat.math.ethz.ch

> On Sun, 11 Feb 2001, Aaron J Mackey wrote:
> > ...  What about a program that
> > dynamically loads R itself ...
> Aaron-
> I've been interested in this topic for a while, too, but haven't done much
> with it yet.  I saw some of the replies, pointing to omegahat, the
> directions for embedding R.
> Another shortcut to perl <-> R interactivity is to use the Expect perl
> module (I think expect was originally in tcl), which is basically a fancy
> bi-directional pipe.  You're still faced with using perl to prepare R
> input, and parse R output, but you can do so one call at a time.  Also,
> expect just waits until it sees the next prompt or times out (though the
> time-out can be disabled).  I tried to automatically convert R output into
> perl data structures; this can be messy.
> I'm interested in web application development that utilizes R.  Duncan
> Temple Lang's RSPerl package, embedding perl in R, lets you access great
> perl modules, like CGI.pm; a hack to R lets it handle scripts like
> #!/usr/local/R-1.2.0/lib/R/bin/R.bin --source 
> invisible(library(RSPerl));
> foo <- .PerlExpr("use CGI;");
> foo <- .PerlExpr("$q = new CGI; 1;");
> a <- .PerlExpr("$q->param('a')");
> cat(.PerlExpr("$q->header . $q->start_html . 'Hellooooo....';"),
>     a,
>     .PerlExpr("$q->end_html"));
> Anyway, that's about all I did with it.  (OK, maybe that's exactly what
> I did; proof of concept and no more.)  It has R functions for accessing
> perl data structures, which might serve as a useful template for the
> reverse process-- having perl functions to access R data structures, for R
> embedded in perl.  I'm a little fuzzy on what the appropriate mapping is
> for different data types.
> A random aside-- there's a package for perl, PDL (perl data language),
> that stores data in C arrays, and has optimized code for
> computation.  This also provides methods for converting perl arrays to
> and from pdl structures, as well as fast I/O routines for pdl
> structures.  The icing on the perl-with-embedded-R cake would be
> conversion methods directly between R arrays and pdl arrays.
> I'm interested if you decide to pursue this further.
> -John Barnett
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


Duncan Temple Lang                duncan at research.bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-3217
700 Mountain Avenue, Room 2C-259  fax:    (908)582-3340
Murray Hill, NJ  07974-2070       
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list