[R] Query on save.image()

Megh Dal megh700004 at yahoo.com
Thu Oct 14 19:18:47 CEST 2010


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.psych at gmail.com> wrote:

> From: Joshua Wiley <jwiley.psych at gmail.com>
> Subject: Re: [R] Query on save.image()
> To: "Megh Dal" <megh700004 at yahoo.com>
> Cc: r-help at 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 <megh700004 at 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 at 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/
> 






More information about the R-help mailing list