Skip to content
Prev 31413 / 398525 Next

Specifying search position for attached package

Jim Rogers <jrogers at cantatapharm.com> wrote:
I wrote a local version of library (g.library) which has an additional "pos=2"
argument.  It's an exact copy of library except for the one line:
   env <- attach(NULL, name = pkgname)
which becomes:
   env <- attach(NULL, pos, name=pkgname)

There may be subtle reasons why this could fail (and why it's not part of the
standard library function), but it's always worked just fine for me (in version
1.6.2 on Solaris 2.6; I haven't upgraded to 1.7.0 yet).

I also wrote a g.sync function that allows me to detach and re-attach (in the
same position) a package that has been modified and rebuilt.  It boils down to:
  g.sync <- function(pos=2) {
    pkg <- sub("package:", "", search()[pos])
    detach(pos=pos)
    g.library(pkg, char=TRUE, pos=pos)
  }