You probably need to change 'marshal' also.  I see that you are
passing in the character string and this is what you are saving.  what
you probably is something like this where you pass in the object and
then get its name:

>> marshal <- function(object.value) {
         object <- deparse(substitute(object.value))
>>     fn <- file.path(Sys.getenv()["TEMP"], paste(object, ".xdr", sep=""))
>>     save(object.value, file=fn, compress=FALSE)
>>     print(sprintf("Saving %s", fn))
>>     }

Then you unmarshal might look like:

>> unmarshal <- function(xdr) {
>>     object <- strsplit(strsplit(xdr, "\\.")[[1]][[1]], "/")
>>     object <- object[[1]][length(object[[1]])]
>>     load(xdr)
>>     print(sprintf("Loading %s", xdr))
>       object.value  # return what it was saved as
>>     }


play around with variation and use the debugger/browser to look at the
values within the functions; 'ls()' will also help.
On Thu, Aug 25, 2011 at 11:34 AM, Albert-Jan Roskam <fo...@yahoo.com> wrote:
> Hi Jim,
>
> Thanks for your reply. It seems that save and load can only be used for
> datasets (as the title in ?load suggests).
> I'd be very glad if I'm mistaken though!
>
> 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?
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> From: jim holtman <jholt...@gmail.com>
> To: Albert-Jan Roskam <fo...@yahoo.com>
> Cc: R Mailing List <r-help@r-project.org>
> Sent: Thursday, August 25, 2011 4:07 PM
> Subject: Re: [R] Question about object permanence/marshalling
>
> 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 <fo...@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@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.
>>
>>
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
>
>
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
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