From ripley at stats.ox.ac.uk  Fri Jun 10 18:32:43 2005
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri, 10 Jun 2005 17:32:43 +0100 (BST)
Subject: [Rd] attach and object masking
In-Reply-To: <1118419953.3626.92.camel@seurat>
References: <1118419953.3626.92.camel@seurat>
Message-ID: <Pine.LNX.4.61.0506101721320.14746@gannet.stats>

On Fri, 10 Jun 2005, Martyn Plummer wrote:

> The library() function prints an information message when objects are
> masked on the search path by (or from) a newly attached package.  The
> attach() function, however, doesn't.  Is there a good reason for this?
> I have adapted the machinery for checking conflicts used in the library
> () function to attach().  If there are no objections, I will check this
> into R-devel.
>
> I have seen new users get into terrible trouble with attach, e.g.
> 1) attaching the same database multiple times, or
> 2) Masking a vector in an attached data frame with a transformed version
> in the global environment and *then* re-running a script that transforms
> the transformed variable. For example, run this snippet twice and you
> might get a surprise.
>
> foo <- data.frame(x=1:10)
> attach(foo)
> x <- x/2
>
> A warning would certainly help.
>
> Incidentally, (1) occurs in the example for the beavers data set. I
> didn't find any other problems.

Not so: it attaches different dfs with the same column names.  I think 
that is quite common and normally intentional.  (Not cleaning up is 
not good practice though, and that example can be improved a lot.)



[Rd] attach and object masking
Prof Brian Ripley