[R] load object

Marc Schwartz MSchwartz at MedAnalytics.com
Thu Jan 13 20:34:45 CET 2005


On Thu, 2005-01-13 at 11:00 -0800, 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...


Does the following help?


# create the object
> r0.prune <- 1:10

# display the object
> r0.prune
 [1]  1  2  3  4  5  6  7  8  9 10

# save the object
> save(r0.prune, file='r0.prune')

# show the object is present
> ls()
[1] "r0.prune"

# remove the object
> rm(r0.prune)

# show that the object is gone
> ls()
character(0)

# reload the object into the current workspace
> load('r0.prune')

# show the object is back
> ls()
[1] "r0.prune"

# display the object
> r0.prune
 [1]  1  2  3  4  5  6  7  8  9 10


# now remove the object again
> rm(r0.prune)

# It's gone
> ls()
character(0)

# now use:
> t <- load('r0.prune')

# See what is now present
> ls()
[1] "r0.prune" "t"

> t
[1] "r0.prune"

> r0.prune
 [1]  1  2  3  4  5  6  7  8  9 10


't' returns the _name(s)_ of the loaded object(s) as a character vector,
just as documented.

The object itself is available in the workspace. You can use 'r0.prune'
just as per normal routine:

> mean(r0.prune)
[1] 5.5

> MyList <- list(r0.prune)

> MyList
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10


HTH,

Marc Schwartz




More information about the R-help mailing list