require load and attach add-on packages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
library(package, help, pos = 2, lib.loc = NULL, character.only = FALSE, logical.return = FALSE, warn.conflicts, quietly = FALSE, verbose = getOption("verbose"), mask.ok, exclude, include.only, attach.required = missing(include.only)) require(package, lib.loc = NULL, quietly = FALSE, warn.conflicts, character.only = FALSE, mask.ok, exclude, include.only, attach.required = missing(include.only)) conflictRules(pkg, mask.ok = NULL, exclude = NULL)
the name of a package, given as a name or
literal character string, or a character string, depending on
the position on the search list at which to attach the
loaded namespace. Can also be the name of a position on the current
search list as given by
a character vector describing the location of R
library trees to search through, or
a logical indicating whether
logical. If it is
a logical. If
a logical. If
character string naming a package.
character vector of names of objects that can mask objects on the search path without signaling an error when strict conflict checking is enabled
character vector of names of objects to
exclude or include in the attached frame. Only one of these arguments
may be used in a call to
logical specifying whether required packages
listed in the
require(package) both load the
namespace of the package with name
package and attach it on the
require is designed for use inside other
functions; it returns
FALSE and gives a warning (rather than an
library() does by default) if the package does not
exist. Both functions check and update the list of currently attached
packages and do not reload a namespace which is already loaded. (If
you want to reload such a package, call
unloadNamespace first.) If you want to load a
package without attaching it on the search list, see
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
library is called with no
argument, it lists all available packages in the libraries specified
lib.loc, and returns the corresponding information in an
object of class
"libraryIQR". (The structure of this class may
change in future versions.) Use
.packages(all = TRUE) to
obtain just the names of all available packages, and
installed.packages() for 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 (
lib.loc, the attached packages are searched before the libraries.
library returns (invisibly) the list of attached
TRUE. When called as
library() it returns an object of
"libraryIQR", and for
library(help=), one of
require returns (invisibly) a logical indicating whether the required
package is available.
Handling of conflicts depends on the setting of the
conflicts.policy option. If this option is not set, then
conflicts result in warning messages if the argument
TRUE. If the option is set to the
"strict", then all unresolved conflicts signal
errors. Conflicts can be resolved using the
include.only arguments to
require. Defaults for
exclude can be
conflicts.policy option is set to the string
"depends.ok" then conflicts resulting from attaching declared
dependencies will not produce errors, but other conflicts will.
This is likely to be the best setting for most users wanting some
additional protection against unexpected conflicts.
The policy can be tuned further by specifying the
conflicts.policy option as a named list with the following
TRUE treat unresolved
conflicts as errors.
FALSE issue a warning
message when conflicts are found.
TRUE ignore conflicts
created by defining S4 generics for functions on the search path.
TRUE do not treat
conflicts with required packages as errors.
character vector of names of packages that are allowed to be masked. These would typically be base packages attached by default.
Some packages have restrictive licenses, and there is a mechanism to
allow users to be aware of such licenses. If
getOption("checkPackageLicense") == TRUE, then at first
use of a package with a not-known-to-be-FOSS (see below) license the
user is asked to view and accept the license: a list of accepted
licenses is stored in file ‘~/.R/licensed’. In a non-interactive
session it is an error to use such a package whose license has not
already been recorded as accepted.
As from R 3.4.0 the license check is done when the namespace is loaded.
Free or Open Source Software (FOSS,
e.g. https://en.wikipedia.org/wiki/FOSS) packages are
determined by the same filters used by
available.packages but applied to just the current
package, not its dependencies.
There can also be a site-wide file ‘R_HOME/etc/licensed.site’ of packages (one per line).
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 namespace 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 namespace. Naturally, if the
package does have any such methods, this will prevent them from
require can only load/attach 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
(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
i386-pc-linux-gnu code can be built on an
The package name given to
require must match
the name given in the package's ‘DESCRIPTION’ file exactly, even
on case-insensitive file systems such as are common on Windows and
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
The initial set of packages attached is set by
options(defaultPackages=): see also
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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) # attach 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: ## if you want to mask as little as possible, use library(mypkg, pos = "package:base") ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.