Inheritance and R expressions"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Defaults and inheritance

The default configuration provides a set of values to use when no named configuration is active. Other configurations automatically inherit all default values so need only define values specialized for that configuration.

For example, in this configuration the production configuration doesn't specify a value for trials so it will be read from the default configuration:

```{yaml output.var="config_yaml"} default: trials: 5 dataset: "data-sampled.csv"

production: dataset: "data.csv"

```r
config::get(config = "production")

All configurations automatically inherit from the default configuration. Configurations can also inherit from one or more other named configurations. For example, in this file the production configuration inherits from the test configuration:

```{yaml, output.var = "config_yaml"} default: trials: 5 dataset: "data-sampled.csv"

test: trials: 30 dataset: "data-test.csv"

production: inherits: test dataset: "data.csv"

```r
config::get(config = "production")

Using R code inside the yaml file

You can execute R code within configuration files by prefacing values with !expr. This could be useful in cases where you want to base configuration values on environment variables, R options, or even other config files. For example:

```{yaml, output.var = "config_yaml"} default: cores: 2 debug: true server: "localhost:5555"

production: cores: !expr getOption("mc.cores") debug: !expr Sys.getenv("ENABLE_DEBUG") == "1" server: !expr config::get("server", file = "etc/server-config.yml")

The default result:

```r
config::get("server")

The production result will depend on the environment variables on the server:

cat(readLines("etc/server-config.yml"), sep = "\n")
config::get("server", config = "production")

Referencing previously assigned parameters

You can use any previously assigned parameter inside of R code so long as it is assigned directly.

```{yaml, output.var = "config_yaml"} default: file: "data.csv"

test: data_dir: "test/out" dataset: !expr file.path(data_dir, file)

production: data_dir: "production/out" dataset: !expr file.path(data_dir, file)

```r
config::get("dataset", config = "test")
config::get(config = "production")

Limitations of using R expressions

The ability to use R expressions gives substantial flexibility to allow configurations to depend on environment variables, files and other information that's available in the target environment.

However, keep in mind these limitations:

If the config file violates these limitations, config::get() will throw an error.

As an example, let's say you want to construct a configuration that computes a location based on a filename and a folder.

A valid example

If both filename and folder are constant (i.e. not computed expressions) this works:

```{yaml, output.var = "config_yaml"} default: filename: "trials.csv" folder: "some/path" location: !expr file.path(folder, filename)

```r
config::get("location")

An invalid example

However, if folder is also a computed value, then location can not be computed and this throws an error:

```{yaml, output.var = "config_yaml"} default: filename: "trials.csv" folder: !expr c("some/path") location: !expr file.path(folder, filename)

```r
config::get("location")


Try the config package in your browser

Any scripts or data that you put into this service are public.

config documentation built on Aug. 30, 2023, 5:16 p.m.