[R] Query on save.image()
Can anyone please tell me how can use save.image() function if it is placed within a function (i.e. some level up from the base level environment)? Here I experimented with following codes: #rm(list=ls()) fn - function() { x - rnorm(5) save.image(f:/dat.RData) } fn() However I see that, the object fn() is actually stored in dat.RData file, not that x. I have gone through the help page and saw there is some argument named envir My question is if I need to supply some value against that argument, then what should be the name of the required environment? Additionally is there any option to see the hierarchy of different environments at my current R session? Thanks, __ 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] Query on save.image()
Hi, I do not believe you can use the save.image() function in this case. save.image() is a wrapper for save() with defaults for the global environment (your workspace). Try this instead, I believe it does what you are after: myfun - function(x) { y - 5 * x + x^2 save(list = ls(envir = environment(), all.names = TRUE), file = myfile.RData, envir = environment()) } Notice that for both save() and ls() I used the environment() function to grab the current environment. This should mean that even if y was defined globally, it would save a copy of the version inside your function. Hope that helps, Josh On Thu, Oct 14, 2010 at 9:25 AM, Megh Dal megh700...@yahoo.com wrote: Can anyone please tell me how can use save.image() function if it is placed within a function (i.e. some level up from the base level environment)? Here I experimented with following codes: #rm(list=ls()) fn - function() { x - rnorm(5) save.image(f:/dat.RData) } fn() However I see that, the object fn() is actually stored in dat.RData file, not that x. I have gone through the help page and saw there is some argument named envir My question is if I need to supply some value against that argument, then what should be the name of the required environment? Additionally is there any option to see the hierarchy of different environments at my current R session? Thanks, __ 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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ __ 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] Query on save.image()
On Thu, Oct 14, 2010 at 11:56 AM, Joshua Wiley jwiley.ps...@gmail.com wrote: Hi, I do not believe you can use the save.image() function in this case. save.image() is a wrapper for save() with defaults for the global environment (your workspace). Try this instead, I believe it does what you are after: myfun - function(x) { y - 5 * x + x^2 save(list = ls(envir = environment(), all.names = TRUE), file = myfile.RData, envir = environment()) } Notice that for both save() and ls() I used the environment() function to grab the current environment. This should mean that even if y was defined globally, it would save a copy of the version inside your function. I think the defaults are actually ok in this case: myfun - function(x) { + y - 5 * x + x^2 + save(list = ls(all.names = TRUE), file = myfile.RData) + } print(load(myfile.RData)) [1] x y Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University 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] Query on save.image()
Hi, Given what Hadley said, it looks like you don't need either one. In any case the logic was this: ls() gets the NAMES of the objects save() gets the actual data Suppose in your workspace you define: y - Hello, from the Global Environment and then you used that little function I showed, that defines: y - 5 * x + x^2 ls() will only give you the name y, you still want save() to get the values from the right environment. Cheers, Josh On Thu, Oct 14, 2010 at 10:18 AM, Megh Dal megh700...@yahoo.com wrote: Thanks Joshua for your reply. However I could not understand one logic. If I write ls(envir = environment(), all.names = TRUE), I am actually telling R to grab all objects within the current environment (in my case, which the environment within fn()). Then what is the point to put again the same against envir. By putting so, what I am going to tell R? Thanks, --- On Thu, 10/14/10, Joshua Wiley jwiley.ps...@gmail.com wrote: From: Joshua Wiley jwiley.ps...@gmail.com Subject: Re: [R] Query on save.image() To: Megh Dal megh700...@yahoo.com Cc: r-h...@stat.math.ethz.ch Date: Thursday, October 14, 2010, 10:26 PM Hi, I do not believe you can use the save.image() function in this case. save.image() is a wrapper for save() with defaults for the global environment (your workspace). Try this instead, I believe it does what you are after: myfun - function(x) { y - 5 * x + x^2 save(list = ls(envir = environment(), all.names = TRUE), file = myfile.RData, envir = environment()) } Notice that for both save() and ls() I used the environment() function to grab the current environment. This should mean that even if y was defined globally, it would save a copy of the version inside your function. Hope that helps, Josh On Thu, Oct 14, 2010 at 9:25 AM, Megh Dal megh700...@yahoo.com wrote: Can anyone please tell me how can use save.image() function if it is placed within a function (i.e. some level up from the base level environment)? Here I experimented with following codes: #rm(list=ls()) fn - function() { x - rnorm(5) save.image(f:/dat.RData) } fn() However I see that, the object fn() is actually stored in dat.RData file, not that x. I have gone through the help page and saw there is some argument named envir My question is if I need to supply some value against that argument, then what should be the name of the required environment? Additionally is there any option to see the hierarchy of different environments at my current R session? Thanks, __ 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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ __ 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] Query on save.image()
Thanks Joshua for your reply. However I could not understand one logic. If I write ls(envir = environment(), all.names = TRUE), I am actually telling R to grab all objects within the current environment (in my case, which the environment within fn()). Then what is the point to put again the same against envir. By putting so, what I am going to tell R? Thanks, --- On Thu, 10/14/10, Joshua Wiley jwiley.ps...@gmail.com wrote: From: Joshua Wiley jwiley.ps...@gmail.com Subject: Re: [R] Query on save.image() To: Megh Dal megh700...@yahoo.com Cc: r-h...@stat.math.ethz.ch Date: Thursday, October 14, 2010, 10:26 PM Hi, I do not believe you can use the save.image() function in this case. save.image() is a wrapper for save() with defaults for the global environment (your workspace). Try this instead, I believe it does what you are after: myfun - function(x) { y - 5 * x + x^2 save(list = ls(envir = environment(), all.names = TRUE), file = myfile.RData, envir = environment()) } Notice that for both save() and ls() I used the environment() function to grab the current environment. This should mean that even if y was defined globally, it would save a copy of the version inside your function. Hope that helps, Josh On Thu, Oct 14, 2010 at 9:25 AM, Megh Dal megh700...@yahoo.com wrote: Can anyone please tell me how can use save.image() function if it is placed within a function (i.e. some level up from the base level environment)? Here I experimented with following codes: #rm(list=ls()) fn - function() { x - rnorm(5) save.image(f:/dat.RData) } fn() However I see that, the object fn() is actually stored in dat.RData file, not that x. I have gone through the help page and saw there is some argument named envir My question is if I need to supply some value against that argument, then what should be the name of the required environment? Additionally is there any option to see the hierarchy of different environments at my current R session? Thanks, __ 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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/ __ 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.