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 forenvvar_get_datetime()
Examples
# Get and use an integer value
envvar_set("MYNUMBER" = 12)
envvar_get_integer("MYNUMBER") + 5
#> [1] 17
# Get and use a numeric value
envvar_set("MYNUMBER" = 12.34)
envvar_get_numeric("MYNUMBER") + 5
#> [1] 17.34
# Check a logical value
isTRUE(envvar_get_logical("RSTUDIO_CLI_HYPERLINKS", default = FALSE))
#> ℹ Environment variable `RSTUDIO_CLI_HYPERLINKS` is not set. Using default value FALSE.
#> [1] 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"))
#> [1] ‘4.3.1’
# Get a date and make sure it's in the past
envvar_set("LAUNCH_DATE" = "2023-03-03")
envvar_get_date("LAUNCH_DATE", validate = \(x) x < Sys.Date())
#> [1] "2023-03-03"