[R] detach()

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Mar 22 19:34:58 CET 2004


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




More information about the R-help mailing list