Skip to content

Configurations

Ben Tupper edited this page Feb 2, 2021 · 1 revision

Configuration

We use a simple and widely used configuration file format called yaml. We rely on the yaml R package for input and output. Once you are within R, a configuration is just a list, possibly nested, where every element in named.

We provide an example that you can read and write.

cfg <- charlier::read_config(example = TRUE, autopopulate = FALSE)
#List of 2
# $ global :List of 2
#  ..$ foo  : num 1
#  ..$ paths:List of 2
#  .. ..$ input_path : chr "/my/input_path"
#  .. ..$ output_path: chr "/my/input_path/output"
# $ process:List of 4
#  ..$ in : chr "$GLOBAL_INPUT_PATH"
#  ..$ out: chr "$GLOBAL_OUTPUT_PATH"
#  ..$ pi : num 3.14
#  ..$ bar: chr "$GLOBAL_FOO"

charlier::write_config(cfg, "~/my_very_own_config.yaml")

Depending upon your needs, you may discover that some values in the configuration file are repeated. Given that yaml files are often hand crafted that can lead to keystroke and other user entry errors. These errors most often happen when working with file and directory path descriptions. We implement an optional 'autopopulate' feature so that you can write into a particular section a set of global values that are automatically sprinkled through the remainder of the configuration file where appropriate.

The example configuration looks like this in the file. Note the presence of value placeholder in the style of $NAME1_NAME2... such as $GLOBAL_PATHS_INPUT_PATH and $GLOBAL_FOO.

global:
  foo: 1.0
  paths:
    input_path: /my/input_path
    output_path: /my/input_path/output
process:
  in: $GLOBAL_PATHS_INPUT_PATH
  out: $GLOBAL_PATHS_OUTPUT_PATH
  pi: 3.14
  bar: $GLOBAL_FOO

We can run autopopulation if are explicit about the names we want to replace. The output shows that placeholder values have been filled with elements from the global section.

y <- charlier::autopopulate_config(x)
# List of 2
#  $ global :List of 2
#   ..$ foo  : num 1
#   ..$ paths:List of 2
#   .. ..$ input_path : chr "/my/input_path"
#   .. ..$ output_path: chr "/my/input_path/output"
#  $ process:List of 4
#   ..$ in : chr "/my/input_path"
#   ..$ out: chr "/my/input_path/output"
#   ..$ pi : num 3.14
#   ..$ bar: int 1

Or, you can read and autopopulate in one go.

cfg <- charlier::read_config(example = TRUE, autopopulate = FALSE)

Clone this wiki locally