attach.all() for R

The attach() function in R can be frustrating because it does not overwrite. So we wrote attach.all():

attach.all <- function (x, overwrite = NA, name = "attach.all")  {
    rem <- names(x) %in% ls(.GlobalEnv)
    if (!any(rem)) overwrite <- FALSE
    rem <- names(x)[rem]
    if (is.na(overwrite)) {
        question <- paste("The following objects in .GlobalEnv will mask\nobjects in the attached database:\n", paste(rem, collapse = ", "), "\nRemove these objects from .GlobalEnv?", sep = "")
        if (interactive()) {
            if (.Platform$OS.type == "windows")  overwrite <- "YES" == winDialog(type = "yesno",  question)
            else overwrite <- 1 == menu(c("YES", "NO"), graphics = FALSE, title = question)
        }
        else overwrite <- FALSE
    }
    if (overwrite) remove(list = rem, envir = .GlobalEnv)
    attach(x, name = name)
}

(Thanks to the commenters for telling me how to do the indentation in html.) It's in the R2WinBUGS package. I never use attach() now, I only use attach.all(). It overwrites by default, whcih is what I want to do when attaching a dataset.

For more details, see the help page. (Install the arm package from your R console (which automatically installs R2WinBugs and some other things, type library("R2WinBUGS"), then type ?attach.all)

6 thoughts on “attach.all() for R

  1. Your best bet is probably "pre" which I believe is short for "preformatted," which causes the text inside to be displayed as-is (so, it preserves spaces, line breaks, etc).

  2. Looks like one very handy function!
    One way to show indentation in HTML is to use the <pre> </pre> tag (meaning "preformatted" I believe), which renders the text in a fixed-width font and preserves spaces, like so:

    <pre>
    This is
    an example
    of indented
    text.
    </pre>

  3. I think it's a really bad idea to use attach. There's to main reasons:

    confusion over what happens with updates:
    <pre>attach(mtcars)
    cyl <- cyl * 2
    mtcars$cyl
    mtcars$wt <- mtcars$wt ^ 2
    wt
    </pre>

    lack of transparency – all else being equal it's better to be verbose and explicit even though it means typing a little more.

    Most of the functions that do require multiple variables as input have a data argument, or you can always use the with function.

  4. Hadley,

    I know what you mean. But there are two situations where I find attach (or, more precisely, attach.all) very useful:

    1. After loading in a big data matrix with lots of named variables (i.e., columns of the matrix), it's convenient to attach.all so that I can work with the data directly (e.g., plot (height, weight), or whatever).

    2. After running a bugs model, it's convenient to be able to access the posterior simulations directly (alpha, beta, theta, whatever), so I use attach.bugs.

    Using the with function is fine if it's inside a function itself, but not so great when I'm trying to do exploratory analysis (which was the original purpose of S, I believe).

    One option would be to make an attach.all() that puts all the objects to be attached directly into the workspace (instead of having them live in this weird "attached" state). Would this be better, maybe?

Comments are closed.