Alireza Mahani
2012-Jul-27 18:59 UTC
[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.
William Dunlap
2012-Jul-27 22:48 UTC
[R] Assigning a new name to object loaded with "load()"
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: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Alireza Mahani > Sent: Friday, July 27, 2012 11:59 AM > To: r-help at r-project.org > 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. > > ______________________________________________ > R-help at 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.
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