Rev 14618 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{library}\title{Loading and Listing of Packages}\alias{library}\alias{require}\alias{R_LIBS}\alias{.Autoloaded}\alias{.First.lib}\alias{.Last.lib}\alias{.Library}\alias{.find.package}\alias{.packages}\alias{.path.package}\alias{.lib.loc}\description{\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 loaded.\code{.packages} returns information about package availability.\code{.path.package} returns information about where a package wasloaded from.\code{.find.package} returns the directory paths of installedpackages.}\usage{library(package, help = NULL, lib.loc = .lib.loc,character.only = FALSE, logical.return = FALSE,warn.conflicts = TRUE, keep.source = getOption("keep.source.pkgs"))require(package, quietly = FALSE, warn.conflicts = TRUE,keep.source = getOption("keep.source.pkgs")).First.lib(libname, pkgname).Last.lib(libpath).packages(all.available = FALSE, lib.loc = .lib.loc).path.package(package = .packages(), quiet = FALSE).find.package(package, lib.loc = .lib.loc, use.attached, quiet = FALSE).lib.loc.Library.Autoloaded}\arguments{\item{package, help}{\link{name} or character string giving the nameof a package.}\item{lib.loc}{a character vector describing the location of \Rlibrary trees to search through.}\item{character.only}{a logical indicating whether \code{package} or\code{help} can be assumed to be character strings.}\item{logical.return}{logical. If it is \code{TRUE}, \code{FALSE} or\code{TRUE} is returned to indicate success.}\item{warn.conflicts}{logical. If \code{TRUE}, warnings areprinted about \code{\link{conflicts}} from attaching the newpackage, unless that package contains an object \code{.conflicts.OK}.}\item{keep.source}{logical. If \code{TRUE}, functions ``keep theirsource'' including comments, see \code{\link{options}(keep.source = *)}.}\item{quietly}{a logical. If \code{TRUE}, a warning will not beprinted if the package cannot be found.}\item{libname}{a character string giving the library directory wherethe package was found.}\item{pkgname}{a character string giving the name of the package.}\item{libpath}{a character string giving the complete path to thepackage.}\item{all.available}{logical; if \code{TRUE} return a character vectorof all available packages in \code{lib.loc}.}\item{quiet}{logical.For \code{.path.package}, should this not give warnings or an errorif the package(s) are not loaded?For \code{.find.package}, should this not give warnings or an errorif the package(s) are not found?}\item{use.attached}{a logical indicating whether attached packagesshould be considered in addition to the ones installed in the givenlibraries.}}\details{\code{library(package)} and \code{require(package)} both load thepackage with name \code{package}. \code{require} is designed for useinside other functions; it returns \code{FALSE} and optionally gives awarning, rather than giving an error, if the package does not exist.Both functions check and update the list of currently loaded packagesand do not reload code that is already loaded.For large packages, setting \code{keep.source = FALSE} may save quitea bit of memory.If \code{library} is called with no \code{package} or \code{help}argument, it gives a list of all available packages in \code{lib.loc}and invisibly returns their names (same as\code{.packages(all = TRUE)}).\code{library(help = somename)} prints information on the package\code{somename}, typically by listing the most important user levelobjects it contains.\code{.First.lib} is called when a package is loaded by\code{library}. It is called with two arguments, the name of thelibrary directory where the package was found (i.e., the correspondingelement of \code{lib.loc}), and the name of the package (in thatorder). 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 packageis not installed in a ``standard'' location). \code{.First.lib}is invoked after the search path interrogated by \code{search()}has been updated, so\code{pos.to.env(match("package:name"), search())} will return theenvironment in which the package is stored. If calling\code{.First.lib} gives an error the loading of the package isabandoned, and the package will be unavailable. Similarly, if theoption \code{".First.lib"} has a list element with the package's name,this element is called in the same manner as \code{.First.lib} whenthe package is loaded. This mechanism allows the user to set package``load hooks'' in addition to startup code as provided by the packagemaintainers.\code{.Last.lib} is called when a package is detached. Bewarethat it might be called if \code{.First.lib} has failed, so itshould be written defensively. (It is called within \code{\link{try}},so errors will not stop the package being detached.)\code{.packages()} returns the ``base names'' of the currently attachedpackages \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 packageswere loaded, or if none were named, for all currently loaded packages.Unless \code{quiet = TRUE} it will warn if some of the packages namedare not loaded, and given an error if none are.\code{.find.package} returns the paths to the locations where thenames packages can be found. If \code{lib.loc} is not given or\code{use.attached} is \code{TRUE}, then attached packages aresearched before the libraries. If a package is found more than once,the first match is used. Unless \code{quiet = TRUE} a warning will begiven about the named packages which are not found, and an error ifnone are. This function is not meant to be called by users, and itsinterface might change in future versions.\code{.Autoloaded} contains the ``base names'' of the packages forwhich autoloading has been promised.\code{.Library} is a character string giving the location of thedefault library, the ``library'' subdirectory of \code{R_HOME}.\code{.lib.loc} is a character vector with the locations of alllibrary trees that \R should use. It is initialized at startup fromthe environment variable \code{R_LIBS} (which should be a#ifdef unixcolon-separated#endif#ifdef windowssemicolon-separated#endiflist of directories at which \R library trees are rooted) followed by\code{.Library}.}\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 requiredpackage is available.}\author{R core; Guido Masarotto for the \code{all.available=TRUE}part of \code{.packages}.}\seealso{\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}},\code{\link{INSTALL}}, \code{\link{REMOVE}}.}\examples{(.packages()) # maybe just "base".packages(all = TRUE) # return all available as character vectorlibrary() # list all available packageslibrary(lib = .Library) # list all packages in the default librarylibrary(help = eda) # documentation on package `eda'library(eda) # load package `eda'require(eda) # the same(.packages()) # "eda", too.path.package().Autoloaded # maybe "ctest".lib.loc.Library == .lib.loc[length(.lib.loc)] # `by definition'require(nonexistent) # FALSE\dontrun{## 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)}}\testonly{detach("package:eda")}}\keyword{data}