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.