[R] Query on save.image()
Hadley Wickham
hadley at rice.edu
Thu Oct 14 19:18:10 CEST 2010
On Thu, Oct 14, 2010 at 11:56 AM, Joshua Wiley <jwiley.psych at 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/
More information about the R-help
mailing list