[R] *not* using attach() *but* in one case ....

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 19 17:31:40 CEST 2011


On Thu, 19 May 2011, Liaw, Andy wrote:

> From: Prof Brian Ripley
>>
>> Hmm, load() does have an 'envir' argument.  So you could simply use
>> that and with() (which is pretty much what attach() does internally).
>>
>> If people really wanted a lazy approach, with() could be extended to
>> allow file names (as attach does).
>
> I'm not sure if laziness like this should be encouraged.

As you can tell from my wording, I was not (and indeed consider the 
change to attach() to accept .rda files to have been a mistake).

> If I may bring up another "black hole":  IMHO the formula interface
> allows too much flexibility (perhaps to allow some laziness?) that
> beginners and even non-beginners fall into its various traps a bit too
> often, and sometimes not even aware of it.  It would be great if there's
> a way to (optionally?) limit the scope of where a formula looks for
> variables.

Note, there is no such thing as 'the formula interface'.  There are 
several, but I expect you mean model.frame.default, for that is what 
is most commonly used to collect together the variables from which to 
interpret a formula.  Now in my view model.frame suffers from having 
been designed for S's scoping rules and subverted for R by users of 
lexical scoping.  So it has to work for both approaches (or break even 
more legacy code than the 1.2.0 changes did).   In my experience the 
most common trap is to pick up variables in the workspace when 'data' 
(or 'newdata') is supplied, and that is unavoidable if you allow 
lexical scoping as the workspace is almost always in the environment 
of the formula.  You can change the latter of course, but I suspect 
that in 99.9% of cases if a user supplies 'data' he intends to get all 
the variables from 'data'.

This is not really the place for such a disussion, so if you want to 
pursue it please restart it on R-devel.


>
> Just my $0.02...
>
> Andy
>
>> On Thu, 19 May 2011, Martin Maechler wrote:
>>
>>> [modified 'Subject' on purpose;
>>> Good mail readers will still thread correctly, using the
>> 'References'
>>> and 'In-Reply-To' headers, however, unfortunately,
>>> in my limited experience, good mail readers seem to
>> disappear more and more ..
>>> ]
>>>
>>>>>>>> Peter Ehlers <ehlers at ucalgary.ca>
>>>>>>>>     on Tue, 17 May 2011 06:08:30 -0700 writes:
>>>
>>>   > On 2011-05-17 02:22, Timothy Bates wrote:
>>>   >> Dear Bryony: the suggestion was not to change the name of
>>>   >> the data object, but to explicitly tell glm.nb what
>>>   >> dataset it should look in to find the variables you
>>>   >> mention in the formula.
>>>   >>
>>>   >> so the salient difference is:
>>>   >>
>>>   >> m1<- glm.nb(Cells ~ Cryogel*Day, data = side)
>>>   >>
>>>   >> instead of
>>>   >>
>>>   >> attach(side) m1<- glm.nb(Cells ~ Cryogel*Day)
>>>   >>
>>>   >> This works for other functions also, but not uniformly as
>>>   >> yet (how I wish it did and I could say hist(x, data=side)
>>>   >> Instead of hist(side$x)
>>>   >>
>>>   >> this inconsistency encourages the need for attach()
>>>
>>>   > Only if the user hasn't yet been introduced to the with()
>>>   > function, which is linked to on the ?attach page.
>>>
>>>   > Note also this sentence from the ?attach page:
>>>   > ".... attach can lead to confusion."
>>>
>>>   > I can't remember the last time I needed attach().
>>>   > Peter Ehlers
>>>
>>> Well, then you don't know  *THE ONE* case where modern users of
>>> R should use attach() ... as I have been teaching for a while,
>>> but seem not have got enought students listening ;-) ...
>>>
>>>  ---  Use it instead of load()  {for save()d R objects} ---
>>>
>>> The advantage of attach() over load() there is that loaded
>>> objects (and there maye be a bunch!), are put into a separate
>>> place in the search path and will not accidentally overwrite
>>> objects in the global "workspace".
>>>
>>> Of course, there are still quite a few situations {e.g. in
>>> typical BATCH use of R for simulations, or Sweaving, etc} where
>>> load() is good enough, and the extras of using attach() are not
>>> worth it.
>>>
>>> But the unconditional  "do not use attach()"
>>> is not quite ok,
>>> at least not when you talk to non-beginners.
>>>
>>> Martin Maechler, ETH Zurich
>>
>> --
>> 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
>>
>> ______________________________________________
>> 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.
>>
> Notice:  This e-mail message, together with any attachme...{{dropped:11}}
>
> ______________________________________________
> 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.
>

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