[Rd] RSPerl...

Duncan Temple Lang duncan@research.bell-labs.com
Thu, 2 Nov 2000 08:53:31 -0500 (EST)

Well, I hadn't got around to announcing it, but seeing as it is
mentioned here....

 There is a first round of a package named RSPerl available

which provides several .C like functions but to Perl.  So one can call
Perl subroutines and object methods directly from R. Also, one can
evaluate Perl scripts in the form of strings or files.  Aside from
having access to all of the Perl modules, a benefit of this approach
is that the Perl interpreter lives in the R process.  We avoid the
expense of spawning a Perl process each call via the system()
function. But more importantly, we can use Perl in the form of an
interactive session as we do R, and so values created in one call are
available to future calls rather than disappearing along with the Perl
sub-process. So one can execute Perl commands incrementally.

At the moment, I have only tested this using the latest development
version of R (i.e. the snapshot of the unreleased 1.2 available from

Now, for Tony's question. 

Playing with strings is one way, but not one
I recommend. How can one sensibly serialize an e.g. lm object in a way
that Perl could understand it without having extra code on the other
side. Instead of serializing the contents of an object, the general
mechanism used in the Java and CORBA modules is to leave the R object
in R and send a reference to it over to Perl. This makes sense for
Perl methods that expect an object which have methods. So, Perl might
call residuals() on its argument. If this were an R reference to an lm
object, the Perl value would have a method residuals() which would
call the corresponding R function with the lm object as its primary

As I say, this is identical to the way we export Perl objects to R and
also how the exchange of references is done in the CORBA and Java
packages. However, in the latter two, this can be done automatically
(via reflectance).  I am still trying to develop a general, automated
way of doing this in Perl and that is the last piece.  Any suggestions
from those who know Perl better than I do (almost everyone who has
ever used it :-)) is very welcome.

Basically what would happen is a call of the form
      .Perl("perlfunctionhackery", matrix(rnorm(100),10,10)), .sigs="Matrix")
would generate a new Perl class named RMatrixReference which has the
same methods as the Perl class Matrix (read and generated by
reflectance). We would then create an instance of this class by
passing it an identifier for  matrix(...) in the argument, and passing
the matrix itself.
One method in this new class might be dim().  The RMatrixReference
object would implement this as
  R::callReference(idForMatrix, "dim")
and this ends up calling 

The basic philosophy is compute on the object in the language in which
it is defined, make calls to those methods from any language.

As the package stands now, it is primarily a one-directional
communication mechanism (like the simple version of the .C
(i.e. without the call_R())).  That provides quite a lot of
functionality, and without any real coding effort by other users.

Hope that helps.


> From: rossini@blindglobe.net (A.J. Rossini)
> Date: 02 Nov 2000 05:22:48 -0800
> Lines: 36
> User-Agent: T-gnus/6.14.5 (based on Gnus v5.8.7) (revision 03) EMY/1.13.8 (Tastes differ) FLIM/1.13.2 (Kasanui) APEL/10.2 MULE XEmacs/21.1 (patch 12) (Channel Islands) (i386-debian-linux)
> Sender: owner-r-devel@stat.math.ethz.ch
> Precedence: bulk
> Duncan -
> (but sent to R-devel, for any other thoughts?)
> How do you envision RSPerl being used?  Without having seen the
> details, I can think of playing with strings; is there a preferred
> incantation for stringification of R objects (serialization)?
> (actually, the main problem I'm having is that I know how I want to code
> something like: 
>      Robject <- .Perl("perlfunctionhackery", "stringifedRObjectToHack")
> which might be something like:
>      Robject <- eval(.Perl("perlfunctionhackery","stringifedRObjectToHack"))
> but sort of what I'm having problems with thinking about is along the
> lines of  
>      Robject <- eval(.Perl("perlfunctionhackery",
>                            RToString(InitialRObject)))
> and my brain isn't kicking into gear).   While I know how to transfer
> data as arrays, and the docs suggest I can transfer tables, and that's
> fine, I'm wondering how to transfer and contort objects, and if the
> above might be a reasonable general sketch, or if there might be
> another approach?
> best,
> -tony
> -- 
> A.J. Rossini				Rsrch. Asst. Prof. of Biostatistics
> BlindGlobe Networks (home/default)	rossini@blindglobe.net	
> UW Biostat/Center for AIDS Research	rossini@u.washington.edu	
> FHCRC/SCHARP/HIV Vaccine Trials Net	rossini@scharp.org
> FHCRC: M/Tu: 206-667-7025 (fax=4812) | Voicemail is pretty sketchy
> CFAR:   W/F: 206-731-3647 (fax=3694) | Email is far better than phone
> UW:    Th/F: 206-543-1044 (fax=3286) | Change last 4 digits of phone for fax
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-devel 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-devel-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


Duncan Temple Lang                duncan@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       

      "Languages shape the way we think, and determine what 
       we can think about."        
                                      Benjamin Whorf
r-devel 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-devel-request@stat.math.ethz.ch