[R-sig-teaching] pros/cons of teaching attach()

Martin Maechler maechler at stat.math.ethz.ch
Thu Sep 23 21:50:24 CEST 2010

>>>>> Douglas Bates <bates at stat.wisc.edu>
>>>>>     on Wed, 22 Sep 2010 07:10:14 -0500 writes:

    > On Wed, Sep 22, 2010 at 4:04 AM, Joshua Wiley
    > <jwiley.psych at gmail.com> wrote:
    >> Thanks to everyone for your thoughtful replies.  I think
    >> I am inclined not to use attach, but to bring it up and
    >> briefly mention some of the mistakes that are easy to
    >> make.  If students already know/use attach, it will at
    >> least be clear why I do not use it, and for students
    >> unfamiliar with it, they should not be tempted to start.
    >> Thanks again for all the responses!

    > I avoid teaching attach() and discourage its use if the
    > students have already seen it.  I do teach both with and
    > within (with provides read-only access, within provides
    > read-write access to the variable names in a data frame or
    > a list).  As mentioned in an earlier reply in this thread,
    > the use of extractor functions, like residuals() or
    > fitted() or coef(), should be preferred to reaching inside
    > a data object and grabbing a component that may or may not
    > continue to be defined in future versions,

    > The supplementary material on R that I use in a first-year
    > grad course on applied statistics is available, for this
    > semester, at www.stat.wisc.edu/~st849-1/Rnotes My approach
    > to data organization is shown in the "Introduction to R".

I agree entirely with Doug and Gavin(mostly) and Jonathan.

However,  there's one remark about attach() that "everyone" seems to
forget (AFAIR even Doug ;-):

Do consider (and teach!) using  attach("foobar.rda") for *.rda files
which I often find quite preferable to load().
I forgot who (from R-core) introduced this idea,
but I do like it:  It rehabilitates attach() into a "decent R
function" :-)


More information about the R-sig-teaching mailing list