Re: [R] loading saved files with objects in same names
In the future, you can avoid this problem by using saveRDS and readRDS. Hadley On Mon, Aug 18, 2014 at 7:30 PM, Jinsong Zhao jsz...@yeah.net wrote: Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. Best regards, Jinsong __ R-help@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. -- http://had.co.nz/ __ R-help@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.
Re: [R] loading saved files with objects in same names
Have you tried the 'envir' argument to load()? E.g., envA - new.environment() load(A.RData, envir=envA) envB - new.environment() load(B.RData, envir=envB) plot(A$object, B$object) Bill Dunlap TIBCO Software wdunlap tibco.com An alternative that I have been advocating is using attach(A.RData) etc. It does something similar as the above, but more conveniently: It loads the objects into a new environment *and* attaches that environment to your search() path, so you can access them directly, but attach() will never accidentally destroy existing R objects in your global environment ( = search()[[1]] ). Martin On Mon, Aug 18, 2014 at 5:30 PM, Jinsong Zhao jsz...@yeah.net wrote: Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. Best regards, Jinsong __ R-help@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. __ R-help@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. __ R-help@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.
Re: [R] loading saved files with objects in same names
On 20/08/2014, 8:58 AM, Barry Rowlingson wrote: On Tue, Aug 19, 2014 at 1:30 AM, Jinsong Zhao jsz...@yeah.net wrote: Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. The technique of loading into an environment already mentioned can be cleaned up and put into a function. First lets save a thing called x into two files with different values: x=first save(x,file=f.RData)) x=second save(x,file=s.RData) This little function wraps the loading: getFrom=function(file, name){e=new.env();load(file,env=e);e[[name]]} So now I can get 'x' from the first file - the value is returned from `getFrom` so I can assign it to anything: x1 = getFrom(f.RData,x) x1 [1] first x2 = getFrom(s.RData,x) x2 [1] second And I can even loop over RData files and read in all the `x`s into a vector: sapply(c(f.RData,s.RData),function(f){getFrom(f,x)}) f.RData s.RData first second (on second thoughts, possibly 'loadFrom' is a better name) That's a nice little function. You could also have lsFrom, that lists the objects stored in the file, along the same lines: lsFrom - function(file, all.names = FALSE, pattern) { e - new.env() load(file, envir = e) ls(e, all.names = all.names, pattern = pattern) } Duncan Murdoch Note that the solution of simply using attach(f.RData) makes the use of ls() very natural, and for Rstudio and similar gui users, even automatically part of their GUI. Martin Maechler __ R-help@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.
Re: [R] loading saved files with objects in same names
On 2014/8/22 1:02, Martin Maechler wrote: Have you tried the 'envir' argument to load()? E.g., envA - new.environment() load(A.RData, envir=envA) envB - new.environment() load(B.RData, envir=envB) plot(A$object, B$object) Bill Dunlap TIBCO Software wdunlap tibco.com An alternative that I have been advocating is using attach(A.RData) etc. It does something similar as the above, but more conveniently: It loads the objects into a new environment *and* attaches that environment to your search() path, so you can access them directly, but attach() will never accidentally destroy existing R objects in your global environment ( = search()[[1]] ). Martin Thanks a lot. I try your method, and I got: attach(D2.1.RData) The following objects are masked _by_ .GlobalEnv: coda.jags.1, df.1, jags.1, Mean, N In this case, how to access the masked objects? Best, Jinsong On Mon, Aug 18, 2014 at 5:30 PM, Jinsong Zhao jsz...@yeah.net wrote: Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. Best regards, Jinsong __ R-help@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.
Re: [R] loading saved files with objects in same names
On 22/08/2014, 1:14 PM, Jinsong Zhao wrote: On 2014/8/22 1:02, Martin Maechler wrote: Have you tried the 'envir' argument to load()? E.g., envA - new.environment() load(A.RData, envir=envA) envB - new.environment() load(B.RData, envir=envB) plot(A$object, B$object) Bill Dunlap TIBCO Software wdunlap tibco.com An alternative that I have been advocating is using attach(A.RData) etc. It does something similar as the above, but more conveniently: It loads the objects into a new environment *and* attaches that environment to your search() path, so you can access them directly, but attach() will never accidentally destroy existing R objects in your global environment ( = search()[[1]] ). Martin Thanks a lot. I try your method, and I got: attach(D2.1.RData) The following objects are masked _by_ .GlobalEnv: coda.jags.1, df.1, jags.1, Mean, N In this case, how to access the masked objects? Don't ever use attach(), and this won't be a problem. Martin gave you bad advice. Duncan Murdoch __ R-help@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.
Re: [R] loading saved files with objects in same names
On Tue, Aug 19, 2014 at 1:30 AM, Jinsong Zhao jsz...@yeah.net wrote: Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. The technique of loading into an environment already mentioned can be cleaned up and put into a function. First lets save a thing called x into two files with different values: x=first save(x,file=f.RData) x=second save(x,file=s.RData) This little function wraps the loading: getFrom=function(file, name){e=new.env();load(file,env=e);e[[name]]} So now I can get 'x' from the first file - the value is returned from `getFrom` so I can assign it to anything: x1 = getFrom(f.RData,x) x1 [1] first x2 = getFrom(s.RData,x) x2 [1] second And I can even loop over RData files and read in all the `x`s into a vector: sapply(c(f.RData,s.RData),function(f){getFrom(f,x)}) f.RData s.RData first second (on second thoughts, possibly 'loadFrom' is a better name) Barry __ R-help@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.
Re: [R] loading saved files with objects in same names
On 20/08/2014, 8:58 AM, Barry Rowlingson wrote: On Tue, Aug 19, 2014 at 1:30 AM, Jinsong Zhao jsz...@yeah.net wrote: Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. The technique of loading into an environment already mentioned can be cleaned up and put into a function. First lets save a thing called x into two files with different values: x=first save(x,file=f.RData)) x=second save(x,file=s.RData) This little function wraps the loading: getFrom=function(file, name){e=new.env();load(file,env=e);e[[name]]} So now I can get 'x' from the first file - the value is returned from `getFrom` so I can assign it to anything: x1 = getFrom(f.RData,x) x1 [1] first x2 = getFrom(s.RData,x) x2 [1] second And I can even loop over RData files and read in all the `x`s into a vector: sapply(c(f.RData,s.RData),function(f){getFrom(f,x)}) f.RData s.RData first second (on second thoughts, possibly 'loadFrom' is a better name) That's a nice little function. You could also have lsFrom, that lists the objects stored in the file, along the same lines: lsFrom - function(file, all.names = FALSE, pattern) { e - new.env() load(file, envir = e) ls(e, all.names = all.names, pattern = pattern) } Duncan Murdoch __ R-help@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.
[R] loading saved files with objects in same names
Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. Best regards, Jinsong __ R-help@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.
Re: [R] loading saved files with objects in same names
Have you tried the 'envir' argument to load()? E.g., envA - new.environment() load(A.RData, envir=envA) envB - new.environment() load(B.RData, envir=envB) plot(A$object, B$object) Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Aug 18, 2014 at 5:30 PM, Jinsong Zhao jsz...@yeah.net wrote: Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. Best regards, Jinsong __ R-help@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. __ R-help@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.
Re: [R] loading saved files with objects in same names
On 19/08/14 14:20, William Dunlap wrote: Have you tried the 'envir' argument to load()? E.g., envA - new.environment() load(A.RData, envir=envA) envB - new.environment() load(B.RData, envir=envB) plot(A$object, B$object) Did you mean plot(envA$object, envB$object) ??? Or am I misunderstanding something? cheers, Rolf -- Rolf Turner Technical Editor ANZJS __ R-help@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.
Re: [R] loading saved files with objects in same names
Rolf, Yes, I meant to write envA$object, etc, but did not read it twice before running off to dinner. Thanks. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Aug 18, 2014 at 7:52 PM, Rolf Turner r.tur...@auckland.ac.nz wrote: On 19/08/14 14:20, William Dunlap wrote: Have you tried the 'envir' argument to load()? E.g., envA - new.environment() load(A.RData, envir=envA) envB - new.environment() load(B.RData, envir=envB) plot(A$object, B$object) Did you mean plot(envA$object, envB$object) ??? Or am I misunderstanding something? cheers, Rolf -- Rolf Turner Technical Editor ANZJS __ R-help@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.
Re: [R] loading saved files with objects in same names
On 2014/8/18 19:20, William Dunlap wrote: Have you tried the 'envir' argument to load()? E.g., envA - new.environment() load(A.RData, envir=envA) envB - new.environment() load(B.RData, envir=envB) plot(A$object, B$object) Bill Dunlap TIBCO Software wdunlap tibco.com Thank you very much. It is what I want. new.environment() is not defined in R 3.1.1. There is new.env(). Best, Jinsong On Mon, Aug 18, 2014 at 5:30 PM, Jinsong Zhao jsz...@yeah.net wrote: Hi there, I have several saved data files (e.g., A.RData, B.RData and C.RData). In each file, there are some objects with same names but different contents. Now, I need to compare those objects through plotting. However, I can't find a way to load them into a workspace. The only thing I can do is to rename them and then save and load again. Is there a convenient to load those objects? Thanks a lot in advance. Best regards, Jinsong __ R-help@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. __ R-help@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.