Skip to contents

envvar_get() returns the value of an environment variable.

envvar_get_oneof() gets the value of an environment variable and ensures that it is within a defined set of potential values.

Usage

envvar_get(
  x,
  default = NULL,
  transform = NULL,
  validate = NULL,
  warn_default = TRUE
)

envvar_get_oneof(
  x,
  choices,
  default = NULL,
  transform = NULL,
  validate = NULL,
  warn_default = TRUE
)

Arguments

x

String containing an environment variable name

default

Optional default value if the environment variable is not set

transform

Optional function that applies a transformation to the variable's value

validate

Optional function that checks a value for validity

warn_default

Show a warning if the default value is used (default: TRUE)

choices

A list containing the potential values

Value

The value of the given environment variable, if set. This is a string unless a transform function has changed the object's type.

Details

envvar_get() and the other type-specific variants follow this workflow:

  • The value of the environment variable is retrieved. If that variable is not set and default non-NULL, the default value is used. Otherwise an error is raised.

  • Optionally, the value can be transformed by a function specified by the transform argument. Transformation functions return a scalar object.

  • Optionally, the value can be validated by a function specified by the validate argument. Validation functions return a logical value indicating whether or not the value matches the given criteria. An error is raised if the validation function does not return TRUE.

  • The transformed, validated value is returned.

Examples

# Get the current user's home directory
envvar_get("HOME")
#> [1] "/home/runner"

# Get the current logging level for your app, ensuring it is a known value
envvar_set("LOG_LEVEL" = "DEBUG")
envvar_get_oneof(
  "LOG_LEVEL",
  choices = c("TRACE", "DEBUG", "INFO", "SUCCESS", "WARN", "ERROR", "FATAL")
)
#> [1] "DEBUG"