[R] load object

Ray Brownrigg ray at mcs.vuw.ac.nz
Thu Jan 13 20:57:13 CET 2005


> From: Douglas Bates <bates at wisc.edu> Fri Jan 14 08:35:33 2005
> 
> Weiwei Shi wrote:
> > Hi,
> > I happen to re-write my codes to save memory and my
> > approach is write my obj into file first and later I
> > load it.
> > 
> > However, it seems like:
> > load(filename) can load the object but the function
> > returns the name of the object instead of the
> > reference to it. For example, I have an object called
> > r0.prune, which is saved by
> > save(r0.prune, file='r0.prune')
> > 
> > and later, I want to load it by using:
> > load('r0.prune')
> > but I need to put the reference to the object r0.prune
> > into a var or a list. I tried:
> > t<-load('r0.prune'),
> > and class(t) gave me a char, which means t stores the
> > name of obj instead of the obj itself.
> > 
> > Sorry for the dumb question but please help...
> > 
> > Weiwei
> 
> I was going to suggest that you read the help page for load but when I 
> looked at it myself I found that it was not obvious what the effect of 
> calling load at the prompt is.  The help page is accurate but it is a 
> bit confusing if you don't know what the default environment is.
> 
> Anyway, when called from the prompt, load has the side effect of loading 
> the object into the global environment.  Try
> 
Well ?load does say:
Value:

     A character vector of the names of objects created, invisibly.

Note the plurals.  The point is that the file being loaded may contain
the definition of more than one R object.  You can say:
tt <- get(load('r0.prune'))	# t is not a good example name to use
which will do what you want, but:
1) load() has a side-effect of also creating r0.prune (in your case)
2) if the file 'r0.prune' contains more than one object, only the first
is assigned to tt (but all of them are loaded into memory with their
original names).

Hope this helps,
Ray Brownrigg




More information about the R-help mailing list