[R] Question about object permanence/marshalling

jim holtman jholtman at gmail.com
Thu Aug 25 16:07:38 CEST 2011


The problem I think is in your unmarshal.  'load' will load the object
into the local environment, not the global.  You have to explicitly
return it, that means you have to know the name that its was 'save'd
by;

> unmarshal <- function(xdr) {
>     object <- strsplit(strsplit(xdr, "\\.")[[1]][[1]], "/")
>     object <- object[[1]][length(object[[1]])]
>     load(xdr)
>     print(sprintf("Loading %s", xdr))
       ?????  # return the object that was just loaded; don't know if
they all have the same name.
>     }




On Thu, Aug 25, 2011 at 9:34 AM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:
> Hello,
>
> I am trying to write some code that dumps R objects to the harddisk in a binary format so they can be quickly re-used later. Goal is to save time. The objects may be quite large (e.g. classes for a GUI). I was thinking that save() and load() would be suitable for this (until now I only thought it could be used for 'real' data, e.g. matrices, data.frames etc), but I am hoping any object can be 'marshalled' using these functions. Probably I am doing something wrong in the unmarshal() function, perhaps with assign().
>
> Thank you in advance!
>
> AJ
>
>
> #########
> # Creation of test data
> #########
> setClass(
>   Class="Test",
>     representation=representation(
>       amounts="data.frame"
>       )
> )
> setMethod(
>   f="initialize",
>   signature="Test",
>   definition=function(.Object, amounts){
>     .Object at amounts <- amounts
>     return(.Object)
>   }
> )
> setGeneric (
>   name="doStuff",
>   def=function(.Object){standardGeneric("doStuff")}
> )
> setMethod(
>   f = "doStuff",
>   signature = "Test",
>   definition=function(.Object) {
>     return(mean(.Object at amounts, na.rm=TRUE))
>   }
> )
>
> print( objects() )
> instance <- new(Class="Test", data.frame(amount=runif(10, 0, 10)))
> doStuff(instance)
>
> #########
> # actual code (incomplete)
> #########
> marshal <- function(object) {
>     fn <- file.path(Sys.getenv()["TEMP"], paste(object, ".xdr", sep=""))
>     save(object, file=fn, compress=FALSE)
>     print(sprintf("Saving %s", fn))
>     }
> unmarshal <- function(xdr) {
>     object <- strsplit(strsplit(xdr, "\\.")[[1]][[1]], "/")
>     object <- object[[1]][length(object[[1]])]
>     assign(object, load(xdr))
>     print(sprintf("Loading %s", xdr))
>     }
> print(objects())
> lapply(c("doStuff", "instance"), marshal)
> rm(list=c("doStuff", "instance"))
>
> xdrs <- Sys.glob(file.path(Sys.getenv()["TEMP"], "*.xdr"))
> lapply(xdrs, unmarshal)
> print(objects())  ## doStuff and instance do not appear! :-(
>
>
> Cheers!!
> Albert-Jan
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list