[R] detach()

Rolf Turner rolf at math.unb.ca
Mon Mar 22 21:32:09 CET 2004


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.

If you did

data(women)
attach(women)
melvin <- 4
detach(melvin)

I would again expect the data base in position 4 to be detached.

If, more realistically, you did

melvin <- grep("women",search())
detach(melvin)

I would expect the data set ``women'' to be detached.  My
expectations would of course be dashed.  The current structure of
detach() seems to me to be a counterintuitive contortion designed to
protect the user from doing something stupid.  Maybe that's a good
thing.

> ?  I think almost everyone would expect the current behaviour.

	I beg to differ.  Only someone who was in on the design
	process and saw the potential pitfall would expect the
	current behaviour.

	It is a rare and subtle phenomenon that

	> foo(42)

	gives a different result from

	> x <- 42
	> foo(x)

> I think the help page should say
> 
> When 'name' is a number, 'pos = name' is used.

	Even that would be totally misleading to the average user.
	None of us would distinguish between providing a numeric
	argument as an absolute constant and providing it as that
	same constant stored in a (scalar numeric) object.

	To make it clear what is going on an example should be
	given:

      ``When 'name' is an absolute constant, 'pos = name' is used.
	For example:
	> detach(2) # Works.
	> n <- 2
	> detach(n) # Doesn't work.''

					cheers,

						Rolf




More information about the R-help mailing list