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 --------
[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 <edward.m at 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-bounces at r-project.org> On Behalf Of Edward McNeil
Sent: Monday, April 27, 2020 2:07 PM
To: r-help at 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