rconfig {rconfig} | R Documentation |
Manage R Configuration at the Command Line
Description
Manage R configuration using files (YAML, JSON, INI, TXT) JSON strings and command line arguments. Command line arguments can be used to provide commands and to override configuration. Period-separated command line flags are parsed as hierarchical lists.
Usage
rconfig(
file = NULL,
list = NULL,
eval = NULL,
flatten = NULL,
debug = NULL,
sep = NULL,
sub = NULL,
...
)
value(x, ...)
## Default S3 method:
value(x, default = NULL, coerce = TRUE, ...)
command(x, ...)
## Default S3 method:
command(x, ...)
Arguments
file |
Configuration file name or URL ( |
list |
A list to override other configs ( |
eval |
Logical, evaluate |
flatten |
Logical, should config contain nested lists or should
results be flat, i.e. |
debug |
Logical, when debug mode is on the configuration
source information are attached as the |
sep |
Character, separator for text files. |
sub |
Logical, substitute environment variables (see Details). |
... |
Other arguments passed to file parsers:
|
x |
A list, e.g. the |
default |
A default value to be used when a configuration entry is not set. |
coerce |
Logical, should values of |
Details
Merges configuration after parsing files, JSON strings,
and command line arguments. Note that rconfig only considers
trailing command line arguments from Rscript.
rconfig differentiates verb/noun syntax, where
verbs are sub-commands following the R script file name
and preceding the command line flags (starting with -
or --
).
Configurations are merged in the following order
(key-values from last element override previous values for the same key):
-
R_RCONFIG_FILE
value or"rconfig.yml"
from working directory JSON strings (following
-j
and--json
flags) and files (following-f
and--file
flags) provided as command line arguments are parsed and applied in the order they appear (key-value pairs are separated by space, only atomic values considered, i.e. file name or string) for each flag, but multiple file/JSON flags are accepted in sequencethe remaining other command line arguments, that can be sub-commands or command line flags (starting with
-
or--
), period-separated command line flags are parsed as hierarchical lists (key-value pairs are separated by space, flags must begin with--
, values are treated as vectors when contain spaces, i.e.--key 1 2 3
)configuration from the
file
argument (one or multiple files, parsed and applied in the order they appear)configuration from the
list
argument
The following environment variables and options can be set to modify the default behavior:
-
R_RCONFIG_FILE
: location of the default configuration file, it is assumed to berconfig.yml
in the current working directory. The file name can be an URL or it can can be missing. -
R_RCONFIG_EVAL
: coerced to logical, indicating whether R expressions starting with!expr
should be evaluated in the namespace environment for the base package (overrides the value ofgetOption("rconfig.eval")
). When not set the value assumed isTRUE
. -
R_RCONFIG_SUB
: coerced to logical, indicating whether environment variables should be substituted (overrides the value ofgetOption("rconfig.sub")
). When not set the value assumed isTRUE
. -
R_RCONFIG_FLATTEN
: coerced to logical, flatten nested lists, i.e.a$b$c
becomes the keya.b.c
(overrides the value ofgetOption("rconfig.flatten")
). When not set the value assumed isFALSE
. -
R_RCONFIG_DEBUG
: coerced to logical, to turn on debug mode (overrides the value ofgetOption("rconfig.debug")
). When not set the value assumed isFALSE
. -
R_RCONFIG_SEP
: separator for text file parser, (overrides the value ofgetOption("rconfig.sep")
). When not set the value assumed is"="
.
When the configuration is a file (file name can also be a URL),
it can be nested structure in JSON or YAML format.
Other text files are parsed using the
separator (R_RCONFIG_SEP
or getOption("rconfig.sep")
) and
period-separated keys are parsed as hierarchical lists
(i.e. a.b.c=12
is treated as a$b$c = 12
) by default.
When the configuration is a file or a JSON string,
values starting with !expr
will be evaluated depending on the
settings R_RCONFIG_EVAL
and getOption("rconfig.eval")
.
E.g. cores: !expr getOption("mc.cores")
, etc.
The rconfig package interprets 3 kinds of substitution patterns:
environment variables (
${VALUE}
): these variables are already present when the configurations is read from the calling environment or from.Renviron
file in the project specific or home folder, set variables can be null or not-nullR global variables (
@{VALUE}
): the rconfig package looks for variables in the global environment at the time of configuration evaluation, however, expressions are not evaluated (unlike the!expr
option for values)configuration values (
#{VALUE}
): the configuration level variables are evaluated last, thus these values can refer to existing keys that are already substituted
For additional details see the package website at https://github.com/analythium/rconfig.
Value
The configuration value (a named list, or an empty list).
When debug mode is on, the "trace"
attribute traces the
merged configurations. The value()
method returns the value
of a given argument or the default value when it is not found
(i.e. NULL
). The command()
method returns a character vector
with command line sub-commands (can be of length 0).
See Also
Examples
cfile <- function(file) {
system.file("examples", file, package = "rconfig")
}
rconfig::rconfig()
rconfig::rconfig(
file = cfile("rconfig.yml"))
rconfig::rconfig(
file = c(cfile("rconfig.json"),
cfile("rconfig-prod.txt")),
list = list(user = list(name = "Jack")))
rconfig::rconfig(
file = c(cfile("rconfig.json"),
cfile("rconfig-prod.txt")),
list = list(user = list(name = "Jack")),
flatten = TRUE)
CONFIG <- rconfig::rconfig(
file = cfile("rconfig.yml"))
value(CONFIG$cores, 2L) # set to 1L
value(CONFIG$test, FALSE) # unset