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.