Title: | Manage Environment Specific Configuration Values |
---|---|
Description: | Manage configuration values across multiple environments (e.g. development, test, production). Read values using a function that determines the current environment and returns the appropriate value. |
Authors: | JJ Allaire [aut], Andrie de Vries [cre], Posit Software, PBC [cph, fnd] |
Maintainer: | Andrie de Vries <[email protected]> |
License: | GPL-3 |
Version: | 0.3.2.9000 |
Built: | 2024-10-27 04:19:27 UTC |
Source: | https://github.com/rstudio/config |
config::get()
.Read from the currently active configuration, retrieving either a single named value or all values as a list.
get( value = NULL, config = Sys.getenv("R_CONFIG_ACTIVE", "default"), file = Sys.getenv("R_CONFIG_FILE", "config.yml"), use_parent = TRUE )
get( value = NULL, config = Sys.getenv("R_CONFIG_ACTIVE", "default"), file = Sys.getenv("R_CONFIG_FILE", "config.yml"), use_parent = TRUE )
value |
Name of value ( |
config |
Name of configuration to read from. Defaults to
the value of the |
file |
Configuration file to read from (defaults to
|
use_parent |
|
For additional details see https://rstudio.github.io/config/.
The requested configuration value (or all values as
a list of NULL
is passed for value
).
A list, or vector, corresponding to the contents of the config file.
We strongly recommend you use config::get()
rather than attaching the
package using library(config)
.
In fact, we strongly recommend you never use library(config)
.
The underlying reason is that the get()
and merge()
functions in
{config}
will mask these functions with the same names in base R.
yaml <- " default: trials: 5 dataset: 'data-sampled.csv' production: trials: 30 dataset: 'data.csv' " get <- base::get with_config(yaml, config::get()) with_config(yaml, config::get("trials"))
yaml <- " default: trials: 5 dataset: 'data-sampled.csv' production: trials: 30 dataset: 'data.csv' " get <- base::get with_config(yaml, config::get()) with_config(yaml, config::get("trials"))
Check whether a configuration is currently active.
is_active(config)
is_active(config)
config |
Configuration name |
The name of the currently active configuration is read from the
R_CONFIG_ACTIVE
environment variable. If the variable is not defined then
the "default" configuration is used.
To test for whether a configuration is active you should use the
is_active()
function rather than inspecting the environment variable
directly (this is to so that tests remain valid if other means of specifying
configurations are introduced in the future).
Logical indicating whether the specified configuration is active
config::merge()
.Merge one configuration into another recursively.
merge(base_config, merge_config)
merge(base_config, merge_config)
base_config |
Configuration to merge values into |
merge_config |
Configuration to merge values from |
Configuration which includes the values from
merge_config
merged into base_config
.
We strongly recommend you use config::get()
rather than attaching the
package using library(config)
.
In fact, we strongly recommend you never use library(config)
.
The underlying reason is that the get()
and merge()
functions in
{config}
will mask these functions with the same names in base R.
This function takes inspiration from withr::with_envvar()
and may be useful
for testing purposes.
with_config( config_yml, code, .active_config = c(R_CONFIG_ACTIVE = "default"), .extra_env_vars = NULL )
with_config( config_yml, code, .active_config = c(R_CONFIG_ACTIVE = "default"), .extra_env_vars = NULL )
config_yml |
Either the path to a config file, or a character string representing a yaml configuration. |
code |
Code to execute in a temporary environment. |
.active_config |
Either a string representing a configuration, e.g.
|
.extra_env_vars |
Additional environment variables to set. |
The result of running the code
, after having temporarily set the
necessary environment variables.
yaml <- ' default: db_name: dbase databases: db1: !expr paste0(db_name, "/one") db2: !expr paste0(db_name, "/two") staging: staging_postfix: _staging db_name: dbase databases: db1: !expr paste0(db_name, staging_postfix, "/one") db2: !expr paste0(db_name, staging_postfix, "/two") ' # Ensure that base::get() doesn't get masked, for tests on CRAN get <- base::get with_config(yaml, config::get() ) with_config(yaml, config::get("databases", config = "default") ) with_config(yaml, config::get("databases", config = "staging") ) config_file <- system.file("tests/testthat/config.yml", package = "config") if (file.exists(config_file)) { with_config(config_file, config::get()) }
yaml <- ' default: db_name: dbase databases: db1: !expr paste0(db_name, "/one") db2: !expr paste0(db_name, "/two") staging: staging_postfix: _staging db_name: dbase databases: db1: !expr paste0(db_name, staging_postfix, "/one") db2: !expr paste0(db_name, staging_postfix, "/two") ' # Ensure that base::get() doesn't get masked, for tests on CRAN get <- base::get with_config(yaml, config::get() ) with_config(yaml, config::get("databases", config = "default") ) with_config(yaml, config::get("databases", config = "staging") ) config_file <- system.file("tests/testthat/config.yml", package = "config") if (file.exists(config_file)) { with_config(config_file, config::get()) }