Lightweight method to check if an R package is installed
Source:R/jamba-utils.R
check_pkg_installed.Rd
Lightweight method to check if an R package is installed
Usage
check_pkg_installed(x, useMethod = c("packagedir", "requireNamespace"), ...)
Arguments
- x
character
string of package or packages to test.- useMethod
character
default "packagedir" with the method of package confirmation."packagedir" provides a rapid test for the presence of an R package, without loading the package namespace. It tests whether
system.file(package=x)
returns a non-empty value, then 'DESCRIPTION' file exists in the package directory. It answers the question: "Is 'x' package installed?" It does not answer: "Is 'x' package usable in the current R session?" WhenuseMethod
also includes "requireNamespace", for any FALSE result it will also perform a secondary check as well, to confirm the package cannot be loaded by another mechanism."requireNamespace" uses
requireNamespace(x, quietly=TRUE)
, with slight benefit that it accepts multiple values forx
, and returns the result without usinginvisible()
. This method loads the package namespace, but does not attach it. This method therefore takes the same time as loading the package, in return for providing the most accurate answer to the question: "Is 'x' package usable by this R session right now?"
- ...
additional arguments are ignored.
Value
logical
vector indicating whether each value in x
represents an installed R package. The vector is named by
packages provided in x
.
Details
There are many methods to test for an installed package.
Most approaches incur some time or resource penalty, so
check_pkg_installed()
is motivated for rapid results without
loading the package namespace.
This function also accepts multiple values for x
for convenience.
There are two available methods defined by useMethod
:
useMethod="packagedir"
confirms: this function represents possibly the most gentle and rapid approach. It simply callssystem.file(package=x)
, for each entry ofx
, then checks these requirements:Does the package directory exist via
system.file(package=x)
Does the package directory contain the file 'DESCRIPTION'?
It does not check whether the package can be loaded into the current R session.
useMethod="requireNamespace"
confirms:requireNamespace(x, quietly=TRUE)
returns TRUEIt therefore loads the package namespace to confirm, but does not attach the package to the current session. It therefore may take time and resources, despite not altering the R environment search path.
The default behavior first tests by "packagedir", then
for any FALSE
results it also tests "requireNamespace"
.
See also
Other jam practical functions:
breakDensity()
,
call_fn_ellipsis()
,
checkLightMode()
,
colNum2excelName()
,
color_dither()
,
exp2signed()
,
getAxisLabel()
,
isFALSEV()
,
isTRUEV()
,
jargs()
,
kable_coloring()
,
lldf()
,
log2signed()
,
middle()
,
minorLogTicks()
,
newestFile()
,
printDebug()
,
reload_rmarkdown_cache()
,
renameColumn()
,
rmInfinite()
,
rmNA()
,
rmNAs()
,
rmNULL()
,
setPrompt()
Examples
check_pkg_installed("methods")
#> methods
#> TRUE
check_pkg_installed(c("jamba",
"multienrichjam",
"venndir",
"methods",
"blah"))
#> jamba multienrichjam venndir methods blah
#> TRUE TRUE TRUE TRUE FALSE