Rev 27625 | 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{.libPaths}\alias{.noGenerics}\alias{print.libraryIQR}\alias{print.packageInfo}\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 detached.\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, 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}\arguments{\item{package, help}{the name of a package, given as a \link{name} orliteral character string, or a character string, depending onwhether \code{character.only} is \code{FALSE} (default) or\code{TRUE}).}\item{pos}{the position on the search list at which to attach theloaded package. Note that \code{.First.lib} may attach otherpackages, and \code{pos} is computed \emph{after} \code{.First.lib}has been run. Can also be the name of a position on the currentsearch list as given by \code{\link{search}()}.}\item{lib.loc}{a character vector describing the location of \Rlibrary trees to search through, or \code{NULL}. The default valueof \code{NULL} corresponds to all libraries currently known.}\item{character.only}{a logical indicating whether \code{package} or\code{help} can be assumed to be character strings.}\item{version}{A character string denoting a version number of thepackage to be loaded. If no version is given, a suitable defaultis chosen.}\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 \dQuote{keep theirsource} including comments, see argument \code{keep.source} to\code{\link{options}}.}\item{verbose}{a logical. If \code{TRUE}, additional diagnostics areprinted.}\item{quietly}{a logical. If \code{TRUE}, no message confirmingpackage loading is printed.}\item{save}{logical or environment. If \code{TRUE}, a call to\code{require} from the source for a package will save the name ofthe required package in the variable \code{".required"}, allowingfunction \code{\link{detach}} to warn if a required package isdetached. See section \sQuote{Packages that require other packages} below.}\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{new}{a character vector with the locations of \R library trees.}}\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 gives a warning(rather than an error as \code{library()} does) if the package doesnot exist. Both functions check and update the list of currentlyloaded packages and 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 lists all available packages in the libraries specifiedby \code{lib.loc}, and returns the corresponding information in anobject of class \code{"libraryIQR"}. The structure of this class maychange in future versions. In earlier versions of R, only the namesof all available packages were returned; use \code{.packages(all =TRUE)} for obtaining these. Note that\code{\link[utils]{installed.packages}()} returns even more information.\code{library(help = somename)} computes basic information about thepackage \code{somename}, and returns this in an object of class\code{"packageInfo"}. The structure of this class may change infuture versions.\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 \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 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\dQuote{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 \dQuote{base names} of the currentlyattached 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 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. This function is notmeant to be called by users, and its interface might change in futureversions.\code{.find.package} returns the paths to the locations where thegiven packages can be found. If \code{lib.loc} is \code{NULL}, thenthen attached packages are searched before the libraries. If apackage is found more than once, the first match is used. Unless\code{quiet = TRUE} a warning will be given about the named packageswhich are not found, and an error if none are. If \code{verbose} istrue, warnings about packages found more than once are given. Thisfunction is not meant to be called by users, and its interface mightchange in future versions.\code{.Autoloaded} contains the \dQuote{base names} of the packages forwhich autoloading has been promised.\code{.Library} is a character string giving the location of thedefault 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 calledwith argument \code{new}, the library search path is set tothe existing files in \code{unique(new, .Library)} and this isreturned. If given no argument, a character vector with the currentlyknown library trees is returned.#ifdef unixThe library search path is initialized at startup from the environmentvariable \env{R\_LIBS} (which should be a colon-separated list ofdirectories at which \R library trees are rooted) by calling\code{.libPaths} with the directories specified in \env{R\_LIBS}.#endif#ifdef windowsThe library search path is initialized at startup from the environmentvariable \env{R\_LIBS} (which should be a semicolon-separated list ofdirectories at which \R library trees are rooted) by calling\code{.libPaths} with the directories specified in \env{R\_LIBS}.#endif}\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.}\section{Packages that require other packages}{The source code for apackage that requires one or more other packages should have a callto \code{require}, preferably near the beginning of the source, andof course before any code that uses functions, classes or methodsfrom the other package. The default for argument \code{save} willsave the names of all required packages in the environment of thenew package. The saved package names are used by\code{\link{detach}} when a package is detached to warn if otherpackages still require the detached package. Also, if a package isinstalled with saved image (see \link[utils]{INSTALL}), the saved packagenames are used to require these packages when the new package is attached.}\section{Formal methods}{\code{library} takes some further actions when package \pkg{methods}is attached (as it is by default). Packages may define formal genericfunctions as well as re-defining functions in other packages (notably\pkg{base}) to be generic, and this information is cached wheneversuch a package is loaded after \pkg{methods} and re-defined functionsare excluded from the list of conflicts. The check requires lookingfor a pattern of objects; the pattern search may be avoided bydefining 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.}\note{\code{library} and \code{require} can only load an \emph{installed}package, and this is detected by having a \file{DESCRIPTION} filecontaining a \code{Built:} field. Packagesinstalled prior to 1.2.0 (released in December 2000) will need to bere-installed.Under Unix-alikes, the code checks that the package was installedunder a similar operating system as given by \code{R.version$platform}(the canonical name of the platform under which R was compiled),provided it contains compiled code. Packages which do not containcompiled code can be shared between Unix-alikes, but not to other OSesbecause of potential problems with line endings and OS-specific helpfiles.\code{library} and \code{require} use the underlying file systemservices to locate the libraries, with the result that oncase-sensitive file systems package names are case-sensitive(i.e., \code{library(foo)} is different from\code{library(Foo)}), but they are not distinguished oncase-insensitive file systems such as MS Windows. A warningis issued if the user specifies a name which isn't a perfect matchto the package name, because future versions of \R will requireexact matches.}\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[utils]{install.packages}} and\code{\link[utils]{installed.packages}};\code{\link[utils]{INSTALL}}, \code{\link[utils]{REMOVE}}.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole.}\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", toodetach("package:eda")# if the package name is in a character vector, usepkg <- "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 aboutrequire(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)}## if you want to mask as little as possible, uselibrary(mypkg, pos = "package:base")}}\keyword{data}