[ top | up ]

Loading of Packages

Syntax

library(name, help = NULL, lib.loc = .lib.loc)
library.dynam("name.so")
provide(name)
require(name, quietly = FALSE)
.lib.loc
.library(chname, lib.loc = .lib.loc, logical.return = FALSE)
.Provided

Arguments

name, chname the name of a package. chname must be a character string whereas name may also be a name.
lib.loc a character vector describing the location of R library trees to search through.
quietly if TRUE, a warning will not be printed if the package cannot be found.

Description

library and require both load a package. require is designed for use inside other functions; it returns 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, require also checks the list .Provided.

provide allows code to register services that it provides. The argument is stored in the list .Provided. provide returns FALSE if the name was already present in .Provided or among the packages in search(). The main use for 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 ns, bs and so on are not included in the R distribution. A package containing these functions can use provide(splines) to register this fact. Another package that needs the functions can execute require(splines) rather than library(splines) to load the spline package only if their functionality is not already available.

.library is an auxiliary function which is used by almost all others.

The functions called with no argument return useful information. library() gives a list of available packages; .library() returns the ``base names'' of the currently attached packages; provide() returns .Provided.

library.dynam loads the specified (shared) object file if it has not been loaded already. It is designed to be used inside a package rather than at the command line.

.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 RLIBS, which should be a colon-separated list of directories at which R library trees are rooted, and the R ``home'' directory RHOME.

library(help = name) prints a list of objects in package "name".

Creating Packages

Packages provide a mechanism for loading optional code and its documentation as needed. The R distribution provides the two example packages eda and mva.

A package consists of a subdirectory containing a TITLe and INDEX file, and subdirectories funs, man and optionally src, src-c, and data. The TITLe file contains a line giving the name of the package and a brief description. 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).

The funs subdirectory contains R code files with names beginning with lowercase letters. One of these files should use library.dynam() to load any necessary compiled code.

The man subdirectory should contain R documentation files for the objects in the package.

Source and a Makefile for the compiled code is in src, and a pure C version of the source should be in src-c. In the common case when all the source is in C it may be convenient to make one of these directories a symbolic link to the other. The Makefile will be passed various machine-dependent compile and link flags, examples of which can be seen in the eda package.

Finally, the data subdirectory is for additional data files the package makes available for loading using data().

Installing Packages

To install a package, do R INSTALL pkg, where pkg is the directory containing the package. If you want to install to the library tree lib instead of the default one, use R INSTALL pkg lib.

Value

library returns the list of loaded libraries; require returns a boolean value indicating whether the required package is available.

See Also

attach, detach, search, objects, autoload.

Examples

library(eda) require(eda) require(nonexistent) library(help = base)