envvar_get_integer {envvar}R Documentation

Get the value of an environment variable as a particular type

Description

envvar_get_integer() reads environment variables with integer values (e.g., 15, 0, -1)

envvar_get_numeric() reads environment variables with numeric values (e.g., 100.12, -31)

envvar_get_logical() reads environment variables with logical values (e.g., TRUE, 1, "T")

envvar_get_version reads environment variables with semantic version numbers (e.g., ⁠4.3.1⁠)

envvar_get_date() reads environment variables with date values (e.g., "2023-01-02")

envvar_get_date() reads environment variables with date-time values (e.g., "2023-01-02 01:23:45 UTC" or 1697037804)

Usage

envvar_get_integer(x, default = NULL, validate = NULL, warn_default = TRUE)

envvar_get_numeric(x, default = NULL, validate = NULL, warn_default = TRUE)

envvar_get_logical(x, default = NULL, validate = NULL, warn_default = TRUE)

envvar_get_version(x, default = NULL, validate = NULL, warn_default = TRUE)

envvar_get_date(x, default = NULL, validate = NULL, warn_default = TRUE, ...)

envvar_get_datetime(
  x,
  default = 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

validate

Optional function that checks a value for validity

warn_default

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

...

Additional arguments passed to lubridate::as_date for envvar_get_date() or lubridate::as_datetime for envvar_get_datetime()

Value

A scalar object

Examples


# Get and use an integer value
envvar_set("MYNUMBER" = 12)
envvar_get_integer("MYNUMBER") + 5

# Get and use a numeric value
envvar_set("MYNUMBER" = 12.34)
envvar_get_numeric("MYNUMBER") + 5

# Check a logical value
isTRUE(envvar_get_logical("RSTUDIO_CLI_HYPERLINKS", default = FALSE))

# Get an IP address value and ensure that it is IPv4
envvar_set("MY_VER" = "4.3.1")
envvar_get_version("MY_VER", validate = \(x) x > as.numeric_version("4.3"))
# Get a date and make sure it's in the future
envvar_set("LAUNCH_DATE" = "2024-08-08")
envvar_get_date("LAUNCH_DATE", validate = \(x) x > Sys.Date())

[Package envvar version 0.1.1 Index]