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

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Feb 2 14:14:34 CET 2007


On Fri, 2 Feb 2007, Vaidotas Zemlys wrote:

> Hi,
>
> On 2/2/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
>
>> > 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)
>> 
> Hm, but say I have some large object in .GlobalEnv, and I generate
> mmodel  10 different times and save the result as a list with length
> 10. Now if I try to save this list, R will save 10 different copies of
> .GlobalEnv together with aforementioned large object?

No, it saves the environment (here .GlobalEnv), not objects, and there can 
be many shared references.

>> 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.)
>> 
> Hm, I copied this code directly from Emacs+ESS, maybe the mailer
> mangled something. What I want to do with this piece of code (I will
> repaste it here)
>
> testf<- function(formula) {
>   mainform <- formula
>   if(deparse(mainform[[3]][[1]])!="|") stop("invalid conditioning")
>   mmodel <- substitute(y~x,list(y=mainform[[2]],x=mainform[[3]][[2]]))
>   mmodel <- as.formula(mmodel)
>   list(formula=list(main=mmodel))
> }

You use no spaces around your operators or after commas.  R does when 
deparsing:

> testf
function (formula)
{
     mainform <- formula
     if (deparse(mainform[[3]][[1]]) != "|")
         stop("invalid conditioning")
     mmodel <- substitute(y ~ x, list(y = mainform[[2]], x = mainform[[3]][[2]]))
     mmodel <- as.formula(mmodel)
     list(formula = list(main = mmodel))
}

because it is (at least to old hands) much easier to read.

IcanreadEnglishtextwithoutanyspacesbutIchoosenotto.Similarly,Rcode.Occasional
spacesare evenharderto parse.

> is to read formula with condition:
>
> formula(y~x|z)
>
> and construct formula
>
> formula(y~x)
>
> I looked for examples in code of coplot in library graphics and
> latticeParseFormula in library lattice.
>
> Vaidotas Zemlys
> --
> Doctorate student, http://www.mif.vu.lt/katedros/eka/katedra/zemlys.php
> Vilnius University
>

-- 
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