library {base} | R Documentation |
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.
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"))
require(package, lib.loc = NULL, quietly = FALSE,
warn.conflicts = TRUE,
keep.source = getOption("keep.source.pkgs"),
character.only = FALSE, save = TRUE)
.First.lib(libname, pkgname)
.Last.lib(libpath)
package , help |
the name of a package, given as a name or
literal character string, or a character string, depending on
whether |
pos |
the position on the search list at which to attach the
loaded package. Note that |
lib.loc |
a character vector describing the location of R
library trees to search through, or |
character.only |
a logical indicating whether |
logical.return |
logical. If it is |
warn.conflicts |
logical. If |
keep.source |
logical. If This argument does not apply to packages using lazy-loading. Whether they have kept source is determined when they are installed (and is most likely false). |
verbose |
a logical. If |
quietly |
a logical. If |
save |
logical or environment. If |
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. |
library(package)
and require(package)
both load the
package with name package
. require
is designed for use
inside other functions; it returns FALSE
and gives a warning
(rather than an error as library()
does by default) if the
package does not exist. Both functions check and update the list of
currently loaded packages and do not reload a package which is already
loaded. (Furthermore, if the package has a name space and a name
space of that name is already loaded, they work from the existing
name space rather than reloading from the file system. If you want to
reload a package, call detach
or
unloadNamespace
first.)
To suppress messages during the loading of packages use
suppressPackageStartupMessages
: this will suppress all
messages from R itself but not necessarily all those from package
authors.
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. Note that
installed.packages()
returns even more information.
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. When used with the default value (NULL
) for
lib.loc
, the loaded packages are searched before the libraries.
.First.lib
is called when a package without a name space is
loaded by library
. (For packages with name spaces see
.onLoad
.) 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.
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, but setHook
is preferred.
.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.)
Normally library
returns (invisibly) the list of loaded
packages, but TRUE
or FALSE
if logical.return
is
TRUE
. When called as library()
it returns an object of
class "libraryIQR"
, and for library(help=)
, one of
class "packageInfo"
.
require
returns (invisbly) a logical indicating whether the required
package is available.
NB: This mechanism has been almost entirely superseded by
using the Depends:
field in the ‘DESCRIPTION’ file of a package.
The source code for a
package that requires one or more other packages should have a call
to require
, preferably near the beginning of the source, and
of course before any code that uses functions, classes or methods
from the other package. The default for argument save
will
save the names of all required packages in the environment of the
new package. The saved package names are used by
detach
when a package is detached to warn if other
packages still require the to-be-detached package. Also, if a package is
installed with saved image (see INSTALL), the saved package
names are used to require these packages when the new package is
attached.
library
takes some further actions when package methods
is attached (as it is by default). Packages may define formal generic
functions as well as re-defining functions in other packages (notably
base) to be generic, and this information is cached whenever
such a package is loaded after methods and re-defined functions
(implicit generics) are excluded from the list of conflicts.
The caching and check for conflicts require looking for a pattern of
objects; the search may be avoided by defining an object
.noGenerics
(with any value) in the package. Naturally, if the
package does have any such methods, this will prevent them from
being used.
library
and require
can only load an installed
package, and this is detected by having a ‘DESCRIPTION’ file
containing a Built:
field.
Under Unix-alikes, the code checks that the package was installed
under a similar operating system as given by R.version$platform
(the canonical name of the platform under which R was compiled),
provided it contains compiled code. Packages which do not contain
compiled code can be shared between Unix-alikes, but not to other OSes
because of potential problems with line endings and OS-specific help
files. If sub-architectures are used, the OS similarity is not
checked since the OS used to build may differ (e.g.
i386-pc-linux-gnu
code can be built on an
x86_64-unknown-linux-gnu
OS).
The package name given to library
and require
must match
the name given in the package's ‘DESCRIPTION’ file exactly, even
on case-insensitive file systems such as MS Windows.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
.libPaths
, .packages
.
attach
, detach
, search
,
objects
, autoload
,
library.dynam
, data
,
install.packages
and
installed.packages
;
INSTALL
, REMOVE
.
library() # list all available packages
library(lib.loc = .Library) # list all packages in the default library
library(help = splines) # documentation on package 'splines'
library(splines) # load package 'splines'
require(splines) # the same
search() # "splines", too
detach("package:splines")
# if the package name is in a character vector, use
pkg <- "splines"
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()))
require(nonexistent) # FALSE
## Not run:
## Suppose a package needs to call a DLL 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, use
library(mypkg, pos = "package:base")
## End(Not run)