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

From: Martin Maechler <>
Date: Thu, 19 May 2011 12:09:50 +0200

[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 <>
>>>>> 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 ;-) ...

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 mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 19 May 2011 - 10:11:56 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 19 May 2011 - 23:20:09 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive