Skip to content
Prev 590 / 885 Next

Problem with "attach"

It may also useful to get some understanding about how attach actually
works -- then you can use in the (possibly rare) cases when it is
convenient and useful, without being worried that it is "strongly not
recommended".

Look at what R told you -- quoted below in full but the first line
was: "The following objects are masked _by_ .GlobalEnv:     approach,
contributions"

The important point to notice here is that  `attach`  attach'es an
environment to the search path (try: search() ), which contains the
various packages that you have loaded, the base package, etc, and the
global environment is always on top. In other words, if you have
variable x in the global environment and you attach a data frame
containing a variable named x, then, by typing x, you will get the
value from the global environment.

Try this:

x <- "bla bla"
df <- data.frame(x=1:5, y=2:6)
print(df)
#     x y
# 1 1 2
# 2 2 3
# 3 3 4
# 4 4 5
# 5 5 6
attach(df)
# The following object is masked _by_ .GlobalEnv:
#
#   x
x
# [1] "bla bla"

But after that you had a lot of other messages:     "The following
objects are masked from donations (position 5):     approach,
contributions"

This probably means that you had attached a number of different copies
of the same data frame. This is almost never a good idea -- use detach
before attach'ing a data frame again (presumably after you've made
changes in it) and use search() to check what you have attached.

When you have a number of possibly different data frames attached, the
result becomes difficult to predict and depends on the order in which
the different copies were attached. Try this:


### copy-paste from here
df1 <- data.frame(x=2:6, y=3:7, z=4:8)
attach(df1)
df2 <- data.frame(x=1:5, y=2:6)
attach(df2)
x
y
z
#### end

##### output
The following object is masked _by_ .GlobalEnv:

    x
The following object is masked _by_ .GlobalEnv:

    x
The following objects are masked from df1:

    x, y
[1] "bla bla"
[1] 2 3 4 5 6
[1] 4 5 6 7 8
##### end of output

So in this example, x is taken from global environment, y from df2 and
z from df1 (because there is no z in df2 or global environment). The
environments in the search path are looked at in the order they're
shown in `search()`, for example:
[1] ".GlobalEnv"        "df2"               "df1"
 [4] "package:stats"     "package:graphics"  "package:grDevices"
 [7] "package:utils"     "package:datasets"  "package:methods"
[10] "Autoloads"         "package:base"

So you can overwrite any (almost any) function in base packages but
this only has an effect until you have these variables in the global
environment:

### commented output
[1] 1 9 3
[1] 1 9 3
[1] "blah"
[1] 1 9 3
[1] 1 9 3
####

If you understand all of the above (or most of it) then you can use
attach more or less safely. Of course, people who do not recommend
using attach are, in most cases, right -- too many things can go wrong
too easily -- but if you can avoid all the traps, attach can be a
useful tool. But I think it is definitely not useful for beginners --
and when you understand the basics of environments and search paths
and stuff, then you're not a complete beginner any more. These things
are, however, useful to understand even if you're not going to use
attach.

Best regards,

Kenn




*****
The following objects are masked _by_ .GlobalEnv:

    approach, contributions
The following objects are masked from donations (position 3):

    approach, contributions
The following objects are masked from donations (position 5):

    approach, contributions
The following objects are masked from donations (position 6):

    approach, contributions
The following objects are masked from donations (position 8):

    approach, contributions
The following objects are masked from donations (position 10):

    approach, contributions
The following objects are masked from donations (position 12):

    approach, contributions
The following objects are masked from donations (position 13):

    approach, contributions
The following objects are masked from donations (position 14):

    approach, contributions
The following objects are masked from donations (position 15):

    approach, contributions
The following objects are masked from donations (position 16):

    approach, contributions
On Mon, Dec 29, 2014 at 2:27 AM, Steven Stoline <sstoline at gmail.com> wrote: