[Rcpp-devel] Save objects across sessions
soeren.vogel at posteo.ch
soeren.vogel at posteo.ch
Tue Apr 21 19:27:00 CEST 2015
(For full original post, see http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-May/007539.html )
> On 06.05.2014, at 13:55, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> 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.
Thanks, Dirk, for the help, that was my plan. It would be totally sufficient to have all saved in a list from which I could recreate all C++ objects on unserialize.
In my C++ class (SVTEST, exposed through Rcpp module) I have a method that returns all necessary stuff as a list -- works charmingly. I have also a second constructor building a new object from a list. But how do I tell R to automatically use these two methods when saving to and loading from RData files?
I try around with serialize.Rcpp_SVTEST but that doesn't work. writeRDS() provides an option for a custom hook, but the general problem remains: how to bring R to invoke it before quitting a session with Rcpp_SVTEST objects in it?
Perhaps I am stuck because I do not understand how that "usually" works in R, but Google didn't help as well. Could you help?
Thanks in advance
Sören
Appendix:
If needed, an example package is here: https://www.dropbox.com/s/gieq5o2wh9bcq5k/SVTEST_0.0.1.tar.gz?dl=0 .
Some R stuff that may save typing, when trying to look at the test package from the link above:
##
library("SVTEST")
getRefClass("Rcpp_SVTEST")
x <- new("Rcpp_SVTEST")
x$getAsList()
y <- x$getAsList()
class(y) <- c(class(y), 'SVTSTlist')
z <- new( "Rcpp_SVTEST", y )
x$setX(1:8)
z$setY(LETTERS)
x$getAsList()
z$getAsList()
##
save( list = c('x', 'y', 'z'), file = 'SVTEST_Work.RData' )
rm( list = c('x', 'y', 'z') )
load('SVTEST_Work.RData')
x; y; z # :-(
More information about the Rcpp-devel
mailing list