[R-sig-teaching] Problem with "attach"

Kenn Konstabel lebatsnok at gmail.com
Tue Dec 30 09:45:39 CET 2014


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
>df1 <- data.frame(x=2:6, y=3:7, z=4:8)
> attach(df1)
The following object is masked _by_ .GlobalEnv:

    x
> df2 <- data.frame(x=1:5, y=2:6)
> attach(df2)
The following object is masked _by_ .GlobalEnv:

    x
The following objects are masked from df1:

    x, y
> x
[1] "bla bla"
> y
[1] 2 3 4 5 6
> z
[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:

> search()
 [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
> c(1,9, 3)   # make a vector with c()
[1] 1 9 3
> c <- "blah"  # assign  a stupid value to "c"
> c(1,9, 3)   # but it still works - the first function named "c" in the search path is still base::c
[1] 1 9 3
> c <- function(...) "blah"   # now we overwrite the previous stupid value with a new function which always returns "blah"
> c(1,9, 3)     # and it works
[1] "blah"
> base::c(1,9,3)  # but we can still use the function in base package using :: operator
[1] 1 9 3
> rm(c)    # now we delete "c" from global env to restore the normal state of affairs
> c(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




*****
> attach(donations)
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:
> Dear Rich:
>
> Thank you very much. it works
>
> Steve
>
>
> On Sun, Dec 28, 2014 at 7:10 PM, Richard M. Heiberger <rmh at temple.edu>
> wrote:
>
>> Steven,
>>
>> It is strongly recommended that you don't use attach.
>> Use the data= argument.  This works.
>>
>> my.data <- data.frame(contributions=c(
>>                         1000,1500,1200,1800,1600,1100,1000,1250,
>>                         1500,1800,2000,1200,2000,1700,1800,1900,
>>                         0900,1000,1200,1500,1200,1550,1000,1100),
>>                       approach=factor(c(
>>                         1,1,1,1,1,1,1,1,
>>                         2,2,2,2,2,2,2,2,
>>                         3,3,3,3,3,3,3,3)))
>>
>> bwplot(contributions ~ approach, data=my.data) ## lattice graphics
>>
>> plot(contributions ~ approach, data=my.data)   ## base graphics
>>
>> Rich
>>
>> On Sun, Dec 28, 2014 at 6:35 PM, Steven Stoline <sstoline at gmail.com>
>> wrote:
>> > Dear All:
>> >
>> > I do have problem with attach. I believe that I am doing the data.frame
>> > correctly. But it seems that I do have problem with the "attach"
>> statement.
>> >
>> > here is what I am doing:
>> >
>> >
>> >
>> contributions<-c(1000,1500,1200,1800,1600,1100,1000,1250,1500,1800,2000,1200,2000,1700,1800,1900,900,1000,1200,1500,1200,1550,1000,1100)
>> > contributions
>> >
>> > ### approach<-c(rep(1,8), rep(2,8), rep(3,8))
>> >
>> > approach<-c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3)
>> > approach
>> >
>> > n<-length(contributions)
>> >
>> > donations <- data.frame(approach=approach,contributions=contributions)
>> > donations
>> >
>> >
>> > attach(donations)
>> >
>> >
>> >
>> > the code "*plot(approach~contributions)* " should yield the box-plots,
>> but
>> > it does not.
>> >
>> >
>> >
>> >
>> >
>> > This what I got from R:
>> > ================
>> >
>> >> attach(donations)
>> > 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
>> >
>> >
>> >
>> > Any help will be highly appreciated.
>> > with many thanks
>> > Steve
>> >
>> >
>> >
>> > --
>> > Steven M. Stoline
>> > 1123 Forest Avenue
>> > Portland, ME 04112
>> > sstoline at gmail.com
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > _______________________________________________
>> > R-sig-teaching at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
>>
>
>
>
> --
> Steven M. Stoline
> 1123 Forest Avenue
> Portland, ME 04112
> sstoline at gmail.com
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-teaching at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-teaching



More information about the R-sig-teaching mailing list