Search code examples
rimportfully-qualified-naming

How to avoid polluting the current scope (with `library(...)`)


As a matter of long-standing policy, I avoid importing names into (aka "polluting") the current scope, and instead I use fully-qualified names when referring to items defined in a different package.

The script below shows that, in R, using qualified names is, in itself, not enough.

#!/usr/bin/env Rscript

set.seed(0)

x <- local({
         x0 <- matrix(rnbinom(80, size = 5, mu = 10), nrow = 20)
         `rownames<-`(rbind(0, c(0, 0, 2, 2), x0),
                      paste("Tag", 1:(nrow(x0) + 2), sep = "."))
     })

y <- edgeR::DGEList(counts = x,
                    group = rep(1:2, each = 2),
                    lib.size = 1001:1004)

## library(edgeR)

y[1, 1]

The script fails with

Error in y[1, 1] : incorrect number of dimensions
Execution halted

The script's only crime appears to be not having included the line library(edgeR) somewhere before the failing statement, since the error disappears if one un-comments the commented-out line.

This is voodoo, imho.

Is there a way to avoid the error without polluting the current scope with library(edgeR)?


Solution

  • When you avoid loading the edgeR package, you also avoid loading the [.DGEList method, which is necessary to execute y[1, 1]. If you prefer not to load the edgeR library, you'll need to call the extraction function directly:

    edgeR::`[.DGEList`(y, 1, 1)
    

    If you don't like the fully qualified syntax, you can bring in the method you need with

    `[.DGEList` <- edgeR::`[.DGEList`
    

    Then y[1, 1] will work as expected. But this is another form of pollution and I'm not sure I'd recommend it as a general solution.