Skip to content

Assigning a new name to object loaded with "load()"

7 messages · William Dunlap, Rolf Turner, Michael Young +3 more

#
I would like to load a binary file into R using load(), and then assign a new
name to it, regardless of the name it was saved under. Can you please
provide a code sample? Thank you!



--
View this message in context: http://r.789695.n4.nabble.com/Assigning-a-new-name-to-object-loaded-with-load-tp4638144.html
Sent from the R help mailing list archive at Nabble.com.
#
Here is one way.  You seem to assume that the save file contains
exactly one object and this function makes the same assumption:
theObjectSavedIn <- function(saveFile) {
     env <- new.env()
     load(saveFile, envir=env)
     loadedObjects <- objects(env, all=TRUE)
     stopifnot(length(loadedObjects)==1)
     env[[loadedObjects]]
 }

Use it as
[1] 101 102 103 104 105 106 107

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
On 28/07/12 06:59, Alireza Mahani wrote:
Ummm,  what is the antecedent of the pronoun "it" in the forgoing?

The structure of your sentence makes it sound like "it" refers to
***the binary file*** --- but I don't believe that's what you mean.

If you by "it" you mean an object (the object? one of the objects?) in
the saved binary file, then something like:

     y <- x
     rm(x)

should do what you want.

Distinguish the container from the thing(s) contained.

     cheers,

         Rolf Turner
#
Another method, similar to William's.

x <- 3
save(x,file="test.Rdata")
x <- 4

y <- local({	
	load("test.Rdata")
	stopifnot(length(ls())==1)
	environment()[[ls()]]
})
On Fri, Jul 27, 2012 at 3:53 PM, Rolf Turner <rolf.turner at xtra.co.nz> wrote:
#
This works, thank you! I imagine that for large objects there will be a
penalty for calling this function since the objects will be loaded and then
copied as the function's return value.



--
View this message in context: http://r.789695.n4.nabble.com/Assigning-a-new-name-to-object-loaded-with-load-tp4638144p4638182.html
Sent from the R help mailing list archive at Nabble.com.
#
Don't imagine too much, because R is pretty good about not doing deep copies unless it is necessary to change data.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Alireza Mahani <alireza.s.mahani at gmail.com> wrote:

            
2 days later
#
Hi,

A bit late with my answer, but another possibility is to use to 
saveObject() / loadObject() functions from R.utils. I find it much 
easier to use than save() / load()

Example:
library(R.utils)
x <- 1:5
saveObject(x, file="test.Rbin")
y <- loadObject("test.Rbin")
identical(x,y)

HTH,
Ivan

--
Ivan CALANDRA
Universit? de Bourgogne
UMR CNRS/uB 6282 Biog?osciences
6 Boulevard Gabriel
21000 Dijon, FRANCE
+33(0)3.80.39.63.06
ivan.calandra at u-bourgogne.fr
http://biogeosciences.u-bourgogne.fr/calandra

Le 28/07/12 06:11, Alireza Mahani a ?crit :