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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Alireza Mahani
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!
Reply | Threaded
Open this post in threaded view
|

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

William Dunlap
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
> tfile <- tempfile()
> myObj <- 101:107
> save(myObj, file=tfile)
> savedObj <- theObjectSavedIn(tfile)
> savedObj
[1] 101 102 103 104 105 106 107

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Alireza Mahani
> Sent: Friday, July 27, 2012 11:59 AM
> To: [hidden email]
> Subject: [R] Assigning a new name to object loaded with "load()"
>
> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

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

Rolf Turner-3
In reply to this post by Alireza Mahani
On 28/07/12 06:59, Alireza Mahani wrote:
> 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!

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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

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

Michael Young
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 <[hidden email]> wrote:

> On 28/07/12 06:59, Alireza Mahani wrote:
>>
>> 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!
>
>
> 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
>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

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

Alireza Mahani
In reply to this post by William Dunlap
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.
Reply | Threaded
Open this post in threaded view
|

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

Jeff Newmiller
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:<[hidden email]>        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 <[hidden email]> 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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

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

Ivan Calandra-2
In reply to this post by Alireza Mahani
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
[hidden email]
http://biogeosciences.u-bourgogne.fr/calandra

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

> 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.
>
> ______________________________________________
> [hidden email] 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.
>
>

______________________________________________
[hidden email] 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.