From ripley@stats.ox.ac.uk  Mon Nov  8 07:51:47 1999
From: ripley@stats.ox.ac.uk (Prof Brian D Ripley)
Date: Mon, 8 Nov 1999 07:51:47 +0000 (GMT)
Subject: [R] loading precedence in requires()
In-Reply-To: <199911080033.NAA23172@aqua.mcs.vuw.ac.nz>
Message-ID: <Pine.GSO.4.05.9911080727320.7214-100000@auk.stats>

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


[R] loading precedence in requires()
Prof Brian D Ripley