I got bitten recently by the following behaviour of detach(); > save(file="Junk") > attach("Junk") > search() [1] ".GlobalEnv" "file:Junk" "package:methods" "package:ctest" [5] "package:mva" "package:modreg" "package:nls" "package:ts" [9] "package:Misc" "Autoloads" "package:base" > detach(2) > # No problem; reattach junk. > attach("Junk") > ind <- 2 > detach(ind) Error in detach(ind) : invalid name > is.numeric(ind) [1] TRUE The help on detach() says: name: The object to detach. Defaults to 'search()[pos]'. This can be a name or a character string but _not_ a character vector. pos: Index position in 'search()' of database to detach. When 'name' is 'numeric', 'pos = name' is used. ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Looking at the code of detach, I see that name gets assigned substitute(name) before the check as to whether name is numeric. So if name is provided as an object --- like ind --- then name becomes the name ``ind'' and is an object of mode "name" and so the check will evaluate to FALSE. (If name is given as an explicit constant --- like 2 --- then name remains equal to 2 and is indeed numeric so the check evaluates TRUE.) Is this the intended behaviour for detach() or is it a bug? It's obviously no big deal because detach(pos=ind) works perfectly. But it did trip me up and could conceivably trip others. cheers, Rolf Turner rolf at math.unb.ca
I think the point is that you supplied name as a name, not a numeric, although it is a fine distinction. name: The object to detach. Defaults to 'search()[pos]'. This can be a name or a character string but _not_ a character vector. ^^^^^^^^^ 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 think almost everyone would expect the current behaviour. I think the help page should say When 'name' is a number, 'pos = name' is used. Brian On Mon, 22 Mar 2004, Rolf Turner wrote:> > I got bitten recently by the following behaviour of detach(); > > > save(file="Junk") > > attach("Junk") > > search() > [1] ".GlobalEnv" "file:Junk" "package:methods" "package:ctest" > [5] "package:mva" "package:modreg" "package:nls" "package:ts" > [9] "package:Misc" "Autoloads" "package:base" > > detach(2) > > # No problem; reattach junk. > > attach("Junk") > > ind <- 2 > > detach(ind) > Error in detach(ind) : invalid name > > is.numeric(ind) > [1] TRUE > > The help on detach() says: > > name: The object to detach. Defaults to 'search()[pos]'. This can > be a name or a character string but _not_ a character vector. > > pos: Index position in 'search()' of database to detach. When > 'name' is 'numeric', 'pos = name' is used. ^^^^ > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > Looking at the code of detach, I see that name gets assigned > substitute(name) before the check as to whether name is numeric. So > if name is provided as an object --- like ind --- then name becomes > the name ``ind'' and is an object of mode "name" and so the check > will evaluate to FALSE. (If name is given as an explicit constant > --- like 2 --- then name remains equal to 2 and is indeed numeric so > the check evaluates TRUE.) > > Is this the intended behaviour for detach() or is it a bug? > > It's obviously no big deal because detach(pos=ind) works perfectly. > But it did trip me up and could conceivably trip others. > > cheers, > > Rolf Turner > rolf at math.unb.ca > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- 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
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
There is a paper on nonstandard evaluation at: http://developer.r-project.org/nonstandard-eval.pdf that seems relevant here. Date: Mon, 22 Mar 2004 16:32:09 -0400 (AST) From: Rolf Turner <rolf at math.unb.ca> To: <ripley at stats.ox.ac.uk> Cc: <r-help at stat.math.ethz.ch> Subject: Re: [R] detach() 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
> 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...] Cheers, Andy ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
> From: Liaw, Andy > > > 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.I forgot about the first line, data(women), which loads `women' into the global environment, and that _is_ overwritten by the line women <- 4. Andy ------------------------------------------------------------------------------Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates (which may be known outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as Banyu) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system.