Dear Bert, > -----Original Message----- > From: Bert Gunter <bgunter.4...@gmail.com> > Sent: Monday, April 27, 2020 11:26 AM > To: Fox, John <j...@mcmaster.ca> > Cc: edwar...@psu.ac.th; r-help@r-project.org > Subject: Re: [R] deciphering help for `attach` > > What is the use case for attach? As the Help says, I find that with() or > sometimes within() handles the situations where I would use it.
I don't believe that I was making a general case for using attach(), and in fact was arguing to the contrary that its use can produce confusion. I was simply trying to clarify what the help page says, which was the focus of the original question. I've only once encountered a situation where I wanted to attach an environment (not a data frame) to the search path. Best, John > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along and > sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > On Mon, Apr 27, 2020 at 7:58 AM Fox, John <j...@mcmaster.ca> wrote: > > > > Dear Edward, > > > > Paragraph 4 in the help page goes on to say, "Rather, a new environment > is created on the search path and the elements of a list (including > columns of a data frame) or objects in a save file or an environment are > copied into the new environment." > > > > That seems reasonably clear to me. Here's an example that also > illustrates how attach can lead to confusion: > > > > ------- snip -------- > > > > > str(cars) > > 'data.frame': 50 obs. of 2 variables: > > $ speed: num 4 4 7 7 8 9 10 10 10 11 ... > > $ dist : num 2 10 4 22 16 10 18 26 34 17 ... > > > > > attach(cars) > > > > > search() > > [1] ".GlobalEnv" "cars" "tools:rstudio" > "package:stats" > > [5] "package:graphics" "package:grDevices" "package:utils" > "package:datasets" > > [9] "package:methods" "Autoloads" "package:base" > > > > > objects() > > character(0) > > > > > objects(pos=2) > > [1] "dist" "speed" > > > > > str(get("dist", pos=2)) > > num [1:50] 2 10 4 22 16 10 18 26 34 17 ... > > > > > dist <- 1:10 > > > > > head(dist) # shadows dist in copy of cars > > [1] 1 2 3 4 5 6 > > > > > head(get("dist", pos=2)) > > [1] 2 10 4 22 16 10 > > > > > assign("dist", 10:1, pos=2) # changes dist in objects copied from > > > cars > > > > > head(get("dist", pos=2)) > > [1] 10 9 8 7 6 5 > > > > ------- snip -------- > > > > Paragraph 5 also seems clear to me. Here's an example: > > > > ------- snip -------- > > > > > attach(NULL) > > > > > search() > > [1] ".GlobalEnv" "NULL" "cars" > "tools:rstudio" > > [5] "package:stats" "package:graphics" "package:grDevices" > "package:utils" > > [9] "package:datasets" "package:methods" "Autoloads" > "package:base" > > > > > assign("x", 10, pos=2) > > > > > x > > [1] 10 > > > > ------- snip -------- > > > > Now that may beg the question of why one would want to do something like > this, which isn't addressed in the help file, but a fair comment is that > if you don't need to store objects in an environment that's accessible on > the path, why worry about it? After all, no one is forcing you to use this > trick. > > > > Finally, I too recommended that students use attach() when I first > starting teaching with R, until I noticed that they frequently tied > themselves into knots by attaching different versions of the same data > during a session, producing confusion about where the data were coming > from and what version they were using. It's not hard in R to avoid the use > of attach(). Of course, attach() is still part of the language, so you, > and your students, are free to continue using it if you wish, and perhaps > your students avoid the problems that mine often created. > > > > Best, > > John > > > > ----------------------------- > > John Fox, Professor Emeritus > > McMaster University > > Hamilton, Ontario, Canada > > Web: http::/socserv.mcmaster.ca/jfox > > > > > On Apr 27, 2020, at 9:26 AM, Edward McNeil <edwar...@psu.ac.th> wrote: > > > > > > Dear Petr, > > > Thanks for your quick reply. Much appreciated. However, you haven't > > > really answered either of my questions, although I don't quite > understand your reference to La Gioconda. > > > > > > In any case, despite your strong recommendation not to use `attach`, > > > I am going to keep using it, as I have done successfully for the > > > past 16 years, and keep teaching it, until it either kills me or > > > disappears from R. Unfortunately I have to teach R to students and I > > > don't like it when they ask me "tricky" questions to which I have no > > > answer. ;) > > > -- > > > Edward McNeil > > > > > > On Mon, April 27, 2020 8:00 pm, PIKAL Petr wrote: > > > Hi. > > > > > > I strongly recommend not to use attach. I agree that mentioned > > > statements are rather contradictory and probably others could give > > > you more insightful answer. You could consider that by attaching > > > some data, you create something like a copy of original data in your > > > system with a feature that you can use column names directly. If you > > > change something in the data after attachment, you change only > attached version and not an original. > > > > > > It is similar as if you take a picture of Gioconda an use some > > > creativity to add a moustache to this picture. In any circumstances > > > moustache does not propagate to the original Louvre painting. Do not > perform any tricks, preferably do not perform attach. > > > > > > Cheers > > > Petr > > > > > >> -----Original Message----- > > >> From: R-help <r-help-boun...@r-project.org> On Behalf Of Edward > > >> McNeil > > >> Sent: Monday, April 27, 2020 2:07 PM > > >> To: r-help@r-project.org > > >> Subject: [R] deciphering help for `attach` > > >> > > >> Hi, > > >> I have two related questions. > > >> > > >> 1. In the help page for `attach` under "Details" it says in paragraph > 3: > > >> "By default the database is attached ..." > > >> > > >> But then paragraph 4 starts: "The database is not actually attached." > > >> > > >> Could somebody explain this contradiction? Is the data(base) > > >> attached or not? > > >> > > >> 2. What is meant by the 5th paragraph: "One useful ‘trick’ is to > > >> use what = NULL (or equivalently a length-zero list) to create a > > >> new environment on the search path into which objects can be assigned > by `assign` ... "? > > >> > > >> I don't understand what this "trick" is or why a "trick" needs to > > >> be performed here. > > >> > > >> Thanks > > >> -- > > >> Edward McNeil > > >> > > >> ______________________________________________ > > >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > >> https://stat.ethz.ch/mailman/listinfo/r-help > > >> PLEASE do read the posting guide http://www.R-project.org/posting- > > >> guide.html and provide commented, minimal, self-contained, > > >> reproducible code. > > > > > > ______________________________________________ > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide > > > http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > > > > > > > > > ______________________________________________ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.