\Header{library}{Loading and Listing of Packages}
\alias{.Autoloaded}{library}
\alias{.find.package}{library}
\alias{.First.lib}{library}
\alias{.Last.lib}{library}
\alias{.libPaths}{library}
\alias{.Library}{library}
\alias{.noGenerics}{library}
\alias{.packages}{library}
\alias{.path.package}{library}
\alias{print.libraryIQR}{library}
\alias{print.packageInfo}{library}
\alias{require}{library}
\alias{R\_LIBS}{library}
\keyword{data}{library}
\begin{Description}\relax
\code{library} and \code{require} load add-on packages.

\code{.First.lib} is called when a package is loaded;
\code{.Last.lib} is called when a package is detached.

\code{.packages} returns information about package availability.

\code{.path.package} returns information about where a package was
loaded from.

\code{.find.package} returns the directory paths of installed
packages.\end{Description}
\begin{Usage}
\begin{verbatim}
library(package, help, pos = 2, lib.loc = NULL, character.only = FALSE,
        logical.return = FALSE, warn.conflicts = TRUE,
        keep.source = getOption("keep.source.pkgs"),
        verbose = getOption("verbose"), version)
require(package, quietly = FALSE, warn.conflicts = TRUE,
        keep.source = getOption("keep.source.pkgs"),
        character.only = FALSE, version, save = TRUE)

.First.lib(libname, pkgname)
.Last.lib(libpath)

.packages(all.available = FALSE, lib.loc = NULL)
.path.package(package = .packages(), quiet = FALSE)
.find.package(package, lib.loc = NULL, quiet = FALSE,
              verbose = getOption("verbose"))
.libPaths(new)

.Library
.Autoloaded
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{package, help}] the name of a package, given as a \link{name} or
literal character string, or a character string, depending on
whether \code{character.only} is \code{FALSE} (default) or
\code{TRUE}).
\item[\code{pos}] the position on the search list at which to attach the
loaded package.  Note that \code{.First.lib} may attach other
packages, and \code{pos} is computed \emph{after} \code{.First.lib}
has been run.  Can also be the name of a position on the current
search list as given by \code{\Link{search}()}.
\item[\code{lib.loc}] a character vector describing the location of \R{}
library trees to search through, or \code{NULL}.  The default value
of \code{NULL} corresponds to all libraries currently known.
\item[\code{character.only}] a logical indicating whether \code{package} or
\code{help} can be assumed to be character strings.
\item[\code{version}] A character string denoting a version number of the
package to be loaded.  If no version is given, a suitable default
is chosen.
\item[\code{logical.return}] logical.  If it is \code{TRUE},  \code{FALSE} or
\code{TRUE} is returned to indicate success.
\item[\code{warn.conflicts}] logical.  If \code{TRUE}, warnings are
printed about \code{\Link{conflicts}} from attaching the new
package, unless that package contains an object \code{.conflicts.OK}.
\item[\code{keep.source}] logical.  If \code{TRUE}, functions \dQuote{keep their
source} including comments, see argument \code{keep.source} to
\code{\Link{options}}.
\item[\code{verbose}] a logical.  If \code{TRUE}, additional diagnostics are
printed.
\item[\code{quietly}] a logical.  If \code{TRUE}, no message confirming
package loading is printed.
\item[\code{save}] logical or environment.  IF \code{TRUE}, a call to
\code{require} from the source for a package will save the name of
the required package in the variable \code{".required"}, allowing
function \code{\Link{detach}} to warn if a required package is
detached. See section \sQuote{Packages that require other packages} below.
\item[\code{libname}] a character string giving the library directory where
the package was found.
\item[\code{pkgname}] a character string giving the name of the package.
\item[\code{libpath}] a character string giving the complete path to the
package.
\item[\code{all.available}] logical; if \code{TRUE} return a character vector
of all available packages in \code{lib.loc}.
\item[\code{quiet}] logical.
For \code{.path.package}, should this not give warnings or an error
if the package(s) are not loaded?
For \code{.find.package}, should this not give warnings or an error
if the package(s) are not found?
\item[\code{new}] a character vector with the locations of \R{} library trees.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{library(package)} and \code{require(package)} both load the
package with name \code{package}.  \code{require} is designed for use
inside other functions; it returns \code{FALSE} and gives a warning
(rather than an error as \code{library()} does) if the package does
not exist.  Both functions check and update the list of currently
loaded packages and do not reload code that is already loaded.

For large packages, setting \code{keep.source = FALSE} may save quite
a bit of memory.

If \code{library} is called with no \code{package} or \code{help}
argument, it lists all available packages in the libraries specified
by \code{lib.loc}, and returns the corresponding information in an
object of class \code{"libraryIQR"}.  The structure of this class may
change in future versions.  In earlier versions of R, only the names
of all available packages were returned; use \code{.packages(all =
    TRUE)} for obtaining these.  Note that
\code{\Link{installed.packages}()} returns even more information.

\code{library(help = somename)} computes basic information about the
package \code{somename}, and returns this in an object of class
\code{"packageInfo"}.  The structure of this class may change in
future versions.

\code{.First.lib} is called when a package is loaded by
\code{library}.  It is called with two arguments, the name of the
library directory where the package was found (i.e., the corresponding
element of \code{lib.loc}), and the name of the package (in that
order).  It is a good place to put calls to \code{\Link{library.dynam}}
which are needed when loading a package into this function (don't call
\code{library.dynam} directly, as this will not work if the package
is not installed in a \dQuote{standard} location).  \code{.First.lib}
is invoked after the search path interrogated by \code{search()}
has been updated, so
\code{as.environment(match("package:name", search()))} will return the
environment in which the package is stored.  If calling
\code{.First.lib} gives an error the loading of the package is
abandoned, and the package will be unavailable.  Similarly, if the
option \code{".First.lib"} has a list element with the package's name,
this element is called in the same manner as \code{.First.lib} when
the package is loaded.  This mechanism allows the user to set package
\dQuote{load hooks} in addition to startup code as provided by the package
maintainers.

\code{.Last.lib} is called when a package is detached.  Beware
that it might be called if \code{.First.lib} has failed, so it
should be written defensively. (It is called within \code{\Link{try}},
so errors will not stop the package being detached.)

\code{.packages()} returns the \dQuote{base names} of the currently attached
packages \emph{invisibly} whereas
\code{.packages(all.available = TRUE)} gives (visibly) \emph{all}
packages available in the library location path \code{lib.loc}.

\code{.path.package} returns the paths from which the named packages
were loaded, or if none were named, for all currently loaded packages.
Unless \code{quiet = TRUE} it will warn if some of the packages named
are not loaded, and given an error if none are.  This function is not
meant to be called by users, and its interface might change in future
versions.

\code{.find.package} returns the paths to the locations where the
given packages can be found.  If \code{lib.loc} is \code{NULL}, then
then attached packages are searched before the libraries.  If a
package is found more than once, the first match is used.  Unless
\code{quiet = TRUE} a warning will be given about the named packages
which are not found, and an error if none are.  If \code{verbose} is
true, warnings about packages found more than once are given.  This
function is not meant to be called by users, and its interface might
change in future versions.

\code{.Autoloaded} contains the \dQuote{base names} of the packages for
which autoloading has been promised.

\code{.Library} is a character string giving the location of the
default library, the \file{library} subdirectory of \env{R\_HOME}.
\code{.libPaths} is used for getting or setting the library trees that
\R{} knows about (and hence uses when looking for packages).  If called
with argument \code{new}, the library search path is set to
\code{unique(new, .Library)} and this is returned.  If given no
argument, a character vector with the currently known library trees is
returned.

The library search path is initialized at startup from the environment
variable \env{R\_LIBS} (which should be a colon-separated list of
directories at which \R{} library trees are rooted) by calling
\code{.libPaths} with the directories specified in \env{R\_LIBS}.\end{Details}
\begin{Value}
\code{library} returns the list of loaded (or available) packages
(or \code{TRUE} if \code{logical.return} is \code{TRUE}).
\code{require} returns a logical indicating whether the required
package is available.\end{Value}
\begin{Section}{Packages that require other packages}
The source code for a
package that requires one or more other packages should have a call
to \code{require}, preferably near the beginning of the source, and
of course before any code that uses functions, classes or methods
from the other package.  The default for argument \code{save} will
save the names of all required packages in the environment of the
new package.  The saved package names are used by
\code{\Link{detach}} when a package is detached to warn if other
packages still require the detached package.  Also, if a package is
installed with saved image (see \link{INSTALL}), the saved package
names are used to require these packages when the new package is attached.\end{Section}
\begin{Section}{Formal methods}
\code{library} takes some further actions when package \pkg{methods}
is attached (as it is by default).  Packages may define formal generic
functions as well as re-defining functions in other packages (notably
\pkg{base}) to be generic, and this information is cached whenever
such a package is loaded after \pkg{methods} and re-defined functions
are excluded from the list of conflicts.  The check requires looking
for a pattern of objects; the pattern search may be avoided by
defining an object \code{.noGenerics}
(with any value) in the package.  Naturally, if the package
\emph{does} have any such methods, this will prevent them from being used.\end{Section}
\begin{Note}\relax
\code{library} and \code{require} can only load an \emph{installed}
package, and this is detected by having a \file{DESCRIPTION} file
containing a \code{Built:} field.  Packages
installed prior to 1.2.0 (released in December 2000) will need to be
re-installed.

Under Unix-alikes, the code checks that the package was installed
under a similar operating system as given by
\code{.Platform\$canonical.name}, provided it contains compiled code.
Packages which do not contain compiled code can be shared between
Unix-alikes, but not to other OSes because of potential problems with
line endings and OS-specific help files.

\code{library} and \code{require} use the underlying file system
services to locate the libraries, with the result that on
case-sensitive file systems package names are case-sensitive
(i.e., \code{library(foo)} is different from
\code{library(Foo)}), but they are not distinguished on
case-insensitive file systems such as MS Windows.  A warning
is issued if the user specifies a name which isn't a perfect match
to the package name, because future versions of \R{} will require
exact matches.\end{Note}
\begin{Author}\relax
R core;
Guido Masarotto for the \code{all.available=TRUE} part of
\code{.packages}.\end{Author}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.\end{References}
\begin{SeeAlso}\relax
\code{\Link{attach}}, \code{\Link{detach}}, \code{\Link{search}},
\code{\Link{objects}}, \code{\Link{autoload}},
\code{\Link{library.dynam}}, \code{\Link{data}},
\code{\Link{install.packages}} and \code{\Link{installed.packages}};
\code{\Link{INSTALL}}, \code{\Link{REMOVE}}.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
(.packages())               # maybe just "base"
.packages(all = TRUE)       # return all available as character vector
library()                   # list all available packages
library(lib = .Library)     # list all packages in the default library
library(help = eda)         # documentation on package 'eda'
library(eda)                # load package 'eda'
require(eda)                # the same
(.packages())               # "eda", too
detach("package:eda")

# if the package name is in a character vector, use
pkg <- "eda"
library(pkg, character.only = TRUE)
detach(pos = match(paste("package", pkg, sep=":"), search()))

require(pkg, character.only = TRUE)
detach(pos = match(paste("package", pkg, sep=":"), search()))

.path.package()
.Autoloaded                 # maybe "ctest"

.libPaths()                 # all library trees R knows about

require(nonexistent)        # FALSE
## Don't run: 
## Suppose a package needs to call a shared library named 'fooEXT',
## where 'EXT' is the system-specific extension.  Then you should use
.First.lib <- function(lib, pkg) {
  library.dynam("foo", pkg, lib)
}

## if you want to mask as little as possible, use
library(mypkg, pos = "package:base")
## End Don't run\end{ExampleCode}
\end{Examples}

