Retrieves a package configuration value in a canonical way. The following configuration sources are consulted in descending order and the first hit is returned:
The R option
<pkg>.<key>.The environment variable
R_<PKG>_<KEY>.The ad-hoc default value specified via this function's
defaultargument (NULLmeans unspecified).The configuration's global default value as specified in the package's funky configuration data (column
default_valueordefault_value_dynamicof<pkg>:::funky_config;NULLmeans unspecified).
Depending on require, an error is thrown if none of the above sources contain a value.
Usage
config_val(
key,
pkg = utils::packageName(env = parent.frame()),
default = NULL,
require = NULL,
env = parent.frame()
)Arguments
- key
Configuration key name. A character scalar.
- pkg
R package name. A character scalar. Defaults to the name of the calling package.
- default
Default value to fall back to if neither the R option
<pkg>.<key>nor the environment variableR_<PKG>_<KEY>is set. IfNULL, the default value forkeyin<pkg>:::funky_configwill be used (if defined).- require
Whether or not to require that the configuration value is set. If
TRUEand no configuration value is set, an error is thrown with instructions on how to provide a value. IfNULL, therequirevalue forkeyin<pkg>:::funky_configwill be used (defaults toTRUE).- env
Environment to evaluate
default_value_dynamicin, if necessary.
Details
This function is intended to be used by package authors who want to expose their package configuration options in a canonical way (as outlined above). For
config_val() to properly work, the configuration data must be available in the package's namespace as object funky_config, which must be a
dataframe with at minimum the columns key (of type character holding the configuration key names) and default_value (of type list
holding static default configuration values) or default_value_dynamic (of type character holding R code expressions that evaluate to default configuration
values dynamically at access time).
See also
xfun::env_option() for a compatible (albeit less powerful) approach to R option and environment variable coherence.
Other package configuration value functions:
config_val_default(),
has_config_val()
Examples
try(
funky::config_val(key = "gen_pkgdown_ref",
pkg = "pkgpurl")
)
#> [1] TRUE