Skip to contents

Paths will be filtered to produce the lowest available level from a hierarchy of paths based on envsetup_environ

Usage

write_path(
  lib,
  filename = NULL,
  envsetup_environ = Sys.getenv("ENVSETUP_ENVIRON")
)

Arguments

lib

Object containing the paths for all environments of a directory

filename

Name of the file you would like to write

envsetup_environ

Name of the environment to which you would like to write. Defaults to the ENVSETUP_ENVIRON environment variable

Value

path to write

Examples

tmpdir <- tempdir()

# account for windows
if (Sys.info()['sysname'] == "Windows") {
  tmpdir <- gsub("\\", "\\\\", tmpdir, fixed = TRUE)
}

# add config for just the data location
hierarchy <- paste0("default:
  paths:
    data: !expr list(
      DEV = file.path('",tmpdir,"', 'demo', 'DEV', 'username', 'project1', 'data'),
      PROD = file.path('",tmpdir,"', 'demo', 'PROD', 'project1', 'data'))")

# write config file to temp directory
writeLines(hierarchy, file.path(tmpdir, "hierarchy.yml"))

config <- config::get(file = file.path(tmpdir, "hierarchy.yml"))

# build folder structure from config
build_from_config(config)
#>  Directories built
#> /tmp/RtmpKPwpGu/demo/
#> ├── DEV
#> │   └── username
#> │       └── project1
#> │           ├── data
#> │           ├── functions
#> │           ├── output
#> │           └── programs
#> └── PROD
#>     └── project1
#>         ├── data
#>         ├── functions
#>         ├── output
#>         └── programs

# setup environment based on config
rprofile(config::get(file = file.path(tmpdir, "hierarchy.yml")))
#> Attaching paths to envsetup:paths

# find location to write mtcars.rds
write_path(data, "mtcars.rds")
#> Write Path:/tmp/RtmpKPwpGu/demo/DEV/username/project1/data/mtcars.rds
#> [1] "/tmp/RtmpKPwpGu/demo/DEV/username/project1/data/mtcars.rds"

# save data in data folder using write_path
saveRDS(mtcars, write_path(data, "mtcars.rds"))
#> Write Path:/tmp/RtmpKPwpGu/demo/DEV/username/project1/data/mtcars.rds