[R] detach()

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Mar 22 22:20:17 CET 2004


"Liaw, Andy" <andy_liaw at merck.com> writes:

> > From: Rolf Turner
> > 
> > Dear Brian,
> > 
> > You wrote:
> > 
> > > If that is to be allowed, then it has to take precedence. What would
> > > you expect if I did
> > > 
> > > data(women)
> > > attach(women)
> > > women <- 4
> > > detach(women)
> > 
> > I would basically expect either all hell to break loose, mainly
> > because of the overwriting of ``women'' or the data base in position
> > 4 to be detached.
> 
> Don't think so.  attach(women) places a copy of `women' in search position
> 2.  women <- 4 creates another object holding the value 4 in the global
> environment.  No data have been destroyed or overwritten.  
> 
> [The current behavior of detach() apparently detaches `women' from position
> 2, rather than detaching whatever is in position 4.  This _is_ what I would
> expect, although I'd say whoever writes real code like that deserves all the
> confusion he/she gets...]

It isn't logical though. It's understandable that people want to see
the symmetry in 

attach(women)
detach(women)

but it really is a false consistency, since it makes good sense to
compute an object in the first case, but in the second what we need
is a name. E.g.

attach(transform(airquality,Month=factor(Month)))
detach(transform(airquality,Month=factor(Month)))

is a bit bizarre, although it works fine.

It's hardly the time to change this though (and if we do, I'd want to
rethink the whole workspace/searchpath structure), especially since
detach(pos=x) will do what you want soon enough.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list