[R] Specifying search position for attached package

David Brahm brahm at alum.mit.edu
Wed Apr 30 15:54:28 CEST 2003

Jim Rogers <jrogers at cantatapharm.com> wrote:
> When I load an add-on package, is there any way to specify where it ends
> up in the search path (as with the pos argument to attach())? From the
> documentation for library(), this doesn't seem like an option. 

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])
    g.library(pkg, char=TRUE, pos=pos)
                              -- David Brahm (brahm at alum.mit.edu)

More information about the R-help mailing list