[R] Multiple copies of attached packages

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Apr 14 15:58:18 CEST 2005


On Thu, 14 Apr 2005, Liaw, Andy wrote:

> I suspect you've attach()'ed `DF' multiple times in your
> code (possibly inside a loop, or perhaps a function that
> was called several times).  Note that if it were a
> `package', it would show up in search() as `package:DF'
> rather than just `DF'.  Also, R Core folks took care to
> avoid attaching the same package multiple times:
>
>> library(MASS)
>> search()
> [1] ".GlobalEnv"        "package:MASS"      "package:methods"
> "package:stats"
> [5] "package:graphics"  "package:grDevices" "package:utils"
> "package:datasets"
> [9] "Autoloads"         "package:base"
>> library(MASS)
>> search()
> [1] ".GlobalEnv"        "package:MASS"      "package:methods"
> "package:stats"
> [5] "package:graphics"  "package:grDevices" "package:utils"
> "package:datasets"
> [9] "Autoloads"         "package:base"
>
> Notice how trying to load a package that's already on the
> search path has no effect.
>
> This is not true for R objects, though.
>
> When you attach a data frame, say, `DF', (or a list), it
> places a _copy_ on the search path, so you can access
> the variables in the data frame (or components of the
> list) directly.  When you make modifications to the
> variables (such as x[i] <- something, rather than
> DF$x[i] <- something), the modifications are applied to
> the _copy_ on the search path, not the original.

Not quite.  The correct description is in ?attach (and apart from 
mentioning attach was used, reading the help before posting is de rigeur).
Here is the version from 2.1.0 beta (which has been expanded):

      The database is not actually attached.  Rather, a new environment
      is created on the search path and the elements of a list (including
      columns of a dataframe) or objects in a save file are _copied_
      into the new environment.  If you use '<<-' or 'assign' to assign
      to an attached database, you only alter the attached copy, not the
      original object.  (Normal assignment will place a modified version
      in the user's workspace: see the examples.) For this reason
      'attach' can lead to confusion.

Examples:

      summary(women$height)   # refers to variable 'height' in the data frame
      attach(women)
      summary(height)         # The same variable now available by name
      height <- height*2.54   # Don't do this. It creates a new variable
                              # in the user's workspace
      find("height")
      summary(height)         # The new variable in the workspace
      rm(height)
      summary(height)         # The original variable.
      height <<- height*25.4  # Change the copy in the attached environment
      find("height")
      summary(height)         # The changed copy
      detach("women")
      summary(women$height)   # unchanged

Notice the difference between <- and <<- .

Assigning to an element follows the same rules, and in addition is an 
error unless an object exists that can be suitably subscripted.


-- 
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