[R] features of save and save.image (unexpected file sizes)

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Feb 2 10:12:29 CET 2007


On Thu, 1 Feb 2007, Vaidotas Zemlys wrote:

> Hi,
>
>
> On 2/1/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
>> On Thu, 1 Feb 2007, Vaidotas Zemlys wrote:
>>
>>> Hi,
>>>
>>> On 1/31/07, Professor Brian Ripley <ripley at stats.ox.ac.uk> wrote:
>>>> Two comments:
>>>>
>>>> 1) ls() does not list all the objects: it has all.names argument.
>>>>
>>> Yes, I tried it with all.names, but the effect was the same, I forgot
>>> to mention it in a letter.
>>>
>>>> 2) save.image() does not just save the objects in the workspace, it also
>>>> saves any environments they may have.  Having a function with a
>>>> large environment is the usual cause of a large saved image.
>>>
>>> I have little experience dealing with enivronments, so is there a
>>> quick way to discard the environments of the functions? When saving
>>> the session I really do not need them.
>>
>> Change, not discard.  E.g. environment(f) <- .GlobalEnv.  If environments
>> are not mentioned by anything saved, they will not be saved.
>>
>
> I found the culprit. I was parsing formulas in my code, and I saved
> them in that large object. So the environment came with saved
> formulas. Is there a nice way to say R: "please do not save the
> environments with the formulas, I do not need them?"

No, but why create them that way?  You could do

mmodel <- as.formula(mmodel, env=.GlobalEnv)

The R way is to create what you want, not fix up afterwards.

(I find your code unreadable--spaces help a great deal, so am not sure if 
I have understood it correctly.)

>
> This is what I was doing (I am discarding irrelevant code)
>
> testf<- function(formula) {
>   mainform <- formula
>   if(deparse(mainform[[3]][[1]])!="|") pandterm("invalid conditioning
> for main regression")
>    mmodel <- substitute(y~x,list(y=mainform[[2]],x=mainform[[3]][[2]]))
>    mmodel <- as.formula(mmodel)
>   list(formula=list(main=mmodel))
> }
>
> when called
> bu <- testf(lnp~I(CE/12000)+hhs|Country)
>
> I get
>
> ls(env=environment(bu$formula$main))
> [1] "formula"  "mainform" "mmodel"
>
> or in actual case, a lot of more objects, which I do not need, but
> which take a lot of place. For the moment I solved the problem with
>
> environment(mmodel) <- NULL
>
> but is this correct R way?
>
> Vaidotas Zemlys
> --
> Doctorate student, http://www.mif.vu.lt/katedros/eka/katedra/zemlys.php
> Vilnius University
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list