| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/packages.R |
| 3 |
\name{pkg_attach} |
| 4 |
\alias{pkg_attach} |
| 5 |
\alias{pkg_load} |
| 6 |
\alias{loadable} |
| 7 |
\alias{pkg_available} |
| 8 |
\alias{pkg_attach2} |
| 9 |
\alias{pkg_load2} |
| 10 |
\title{Attach or load packages, and automatically install missing packages if |
| 11 |
requested} |
| 12 |
\usage{ |
| 13 |
pkg_attach( |
| 14 |
..., |
| 15 |
install = FALSE, |
| 16 |
message = getOption("xfun.pkg_attach.message", TRUE) |
| 17 |
) |
| 18 |
|
| 19 |
pkg_load(..., error = TRUE, install = FALSE) |
| 20 |
|
| 21 |
loadable(pkg, strict = TRUE, new_session = FALSE) |
| 22 |
|
| 23 |
pkg_available(pkg, version = NULL) |
| 24 |
|
| 25 |
pkg_attach2(...) |
| 26 |
|
| 27 |
pkg_load2(...) |
| 28 |
} |
| 29 |
\arguments{ |
| 30 |
\item{...}{Package names (character vectors, and must always be quoted).} |
| 31 |
|
| 32 |
\item{install}{Whether to automatically install packages that are not |
| 33 |
available using \code{\link[=install.packages]{install.packages()}}. Besides \code{TRUE} and |
| 34 |
\code{FALSE}, the value of this argument can also be a function to install |
| 35 |
packages (\code{install = TRUE} is equivalent to \code{install = install.packages}), or a character string \code{"pak"} (equivalent to |
| 36 |
\code{install = pak::pkg_install}, which requires the \pkg{pak} package). |
| 37 |
You are recommended to set a CRAN mirror in the global option \code{repos} |
| 38 |
via \code{\link[=options]{options()}} if you want to automatically install packages.} |
| 39 |
|
| 40 |
\item{message}{Whether to show the package startup messages (if any startup |
| 41 |
messages are provided in a package).} |
| 42 |
|
| 43 |
\item{error}{Whether to signal an error when certain packages cannot be loaded.} |
| 44 |
|
| 45 |
\item{pkg}{A single package name.} |
| 46 |
|
| 47 |
\item{strict}{If \code{TRUE}, use \code{\link[=requireNamespace]{requireNamespace()}} to test if |
| 48 |
a package is loadable; otherwise only check if the package is in |
| 49 |
\code{\link{.packages}}\code{(TRUE)} (this does not really load the package, so it |
| 50 |
is less rigorous but on the other hand, it can keep the current R session |
| 51 |
clean).} |
| 52 |
|
| 53 |
\item{new_session}{Whether to test if a package is loadable in a new R |
| 54 |
session. Note that \code{new_session = TRUE} implies \code{strict = TRUE}.} |
| 55 |
|
| 56 |
\item{version}{A minimal version number. If \code{NULL}, only test if a |
| 57 |
package is available and do not check its version.} |
| 58 |
} |
| 59 |
\value{ |
| 60 |
\code{pkg_attach()} returns \code{NULL} invisibly. \code{pkg_load()} |
| 61 |
returns a logical vector, indicating whether the packages can be loaded. |
| 62 |
} |
| 63 |
\description{ |
| 64 |
\code{pkg_attach()} is a vectorized version of \code{\link[=library]{library()}} over |
| 65 |
the \code{package} argument to attach multiple packages in a single function |
| 66 |
call. \code{pkg_load()} is a vectorized version of |
| 67 |
\code{\link[=requireNamespace]{requireNamespace()}} to load packages (without attaching them). |
| 68 |
The functions \code{pkg_attach2()} and \code{pkg_load2()} are wrappers of |
| 69 |
\code{pkg_attach(install = TRUE)} and \code{pkg_load(install = TRUE)}, |
| 70 |
respectively. \code{loadable()} is an abbreviation of |
| 71 |
\code{requireNamespace(quietly = TRUE)}. \code{pkg_available()} tests if a |
| 72 |
package with a minimal version is available. |
| 73 |
} |
| 74 |
\details{ |
| 75 |
These are convenience functions that aim to solve these common problems: (1) |
| 76 |
We often need to attach or load multiple packages, and it is tedious to type |
| 77 |
several \code{library()} calls; (2) We are likely to want to install the |
| 78 |
packages when attaching/loading them but they have not been installed. |
| 79 |
} |
| 80 |
\examples{ |
| 81 |
library(xfun) |
| 82 |
pkg_attach("stats", "graphics") |
| 83 |
# pkg_attach2('servr') # automatically install servr if it is not installed |
| 84 |
|
| 85 |
(pkg_load("stats", "graphics")) |
| 86 |
} |
| 87 |
\seealso{ |
| 88 |
\code{pkg_attach2()} is similar to \code{pacman::p_load()}, but does |
| 89 |
not allow non-standard evaluation (NSE) of the \code{...} argument, i.e., |
| 90 |
you must pass a real character vector of package names to it, and all names |
| 91 |
must be quoted. Allowing NSE adds too much complexity with too little gain |
| 92 |
(the only gain is that it saves your effort in typing two quotes). |
| 93 |
} |