Rev 500 | Rev 768 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{library}\title{Loading and Listing of Packages}\usage{library(name, help = NULL, lib.loc = .lib.loc,character.only = FALSE, logical.return = FALSE)require(name, quietly = FALSE)provide(name)library.dynam(chname).First.lib(section,libname).packages().lib.loc.Library.Provided.Autoloaded}\alias{library}\alias{library.dynam}\alias{provide}\alias{require}\alias{RLIBS}\alias{.Autoloaded}\alias{.First.lib}\alias{.Library}\alias{.Provided}\alias{.packages}\alias{.lib.loc}\arguments{\item{name, help}{\code{name} or character string giving the name of apackage}\item{lib.loc}{a character vector describing the location of \R librarytrees to search through.}\item{character.only}{a logical indicating whether \code{name} or\code{help} can be assumed to be character strings}\item{quietly}{if \code{TRUE}, a warning will not be printed if thepackage cannot be found.}\item{chname}{a character string naming a shared library to load}\item{section}{a character string giving the library directory where thepackage was found}\item{section}{a character string giving the name of the subdirectory inwhich the package was found. This is always \code{name}}}\description{\code{library(name)} and \code{require(name)} both load the packagewith name \code{name}.\code{require} is designed for use inside other functions; it returns\code{FALSE} and optionally gives a warning, rather than giving anerror, 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. \code{require} alsochecks the list \code{.Provided}.\code{provide} allows code to register services that it provides. Theargument is stored in the list \code{.Provided}. \code{provide}returns \code{FALSE} if the name was already present in\code{.Provided} or among the packages in \code{search()}. The mainuse for \code{provide} is when multiple packages share code. This ismost likely when the code implements features present in S(-PLUS) butnot in R. For example, the spline functions \code{ns}, \code{bs} andso on are not included in the \R distribution. A package containingthese functions can use \code{provide(splines)} to register this fact.Another package that needs the functions can execute\code{require(splines)} rather than \code{library(splines)} to loadthe spline package only if their functionality is not already available.If \code{library} is called with no argument, it gives a list of allavailable packages. \code{library(help = name)} prints information onthe package \code{name}, typically by listing the most important userlevel objects it contains.\code{.First.lib()} is called when a package is loaded by\code{library()}. It is a good place to put calls to\code{library.dynam()}. It is invoked after \code{\link{search()}} hasbeen updated, so \code{pos.to.env(match("package:name"),search())}will return the environment in which the package is stored.\code{library.dynam} loads the specified (shared) object file if ithas not been loaded already. It is designed to be used inside apackage rather than at the command line. The system-specificextension for shared libraries (e.g., ``.so'' on Unix systems) shouldnot be added.\code{.packages()} returns the ``base names'' of the currently attachedpackages \emph{invisibly}.\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{RHOME}.\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{RLIBS}, which should be acolon-separated list of directories at which \R library trees arerooted, and \code{.Library}.}\section{Creating Packages}{Packages provide a mechanism for loading optional code and itsdocumentation as needed. The \R distribution provides the two examplepackages \code{eda} and \code{mva}.A package consists of a subdirectory containing a \file{TITLE} and\file{INDEX} file, and subdirectories \file{R}, \file{man} andoptionally \file{src}, \file{src-c}, \file{data}, and \file{exec}.The \file{TITLE} file contains a line giving the name of the packageand a brief description. \file{INDEX} contains a line for eachsufficiently interesting object in the package, giving its name and adescription (functions such as print methods not usually calledexplicitly might not be included).The \file{R} subdirectory contains \R code files with names beginningwith lowercase letters. One of these files should use\code{library.dynam()} to load any necessary compiled code.The \file{man} subdirectory should contain \R documentation files forthe objects in the package.Source and a Makefile for the compiled code is in \file{src}, and apure C version of the source should be in \file{src-c}. In the commoncase when all the source is in C it may be convenient to make one ofthese directories a symbolic link to the other. The Makefile will bepassed various machine-dependent compile and link flags, examples ofwhich can be seen in the \code{eda} package.The \file{data} subdirectory is for additional data files the packagemakes available for loading using \code{data()}. Note that (at leastcurrently) all such files are in fact R code files, and must have theextension `.R'.Finally, \file{exec} could contain executables, typically (shell orPerl) scripts, the package needs. Note that this mechanism currentlyonly is experimental.}\section{Installing and Removing Packages}{To install a package, do \code{R INSTALL pkg}, where \code{pkg} is thedirectory containing the package. If you want to install into thelibrary tree \code{lib} instead of the default one, use\code{R INSTALL pkg lib}.To remove the package \code{pkg} from the default library or thelibrary \code{lib}, do \code{R REMOVE pkg} or \code{R REMOVE pkg lib},respectively.}\value{\code{library} returns the list of loaded packages (or \code{TRUE} if\code{logical.return} is \code{TRUE}).\code{require} returns a logical indicating whether the requiredpackage is available.}\seealso{\code{\link{attach}}, \code{\link{detach}}, \code{\link{search}},\code{\link{objects}}, \code{\link{autoload}}.}\examples{print(.packages()) # maybe just "base"library() # 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 sameprint(.packages()) # "eda", toorequire(nonexistent) # FALSE}\keyword{data}