The R Project SVN R

Rev

Rev 76248 | Rev 85065 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/library.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
65214 ripley 3
% Copyright 1995-2014 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
2 r 6
\name{library}
63986 ripley 7
\title{Loading/Attaching and Listing of Packages}
56186 murdoch 8
\alias{library}
12778 pd 9
\alias{require}
76248 luke 10
\alias{conflictRules}
56381 murdoch 11
 
23111 ripley 12
\alias{.noGenerics}
56752 hornik 13
\alias{format.libraryIQR}
16280 hornik 14
\alias{print.libraryIQR}
56752 hornik 15
\alias{format.packageInfo}
17778 hornik 16
\alias{print.packageInfo}
12778 pd 17
\description{
63986 ripley 18
  \code{library} and \code{require} load and attach add-on packages.
12778 pd 19
}
2 r 20
\usage{
30912 ripley 21
library(package, help, pos = 2, lib.loc = NULL,
22
        character.only = FALSE, logical.return = FALSE,
76248 luke 23
        warn.conflicts, quietly = FALSE,
24
        verbose = getOption("verbose"),
25
        mask.ok, exclude, include.only,
26
        attach.required = missing(include.only))
30915 ripley 27
 
44236 ripley 28
require(package, lib.loc = NULL, quietly = FALSE,
76248 luke 29
        warn.conflicts,
30
        character.only = FALSE,
31
        mask.ok, exclude, include.only,
32
        attach.required = missing(include.only))
33
 
34
conflictRules(pkg, mask.ok = NULL, exclude = NULL)
35
 
2 r 36
}
37
\arguments{
25543 hornik 38
  \item{package, help}{the name of a package, given as a \link{name} or
39
    literal character string, or a character string, depending on
40
    whether \code{character.only} is \code{FALSE} (default) or
74363 maechler 41
    \code{TRUE}.}
24384 ripley 42
  \item{pos}{the position on the search list at which to attach the
63986 ripley 43
    loaded namespace.  Can also be the name of a position on the current
25543 hornik 44
    search list as given by \code{\link{search}()}.}
768 hornik 45
  \item{lib.loc}{a character vector describing the location of \R
16670 hornik 46
    library trees to search through, or \code{NULL}.  The default value
56177 ripley 47
    of \code{NULL} corresponds to all libraries currently known to
48
    \code{\link{.libPaths}()}.
35063 ripley 49
    Non-existent library trees are silently ignored.}
7418 maechler 50
  \item{character.only}{a logical indicating whether \code{package} or
882 hornik 51
    \code{help} can be assumed to be character strings.}
2802 maechler 52
  \item{logical.return}{logical.  If it is \code{TRUE},  \code{FALSE} or
25543 hornik 53
    \code{TRUE} is returned to indicate success.}
2802 maechler 54
  \item{warn.conflicts}{logical.  If \code{TRUE}, warnings are
5318 ripley 55
    printed about \code{\link{conflicts}} from attaching the new
58654 ripley 56
    package.  A conflict is a function masking a function,
76248 luke 57
    or a non-function masking a non-function. The default is \code{TRUE}
58
    unless specified as \code{FALSE} in the \code{conflicts.policy} option.
36671 ripley 59
  }
16670 hornik 60
  \item{verbose}{a logical.  If \code{TRUE}, additional diagnostics are
21789 ripley 61
    printed.}
62
  \item{quietly}{a logical.  If \code{TRUE}, no message confirming
63986 ripley 63
    package attaching is printed, and most often, no errors/warnings are
64
    printed if package attaching fails.}
76248 luke 65
  \item{pkg}{character string naming a package.}
66
  \item{mask.ok}{character vector of names of objects that can mask
67
    objects on the search path without signaling an error when strict
68
    conflict checking is enabled}
69
  \item{exclude,include.only}{character vector of names of objects to
76268 hornik 70
    exclude or include in the attached frame. Only one of these arguments
76248 luke 71
    may be used in a call to \code{library} or \code{require}.}
72
  \item{attach.required}{logical specifying whether required packages
73
    listed in the \code{Depends} clause of the \code{DESCRIPTION} file
74
    should be attached automatically.}
2 r 75
}
3076 pd 76
\details{
7779 hornik 77
  \code{library(package)} and \code{require(package)} both load the
63986 ripley 78
  namespace of the package with name \code{package} and attach it on the
79
  search list.  \code{require} is designed for use inside other
80
  functions; it returns \code{FALSE} and gives a warning (rather than an
81
  error as \code{library()} does by default) if the package does not
82
  exist.  Both functions check and update the list of currently attached
83
  packages and do not reload a namespace which is already loaded.  (If
84
  you want to reload such a package, call \code{\link{detach}(unload =
85
  TRUE)} or \code{\link{unloadNamespace}} first.)  If you want to load a
86
  package without attaching it on the search list, see
87
  \code{\link{requireNamespace}}.
2 r 88
 
41377 ripley 89
  To suppress messages during the loading of packages use
90
  \code{\link{suppressPackageStartupMessages}}: this will suppress all
91
  messages from \R itself but not necessarily all those from package
92
  authors.
61433 ripley 93
 
7418 maechler 94
  If \code{library} is called with no \code{package} or \code{help}
16670 hornik 95
  argument, it lists all available packages in the libraries specified
96
  by \code{lib.loc}, and returns the corresponding information in an
65216 ripley 97
  object of class \code{"libraryIQR"}.  (The structure of this class may
98
  change in future versions.)  Use \code{.packages(all = TRUE)} to
99
  obtain just the names of all available packages, and
100
  \code{\link{installed.packages}()} for even more information.
3076 pd 101
 
17778 hornik 102
  \code{library(help = somename)} computes basic information about the
65216 ripley 103
  package \pkg{somename}, and returns this in an object of class
104
  \code{"packageInfo"}.  (The structure of this class may change in
105
  future versions.)  When used with the default value (\code{NULL}) for
56177 ripley 106
  \code{lib.loc}, the attached packages are searched before the libraries.
75 hornik 107
}
2 r 108
\value{
56177 ripley 109
  Normally \code{library} returns (invisibly) the list of attached
47531 ripley 110
  packages, but \code{TRUE} or \code{FALSE} if \code{logical.return} is
111
  \code{TRUE}.  When called as \code{library()} it returns an object of
112
  class \code{"libraryIQR"}, and for \code{library(help=)}, one of
113
  class \code{"packageInfo"}.
114
 
48873 murdoch 115
  \code{require} returns (invisibly) a logical indicating whether the required
57520 ripley 116
  package is available.
2 r 117
}
76248 luke 118
 
119
\section{Conflicts}{
120
  Handling of conflicts depends on the setting of the
121
  \code{conflicts.policy} option. If this option is not set, then
122
  conflicts result in warning messages if the argument
123
  \code{warn.conflicts} is \code{TRUE}. If the option is set to the
124
  character string \code{"strict"}, then all unresolved conflicts signal
125
  errors.  Conflicts can be resolved using the \code{mask.ok},
76268 hornik 126
  \code{exclude}, and \code{include.only} arguments to \code{library} and
76248 luke 127
  \code{require}. Defaults for \code{mask.ok} and \code{exclude} can be
128
  specified using \code{conflictRules}.
129
 
130
  If the \code{conflicts.policy} option is set to the string
131
  \code{"depends.ok"} then conflicts resulting from attaching declared
132
  dependencies will not produce errors, but other conflicts will.
133
  This is likely to be the best setting for most users wanting some
134
  additional protection against unexpected conflicts.
135
 
136
  The policy can be tuned further by specifying the
137
  \code{conflicts.policy} option as a named list with the following
138
  fields:
139
  \describe{
140
    \item{\code{error}:}{logical; if \code{TRUE} treat unresolved
141
      conflicts as errors.}
142
    \item{\code{warn}:}{logical; unless \code{FALSE} issue a warning
143
      message when conflicts are found.}
144
    \item{\code{generics.ok}:}{logical; if \code{TRUE} ignore conflicts
145
      created by defining S4 generics for functions on the search path.}
146
    \item{\code{depends.ok}:}{logical; if \code{TRUE} do not treat
147
      conflicts with required packages as errors.}
76268 hornik 148
    \item{\code{can.mask}:}{character vector of names of packages that
76248 luke 149
      are allowed to be masked. These would typically be base packages
150
      attached by default.}
151
  }
152
}
153
 
50977 ripley 154
\section{Licenses}{
57520 ripley 155
  Some packages have restrictive licenses, and there is a mechanism to
156
  allow users to be aware of such licenses.  If
50977 ripley 157
  \code{\link{getOption}("checkPackageLicense") == TRUE}, then at first
158
  use of a package with a not-known-to-be-FOSS (see below) license the
159
  user is asked to view and accept the license: a list of accepted
160
  licenses is stored in file \file{~/.R/licensed}.  In a non-interactive
161
  session it is an error to use such a package whose license has not
71430 ripley 162
  already been recorded as accepted.
50977 ripley 163
 
71430 ripley 164
  As from \R 3.4.0 the license check is done when the namespace is loaded.
165
 
69446 ripley 166
  Free or Open Source Software (FOSS,
167
  e.g.\sspace{}\url{https://en.wikipedia.org/wiki/FOSS}) packages are
168
  determined by the same filters used by
169
  \code{\link{available.packages}} but applied to just the current
170
  package, not its dependencies.
50991 ripley 171
 
172
  There can also be a site-wide file \file{R_HOME/etc/licensed.site} of
173
  packages (one per line).
50977 ripley 174
}
23111 ripley 175
\section{Formal methods}{
25118 hornik 176
  \code{library} takes some further actions when package \pkg{methods}
23111 ripley 177
  is attached (as it is by default).  Packages may define formal generic
178
  functions as well as re-defining functions in other packages (notably
25118 hornik 179
  \pkg{base}) to be generic, and this information is cached whenever
63986 ripley 180
  such a namespace is loaded after \pkg{methods} and re-defined functions
43625 ripley 181
  (\link{implicit generic}s) are excluded from the list of conflicts.
63986 ripley 182
  The caching and check for conflicts require looking for a pattern of
43625 ripley 183
  objects; the search may be avoided by defining an object
63986 ripley 184
  \code{.noGenerics} (with any value) in the namespace.  Naturally, if the
43625 ripley 185
  package \emph{does} have any such methods, this will prevent them from
186
  being used.
23111 ripley 187
}
20191 ripley 188
\note{
63986 ripley 189
  \code{library} and \code{require} can only load/attach an
190
  \emph{installed} package, and this is detected by having a
191
  \file{DESCRIPTION} file containing a \samp{Built:} field.
20264 ripley 192
 
193
  Under Unix-alikes, the code checks that the package was installed
26663 ripley 194
  under a similar operating system as given by \code{R.version$platform}
195
  (the canonical name of the platform under which R was compiled),
196
  provided it contains compiled code.  Packages which do not contain
197
  compiled code can be shared between Unix-alikes, but not to other OSes
198
  because of potential problems with line endings and OS-specific help
43292 ripley 199
  files.  If sub-architectures are used, the OS similarity is not
69446 ripley 200
  checked since the OS used to build may differ
201
  (e.g.\sspace{}\code{i386-pc-linux-gnu} code can be built on an
43292 ripley 202
  \code{x86_64-unknown-linux-gnu} OS).
24105 ripley 203
 
38402 ripley 204
  The package name given to \code{library} and \code{require} must match
38577 hornik 205
  the name given in the package's \file{DESCRIPTION} file exactly, even
62602 ripley 206
  on case-insensitive file systems such as are common on Windows and
70779 ripley 207
  macOS.
20191 ripley 208
}
2 r 209
\seealso{
30722 ripley 210
  \code{\link{.libPaths}}, \code{\link{.packages}}.
211
 
9216 maechler 212
  \code{\link{attach}}, \code{\link{detach}}, \code{\link{search}},
76248 luke 213
  \code{\link{objects}}, \code{\link{autoload}},
63922 murdoch 214
  \code{\link{requireNamespace}},
26532 maechler 215
  \code{\link{library.dynam}}, \code{\link{data}},
30461 ripley 216
  \code{\link{install.packages}} and
217
  \code{\link{installed.packages}};
218
  \code{\link{INSTALL}}, \code{\link{REMOVE}}.
76248 luke 219
 
56177 ripley 220
  The initial set of packages attached is set by
51681 ripley 221
  \code{\link{options}(defaultPackages=)}: see also \code{\link{Startup}}.
2 r 222
}
24300 ripley 223
\references{
224
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
225
  \emph{The New S Language}.
47262 ripley 226
  Wadsworth & Brooks/Cole.
24300 ripley 227
}
2 r 228
\examples{
7313 ripley 229
library()                   # list all available packages
41621 ripley 230
library(lib.loc = .Library) # list all packages in the default library
51741 ripley 231
\donttest{library(help = splines)     # documentation on package 'splines'}
63986 ripley 232
library(splines)            # attach package 'splines'
29021 ripley 233
require(splines)            # the same
30722 ripley 234
search()                    # "splines", too
29021 ripley 235
detach("package:splines")
17614 ripley 236
 
237
# if the package name is in a character vector, use
29021 ripley 238
pkg <- "splines"
17614 ripley 239
library(pkg, character.only = TRUE)
61150 ripley 240
detach(pos = match(paste("package", pkg, sep = ":"), search()))
17614 ripley 241
 
21405 rgentlem 242
require(pkg, character.only = TRUE)
61150 ripley 243
detach(pos = match(paste("package", pkg, sep = ":"), search()))
21405 rgentlem 244
 
7313 ripley 245
require(nonexistent)        # FALSE
25543 hornik 246
\dontrun{
24442 ripley 247
## if you want to mask as little as possible, use
248
library(mypkg, pos = "package:base")
8219 ripley 249
}}
286 maechler 250
\keyword{data}