\Header{library}{Loading and Listing of Packages}
\alias{provide}{library}
\alias{require}{library}
\alias{RLIBS}{library}
\alias{.Autoloaded}{library}
\alias{.First.lib}{library}
\alias{.Library}{library}
\alias{.Provided}{library}
\alias{.packages}{library}
\alias{.lib.loc}{library}
\keyword{data}{library}
\begin{Usage}
\begin{verbatim}
library(name, help = NULL, lib.loc = .lib.loc,
	character.only = FALSE, logical.return = FALSE,
	warn.conflicts = TRUE)
require(name, quietly = FALSE)
provide(name)

.First.lib(libname, pkgname)

.packages(all.available = FALSE, lib.loc = .lib.loc)
.lib.loc
.Library
.Provided
.Autoloaded
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\texttt{name, help}] \texttt{name} or character string giving the name of a
package.
\item[\texttt{lib.loc}] a character vector describing the location of \R
library trees to search through.
\item[\texttt{character.only}] a logical indicating whether \texttt{name} or
\texttt{help} can be assumed to be character strings.
\item[\texttt{logical.return}] logical.  If it is \texttt{TRUE},  \texttt{FALSE} or
\texttt{TRUE} is returned to indicate success.
\item[\texttt{warn.conflicts}] logical.  If \texttt{TRUE}, warnings are
printed about \texttt{\Link{conflicts}} from attaching the new package.
\item[\texttt{quietly}] a logical.  If \texttt{TRUE}, a warning will not be
printed if the package cannot be found.
\item[\texttt{libname}] a character string giving the library directory where
the package was found.
\item[\texttt{pkgname}] a character string giving the name of the package.
\item[\texttt{all.available}] logical; if \texttt{TRUE} return \texttt{character}
vector of all available packages in \texttt{lib.loc}.
\end{ldescription}
\end{Arguments}
\begin{Description}
\texttt{library(name)} and \texttt{require(name)} both load the package
named \texttt{name}.  \texttt{provide} allows code to register services that
it provides.

\texttt{.First.lib()} is called when a package is loaded by \texttt{library()}. 
\texttt{.packages()} and the \texttt{.xxx} variables return information about
package availability.\end{Description}
\begin{Details}
\texttt{library(name)} and \texttt{require(name)} both load the package
with name \texttt{name}.  \texttt{require} is designed for use inside
other functions; it returns \texttt{FALSE} and optionally gives a
warning, rather than giving an error, 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.  \texttt{require} also
checks the list \texttt{.Provided}.

\texttt{provide} allows code to register services that it provides.  The
argument is stored in the list \texttt{.Provided}.  \texttt{provide}
returns \texttt{FALSE} if the name was already present in
\texttt{.Provided} or among the packages in \texttt{search()}.  The main
use for \texttt{provide} is when multiple packages share code.	This is
most likely when the code implements features present in S(-PLUS) but
not in R. For example, the spline functions \texttt{ns}, \texttt{bs} and
so on are not included in the \R\ distribution.  A package containing
these functions can use \texttt{provide(splines)} to register this fact.
Another package that needs the functions can execute
\texttt{require(splines)} rather than \texttt{library(splines)} to load
the spline package only if their functionality is not already available.

If \texttt{library} is called with no \texttt{name} or \texttt{help}
argument, it gives a list of all available packages in \texttt{lib.loc}
and invisibly returns their names (same as \texttt{.packages(all=T)}).

\texttt{library(help = name)} prints information on
the package \texttt{name}, typically by listing the most important user
level objects it contains.

\texttt{.First.lib()} is called when a package is loaded by
\texttt{library(.)}.  It is called with two arguments, the name of the
library tree where the package was found (i.e., the corresponding
element of \texttt{lib.loc}), and the name of the package (in that
order).  It is a good place to put calls to \texttt{library.dynam()}
which are needed when loading a package into this function (don't call
\texttt{library.dynam()} directly, as this will not work if the package
is not installed in a ``standard'' location).  \texttt{.First.lib()}
is invoked after \texttt{search()} has been updated, so
\texttt{pos.to.env(match("package:name"), search())} will return the
environment in which the package is stored.

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

\texttt{.Autoloaded} contains the ``base names'' of the packages for
which autoloading has been promised.

\texttt{.Library} is a character string giving the location of the
default library, the ``library'' subdirectory of \texttt{RHOME}.
\texttt{.lib.loc} is a character vector with the locations of all
library trees that \R\ should use.  It is initialized at startup from
the environment variable \texttt{RLIBS}, which should be a
colon-separated list of directories at which \R\ library trees are
rooted, and \texttt{.Library}.\end{Details}
\begin{Value}
\texttt{library} returns the list of loaded (or available) packages (or
\texttt{TRUE} if \texttt{logical.return} is \texttt{TRUE}).
\texttt{require} returns a logical indicating whether the required
package is available.\end{Value}
\begin{Section}{Creating Packages}
Packages provide a mechanism for loading optional code and attached
documentation as needed.  The \R\ distribution provides the example
packages \texttt{eda}, \texttt{mva}, and \texttt{stepfun}.

A package consists of a subdirectory containing the files
\file{DESCRIPTION}, \file{INDEX}, and \file{TITLE}, and the
subdirectories \file{R}, \file{data}, \file{exec}, \file{man}, and
\file{src} (some of which can be missing).

The \file{DESCRIPTION} file contains information about authors,
version, copyright, etc., and looks like

Package:\\{}
Version:\\{}
Author:\\{}
Description:\\{}
Depends:\\{}
License:

Continuation lines (e.g., for descriptions longer than one line) start
with a whitespace character.  The license field should contain an
explicit statement or a well-known abbreviation (such as `GPL',
`LGPL', `BSD' and `Artistic'), maybe followed by a reference to the
actual license file.  It is very important that this information is
included---otherwise, it may not even be legally correct for others
to distribute copies of the package.

The \file{TITLE} file contains a line giving the name of the package
and a brief description.  \file{INDEX} contains a line for each
sufficiently interesting object in the package, giving its name and a
description (functions such as print methods not usually called
explicitly might not be included).  Note that you can automatically
create this file using the \texttt{Rdindex} program in \file{RHOME/etc},
provided that Perl is available on your system.

The \file{R} subdirectory contains \R\ code files.  The code files to
be installed must start with a (lower- or uppercase) letter and have
one of the extensions \file{.R}, \file{.S}, \file{.q}, \file{.r}, or
\file{.s}.  We recommend using \file{.R}, as this extension seems to
be not used by any other software.  If necessary, one of these files
(historically \file{zzz.R}) should use \texttt{library.dynam()}
\emph{inside} \texttt{.First.lib()} to load compiled code.

The \file{man} subdirectory should contain \R\ documentation files for
the objects in the package.  The documentation files to be installed
must also start with a (lower- or uppercase) letter and have the
extension \file{.Rd} (the default) or \file{.rd}.

Source and a Makefile for the compiled code is in \file{src},
containing C, FORTRAN, or Ratfor source.  The Makefile will be
passed various machine-dependent compile and link flags, examples of
which can be seen in the \texttt{eda} package.

The \file{data} subdirectory is for additional data files the package
makes available for loading using \texttt{data()}.  Currently, data
files can have one of three types as indicated by their extension:
plain R code (\file{.R} or \file{.r}), tables (\file{.tab},
\file{.txt}, or \file{.csv}), or \texttt{save()} images (\file{.RData}
or \file{.rda}).  See the documentation for \texttt{data()} for more
information.  If there is a \file{data} subdirectory it should contain
a \file{00Index} file that describes the datasets available.

Finally, \file{exec} could contain additional executables the package
needs, typically Shell or Perl scripts.  This mechanism is currently
not used by any package, and still experimental.\end{Section}
\begin{Author}
R core;  Guido Masarotto for the \texttt{all.available=TRUE}
part of \texttt{.packages}.\end{Author}
\begin{SeeAlso}
\texttt{\Link{attach}}, \texttt{\Link{detach}}, \texttt{\Link{search}},
\texttt{\Link{objects}}, \texttt{\Link{autoload}},
\texttt{\Link{library.dynam}};
\texttt{\Link{data}};
\texttt{\Link{INSTALL}}, \texttt{\Link{REMOVE}}\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
( .packages() )	    	    # maybe just "base"
.packages(all = TRUE)       # return all available as char.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
require(nonexistent)	    # FALSE
## Suppose the a package needs to call a shared library named "foo.EXT",
## where "EXT" is the system-specific extension.  Then you should use
.First.lib <- function(lib, pkg) {
  library.dynam("foo", pkg, lib)
}
\end{ExampleCode}
\end{Examples}

