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@amounts <- amounts
    return(.Object)
  }
)
setGeneric (
  name="doStuff",
  def=function(.Object){standardGeneric("doStuff")}
)
setMethod(
  f = "doStuff",
  signature = "Test",
  definition=function(.Object) {
    return(mean(.Object@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@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.

Reply via email to