This help topic is for R version 1.5.0. For the current version of R, try https://stat.ethz.ch/R-manual/R-patched/library/base/html/library.html
library {base}R Documentation

Loading and Listing of Packages

Description

library and require load add-on packages. .First.lib is called when a package is loaded; .Last.lib is called when a package is detached. .packages returns information about package availability. .path.package returns information about where a package was loaded from. .find.package returns the directory paths of installed packages.

Usage

library(package, help, lib.loc = NULL, character.only = FALSE,
        logical.return = FALSE, warn.conflicts = TRUE,
        keep.source = getOption("keep.source.pkgs"),
        verbose = getOption("verbose"))
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 = NULL)
.path.package(package = .packages(), quiet = FALSE)
.find.package(package, lib.loc = NULL, use.attached, quiet = FALSE,
              verbose = getOption("verbose"))
.libPaths(new)

.Library
.Autoloaded

Arguments

package, help

name or character string giving the name of a package.

lib.loc

a character vector describing the location of R library trees to search through, or NULL. The default value of NULL corresponds to all libraries currently known.

character.only

a logical indicating whether package or help can be assumed to be character strings.

logical.return

logical. If it is TRUE, FALSE or TRUE is returned to indicate success.

warn.conflicts

logical. If TRUE, warnings are printed about conflicts from attaching the new package, unless that package contains an object .conflicts.OK.

keep.source

logical. If TRUE, functions “keep their source” including comments, see argument keep.source to options.

verbose

a logical. If TRUE, additional diagnostics are printed.

quietly

a logical. If TRUE, a warning will not be printed if the package cannot be found.

libname

a character string giving the library directory where the package was found.

pkgname

a character string giving the name of the package.

libpath

a character string giving the complete path to the package.

all.available

logical; if TRUE return a character vector of all available packages in lib.loc.

quiet

logical. For .path.package, should this not give warnings or an error if the package(s) are not loaded? For .find.package, should this not give warnings or an error if the package(s) are not found?

use.attached

a logical indicating whether attached packages should be considered in addition to the ones installed in the given libraries.

new

a character vector with the locations of R library trees.

Details

library(package) and require(package) both load the package with name 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.

For large packages, setting keep.source = FALSE may save quite a bit of memory.

If library is called with no package or help argument, it lists all available packages in the libraries specified by lib.loc, and returns the corresponding information in an object of class "libraryIQR". The structure of this class may change in future versions. In earlier versions of R, only the names of all available packages were returned; use .packages(all = TRUE) for obtaining these.

library(help = somename) computes basic information about the package somename, and returns this in an object of class "packageInfo". The structure of this class may change in future versions.

.First.lib is called when a package is loaded by library. It is called with two arguments, the name of the library directory where the package was found (i.e., the corresponding element of lib.loc), and the name of the package (in that order). It is a good place to put calls to library.dynam which are needed when loading a package into this function (don't call library.dynam directly, as this will not work if the package is not installed in a “standard” location). .First.lib is invoked after the search path interrogated by search() has been updated, so as.environment(match("package:name"), search()) will return the environment in which the package is stored. If calling .First.lib gives an error the loading of the package is abandoned, and the package will be unavailable. Similarly, if the option ".First.lib" has a list element with the package's name, this element is called in the same manner as .First.lib when the package is loaded. This mechanism allows the user to set package “load hooks” in addition to startup code as provided by the package maintainers.

.Last.lib is called when a package is detached. Beware that it might be called if .First.lib has failed, so it should be written defensively. (It is called within try, so errors will not stop the package being detached.)

.packages() returns the “base names” of the currently attached packages invisibly whereas .packages(all.available = TRUE) gives (visibly) all packages available in the library location path lib.loc.

.path.package returns the paths from which the named packages were loaded, or if none were named, for all currently loaded packages. Unless quiet = TRUE it will warn if some of the packages named are not loaded, and given an error if none are. This function is not meant to be called by users, and its interface might change in future versions.

.find.package returns the paths to the locations where the given packages can be found. If lib.loc is NULL, then then attached packages are searched before the libraries. If a package is found more than once, the first match is used. Unless quiet = TRUE a warning will be given about the named packages which are not found, and an error if none are. If verbose is true, warnings about packages found more than once are given. Argument use.attached is deprecated. This function is not meant to be called by users, and its interface might change in future versions.

.Autoloaded contains the “base names” of the packages for which autoloading has been promised.

.Library is a character string giving the location of the default library, the ‘library’ subdirectory of R_HOME. .libPaths is used for getting or setting the library trees that R knows about (and hence uses when looking for packages). If called with argument new, the library search path is set to unique(new, .Library) and this is returned. If given no argument, a character vector with the currently known library trees is returned. The library search path is initialized at startup from the environment variable R_LIBS (which should be a colon-separated list of directories at which R library trees are rooted) by calling .libPaths with the directories specified in R_LIBS. Currently, the global variable .lib.loc is used for storing the paths to the known library trees; however, this variable should not be accessed directly.

Value

library returns the list of loaded (or available) packages (or TRUE if logical.return is TRUE). require returns a logical indicating whether the required package is available.

Author(s)

R core; Guido Masarotto for the all.available=TRUE part of .packages.

See Also

attach, detach, search, objects, autoload, library.dynam, data, install.packages, INSTALL, REMOVE.

Examples

(.packages())		    # maybe just "base"
.packages(all = TRUE)       # return all available as character 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
detach("package:eda")

# if the package name is in a character vector, use
pkg <- "eda"
library(pkg, character.only = TRUE)
detach(pos = match(paste("package", pkg, sep=":"), search()))

.path.package()
.Autoloaded                 # maybe "ctest"

.libPaths()                 # all library trees R knows about

require(nonexistent)        # FALSE
## Not run: ## 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)
}
## End(Not run)

[Package base version 1.5.0 ]