[Rcpp-devel] Save objects across sessions

Dirk Eddelbuettel edd at debian.org
Tue May 6 13:55:01 CEST 2014

On 6 May 2014 at 09:41, soeren.vogel at posteo.ch wrote:
| Hello
| I am trying to enhance my enhance my Rcpp class with the functionality to
| be saved across R sessions. For convenience reasons I would like to do that
| in R, using the ReferenceClass's S4 member function 'finalize()'. My plan

You talk about "your Rcpp class" and about RefClass / S4. 

Did you set up your object as an S4 or RefClass object?  You could but you
don't have to.

| is to simply save all object's fields (vectors of various types) to a
|  special list (with a special class name), remove the Rcpp object, and save

Serializing / saving all content is a good plan.  

But I am not sure what "remove the Rcpp object" is supposed to say.

| the plain R object instead. Would that work/be a good option? In
| particular, what happens on the C++ side if I call rm(reference_object)?

Rcpp objects are R objects. 

They have the same behaviour as rm() for R objects: reference counting, and
gc() effects only if nothing else depends on them (ie zero refcount).  [ I am
simplyfying here but that is the basic story. ]

| Would that remove the C++ side and free its memory? And how can the reverse

It depends. It can.  

[ I think we discuss that a little in the context of modules. Your Rcpp
objects can have finalizers. Else it is standard C++ behaviour. ]

| be accomplished, that is, if an object (identified by its class name) is
| loaded from a previous session, rebuild the C++ side and form the original
| reference object? 

There is a lot in this email, but it is a little muddled (and it is early in
the day here and I did not yet have that much coffee, but I did have a
morning run ...)

Conceptually, if it helped, you could think about 

 -- copying all content into an object you serialize (eg an R object for
    which you use save() or writeRDS())

 -- creating the reverse to create an object 'factory' that creates an
    instance of your Rcpp object given an R object.

The basic difficulty with more complicated Rcpp objects is that they are
dynamic objects which 'hang' at a semi-random memory location, and on
recreation you will have a different location.  

That made just calling save() impossible. And this was discussed (without a
generic solution as there probably isn;t one) here in the past.

Just FYI you could also look into other serilization approaches as eg in
RProtoBuf, see our vignette / paper at http://arxiv.org/abs/1401.7372

But the overall topic is not trivial.  Maybe try a simplification / problem
subset first.

Hope this helps, Dirk

| Sort of finalising:
| <art>
| x = new( 'Rcpp_Myclass' )
| x$finalize() calls:
| 	x_list = make_my_special_list( x )
| 	rm( x )
| and then the system calls:
| gc()
| </art>
| Sort of loading:
| <art>
| start_function_?() calls:
| 	x_lists = look_if_there_is_some_x_lists()
| 	for every x_list in x_lists:
| 		name = name_of(x_list)
| 		name = new( 'Rcpp_Myclass' )
| 		name$assign_values_from_x_list()
| </art>
| Thank you for help.
| Bests
| Sören
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com

More information about the Rcpp-devel mailing list