[Rd] pos argument to library

David Brahm brahm@alum.mit.edu
Thu, 28 Mar 2002 17:52:05 -0500


While R-1.5.0 is still unfrozen, I'd like to lobby again for a "pos" argument
in library().  It seems straightforward to implement -- just change this line:
  env <- attach(NULL, name=pkgname)
to:
  env <- attach(NULL, pos, name=pkgname)

The reason I want this, by the way, is to mimic the S-Plus synchronize()
command.  Essentially, my version of synchronize() simply detaches a package
and then re-attaches it in the same position (thus requiring the "pos"
argument).  A fancier version would handle dynamic shared libraries correctly.
Here it is (it's called g.sync), if anyone is interested.

--- begin code ---
g.sync <- function(pos=2) {
  if (is.character(pos)) pos <- match(pos, sub("package:","",search()))
  pkg <- sub("package:", "", search()[pos])
  path <- searchpaths()[pos]
  is.lib <- match(dirname(path), .lib.loc, 0) > 0
  g.library <- g.library;  g.data.attach <- g.data.attach
  detach(pos=pos)
  if (is.lib) g.library(pkg, char=T, pos=pos) else g.data.attach(path, pos)
}
--- end code ---

Note that g.library is my copy of library() with the "pos" argument added, and
that I've assumed the "g.data" package is in use.  g.sync makes local copies of
g.library and g.data.attach in case the package being sync'ed contains one of
those functions!

-- 
                              -- David Brahm (brahm@alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._