Skip to contents

envvar_get_file() gets a file name from an environment variable

envvar_get_dir() gets a directory name from an environment variable

Usage

envvar_get_file(
  x,
  default = NULL,
  create = TRUE,
  check_readable = FALSE,
  check_writable = FALSE,
  transform = NULL,
  warn_default = TRUE,
  ...
)

envvar_get_dir(
  x,
  default = NULL,
  create = TRUE,
  transform = NULL,
  check_readable = FALSE,
  check_writable = FALSE,
  warn_default = TRUE,
  ...
)

Arguments

x

String containing an environment variable name

default

Optional default value if the environment variable is not set

create

Create the file or directory if it does not exist (default: TRUE)

check_readable

Ensure that the file or directory is readable

check_writable

Ensure that the file or directory is writable

transform

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

warn_default

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

...

Additional arguments passed to fs::file_create for envvar_get_file() or fs::dir_create for envvar_get_dir()

Value

A string containing the path to a file or directory

Examples

# \donttest{
# Get a file path and make sure it exists
envvar_set("MY_DATA" = "data.parquet")
envvar_get_file("MY_DATA")
#>  File data.parquet does not exist. Creating.
#> [1] "data.parquet"

# Cleanup
file.remove("data.parquet")
#> [1] TRUE
# }
# \donttest{
envvar_set("MY_DATA_DIR" = "data")
envvar_get_dir("MY_DATA_DIR")
#>  Directory data does not exist. Creating.
#> [1] "data"

# Cleanup
unlink("data", recursive = TRUE)
# }