[R] loading precedence in requires()

Prof Brian D Ripley ripley@stats.ox.ac.uk
Mon, 8 Nov 1999 07:51:47 +0000 (GMT)


[ Moved to R-devel as it is a think this is a development issue. ]

On Mon, 8 Nov 1999, Ray Brownrigg wrote:

> It appears that requires(), when it appears in the .First.lib of a
> package, will load the required package *after* the package requiring
> it, i.e. the 'required' package appears earlier in the search list than
> the package requiring it.
> 
> This will cause problems if a package (deliberately) masks any of the
> functions in the required package. I.e. the expected masking does not
> occur.
> 
> Apart from expecting the end-user to issue a library() call for the
> required package before the requiring package, are there any
> workarounds for this, or is such deliberate masking considered to be a
> no-no?

At present in R (unlike S) packages are effectively thought of as equal,
and searched in inverse order of loading. Unlike S, library (and hence
require) has no way to set the position in the ordering, nor can a library
`database' be re-attached in a different place in the order by attach().
(The latter is a common trick in .First.lib in S.)

I have two ideas for now:

(a) Write a package foo containing just a .First.lib with

require(bar)
require(foo2)

and put the real code in foo2.

(b) in the .First.lib for package foo, use something like

.First.lib <- function (...) 
{
    s <- search()
    require(bar)
    if (length(search()) > length(s)) {
        detach("package:foo")
        library(foo)
    }
}

In general such masking (presumably of a system library or why not enhance
bar?) is confusing and leads to support difficulties. It is a headache for
S-PLUS where Harrell's Design and Hmisc mask many system functions, and one
gets used to asking: "are you using Design or Hmisc?" if there are reports
of strange behaviour in one's own code.  My view has become `only mask if
you wish to correct something, and try to ge the original changed'.

However, question to R-devel: should we add to library and require
argument(s) to say where they should go in the search list?  S3 has
first=T (and defaults to last), and S-PLUS 5 has both first=T and pos
(which can be a number or database, and the library is inserted just before
the one specified, and always before main).  The most flexible scheme might
be to have both before= and after= for names or numbers.

Ross is planning changes in this area that may make the re-attach idea
feasible in R.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._