[Rcpp-devel] R.e. loadRcppModules

Christoph Bergmeir c.bergmeir at decsai.ugr.es
Fri May 27 15:39:12 CEST 2011

Hi everybody,

Just in case anyone is interested: I managed to come up with a solution that works at least for my case, but unfortunately it doesn't solve much of the general problem:

I found a function in my code where I can assume that the objects are in a final state, and which is called for every object. There I added a call to the serialization function.

As I use the $-wrapper mechanism that is documented in one of the Rcpp vignettes, I added there some code that first checks if the external pointer is <nil>, and if this is the case, then the object is re-created from the serialization (if a serialization is present).

If anyone is interested in the implementation, have a look at the next release of RSNNS, that is about to come soon...


On 05/26/2011 12:00 PM, rcpp-devel-request at r-forge.wu-wien.ac.at wrote:
> Send Rcpp-devel mailing list submissions to
> 	rcpp-devel at lists.r-forge.r-project.org
> To subscribe or unsubscribe via the World Wide Web, visit
> 	https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> or, via email, send a message with subject or body 'help' to
> 	rcpp-devel-request at lists.r-forge.r-project.org
> You can reach the person managing the list at
> 	rcpp-devel-owner at lists.r-forge.r-project.org
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Rcpp-devel digest..."
> Today's Topics:
>     1. Re: R.e. loadRcppModules (Christoph Bergmeir)
>     2. Re: R.e. loadRcppModules (Romain Francois)
>     3. Re: R.e. loadRcppModules (baptiste auguie)
> ----------------------------------------------------------------------
> Message: 1
> Date: Wed, 25 May 2011 18:29:20 +0200
> From: Christoph Bergmeir<c.bergmeir at decsai.ugr.es>
> Subject: Re: [Rcpp-devel] R.e. loadRcppModules
> To: rcpp-devel at lists.r-forge.r-project.org
> Message-ID:<4DDD2E60.5080102 at decsai.ugr.es>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> Hi list,
> I remembered that this discussion was going on here about 6 weeks back, and now I have nearly the same problem. Maybe somebody has made any progress:
> Luckily, I have for some of my C++ objects a serialization function ready. So I'd like to make them survive the save/restart/load cycle. The pointer to the C++ object is inside of an S3 object. So the general idea as I understood it so far is:
> - serialize the C++ object, store it in the S3 object
> - do save/restart/load
> - build a new C++ object out of the serialization.
> Some questions arise:
> - How will the S3 class notify that it is going to be saved? I heard about finalizers, but aren't they called also if I just delete the object? And arent they called *after* save?
> - Same for load...This is somewhat more easy because if I get a NULL-pointer I can just try to load the serialization. But still: Can the object be notified that it just was loaded?
> Regards,
> Christoph
> PD: I know that this is more of a general R question, and maybe the solution is somewhere hidden in the R-exts or R-ints documents..however, I didn't find nothing there, and as I stated, I think its exactly the same problem that was discussed here earlier, so I'll ask the question here...
> On 14 April 2011 at 07:49, Douglas Bates wrote:
> | On Thu, Apr 14, 2011 at 12:43 AM, Christian Gunning<xian at unm.edu>  wrote:
> |>  On Tue, Apr 12, 2011 at 11:07 PM, Romain Francois
> |>  <romain at r-enthusiasts.com>  wrote:
> |>>>
> |>>>  Just to clarify -- ?does your plan include storing user-modified
> |>>>  fields and properties in objects? ?E.g. below, modtest with the msg
> |>>>  set?
> |>>
> |>>  yes. Although the way I see it, this will require more work for the
> |>>  developper, who would have to provide a serialization/deserialization couple
> |>>  for his/her classes.
> |>>
> |>>  This is a nice goal for a future version.
> |>
> |>  Am I naive to think that a 1-to-1 mapping between C++ object fields&
> |>  properties and an S4 object might make sense? ?This reduces the module
> |>  developer's problem of serial/deserialization to 1) writing a
> |>  constructor (and perhaps with validator) that takes an S4 object, and
> |>  2) writing a finalizer that populates a specified S4 object in the
> |>  user's environment, tears down the C++ object (as per R Exts manual),
> |>  and let's save() do the rest?
> |>
> |>  It seems that, in theory, there's a canonical mapping between the
> |>  names/types of fields/properties exposed in the RCPP_MODULE call and
> |>  the proposed S4 object. ?The main gotcha I see here is that any
> |>  variable within a C++ object *not* exposed as a field/property must be
> |>  viewed as a const to ensure consistency of the C++ object between
> |>  write and re-construct...
> |>
> |>  If this is off-base, feel free to ignore or point out good reading material :)
> |
> | I don't think it is off-base.  I was planning something very similar
> | to that except that not every field/property would be saved.  I have
> | classes in which many of the properties are calculated on the fly and
> | I don't need to save those.  I just need to save enough information to
> | create the object again.
> |
> | This approach would run into trouble if there were data fields in the
> | C++ object that could not be expressed as R objects.  However, it
> | seems to me that if the C++ object can be generated from R objects in
> | the first place you should be able to serialize enough information to
> | reconstruct it.
> This really is darn close to what RProtoBuf does to fit into (Google's)
> Protocol Buffers "everything to everyone" scheme reportedly used for all
> intermachine (and interlanguage) communication.  But that would of course
> introduce a new non-R dependency so you probably do not want that here.
> So we'd have to reinvent a suitable subset, leaning on R's serialization.
> Too bad we didn't have that insight four weeks ago. This would have made a
> swell project for Google Summer of Code.  I blame Christian ;-)
> Dirk
> -- Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> On 04/14/2011 04:20 PM, rcpp-devel-request at r-forge.wu-wien.ac.at wrote:
>> Re: R.e. loadRcppModules

Christoph Bergmeir
e-mail: c.bergmeir at decsai.ugr.es
Grupo SCI2S, DiCITS Lab          (http://sci2s.ugr.es/DiCITS)
Dpto. de Ciencias de la Computacion e Inteligencia Artificial
E.T.S. Ingenierias de Informatica y Telecomunicacion
Universidad de Granada
18071 - GRANADA (Spain)

More information about the Rcpp-devel mailing list