Skip to content

How to specify search order for require()

5 messages · A.J. Rossini, Thomas Lumley, Luke Tierney +1 more

#
In a .First.lib I want to issue two require()s to insure that two other packages are loaded.  But I want the package being loaded by .First.lib using library.dynam("mypackage",pkb,lib) to be higher in the search order than the two required packages, because I want to have a couple of functions from the two required packages overridden.  What is the best way to do that?  Thanks in advance -Frank
#
frank> In a .First.lib I want to issue two require()s to insure
    frank> that two other packages are loaded.  But I want the package
    frank> being loaded by .First.lib using
    frank> library.dynam("mypackage",pkb,lib) to be higher in the
    frank> search order than the two required packages, because I want
    frank> to have a couple of functions from the two required
    frank> packages overridden.  What is the best way to do that?
    frank> Thanks in advance -Frank 

Good question.  If you find out, let me know, too!

best,
-tony
#
On Fri, 12 Apr 2002, Frank E Harrell Jr wrote:

            
This would really need library() and require() to take a pos= argument (I
don't see why they shouldn't, but given the current feature freeze it's
not happening right now).

A work-around is to define two packages, say,
"mypackage" and "my.actual.package" and bundle them together.

.First.lib in "mypackage" would require() the two additional packages and
then require() "my.actual.package".

.First.lib in "my.actual.package" would check that the required packages
(and "mypackage") had been loaded, and otherwise give an error telling the
user to use library(mypackage) instead of library(my.actual.package).

As one of the comments in the R source code says in a different context,
this is the sort of thing that gives horrible hacks a bad name, but it
should work.

	-thomas


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
As of 1.4 (I believe) packages loaded by a require in the package
source, as oopsed to .First.lib, are loaded after the package that
calls require.  This was done in part to allow packages to
require(methods) and then use functions defined in methods within the
package body.

Hopefully R 1.6 will add a name space mechanism that will provide much
finer control over package dependencies; some information on how that
might work along with a draft implementation is available off the
developer page developer.r-project.org or at
http://www.stat.umn.edu/~luke/R/namespaces/morenames.html.

luke
On Fri, Apr 12, 2002 at 12:06:31PM -0400, Frank E Harrell Jr wrote:

  
    
#
Thank you Thomas Lumley and Luke Tierney for your responses.  I think in the long run Luke's approach is a good one.  For now I decided to be expeditious by redefining library to take a pos= argument.  All that's needed is to pass this to the attach( ) inside library, which I hope R-core will add to the library function.  I also modified require (calling it requirePos) to implement a pos argument.  My usage of these is as follows:

library(Hmisc)    
# attaches Hmisc library, redefine library, define requirePos

library(Design, pos=2)

.First.lib in Design:

.First.lib <- function(lib, pkg) {
  library.dynam("Design", pkg, lib)
  requirePos('Hmisc',pos=2)
  requirePos('survival',pos=3)
  p <- .packages()
  if(match('Design',p) > match('survival',p))
    warning('By not specifying library(Design,pos=2), functions in the\nsurvival package such as survfit will override those in Design.')
  invisible()
}

I realize that overrides are to be avoided but there are a few I couldn't get around easily.  I hope that library and require are enhanced with pos=.

Frank

On Fri, 12 Apr 2002 12:06:31 -0400
Frank E Harrell Jr <fharrell at virginia.edu> wrote: