The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
Sometimes data moves between environments during development, or you need to check multiple locations for files. This guide shows you how to set up dynamic path resolution that adapts to your workflow.
Imagine this scenario with our friend Tidy McVerse:
/demo/DEV/username/project1/data
/demo/PROD/project1/data
Configure paths as lists with multiple possible locations:
library(envsetup)
# Create temporary directory structure
dir <- fs::file_temp()
dir.create(dir)
config_path <- file.path(dir, "_envsetup.yml")
# Write configuration with multiple data paths
file_conn <- file(config_path)
writeLines(
paste0(
"default:
paths:
data: !expr list(DEV = '", dir,"/demo/DEV/username/project1/data', PROD = '", dir, "/demo/PROD/project1/data')
output: '", dir, "/demo/DEV/username/project1/output'
programs: '", dir, "/demo/DEV/username/project1/programs'
envsetup_environ: !expr Sys.setenv(ENVSETUP_ENVIRON = 'DEV'); 'DEV'"
), file_conn)
close(file_conn)
# Load and apply configuration
envsetup_config <- config::get(file = config_path)
rprofile(envsetup_config)
#> Assigned paths to R_GlobalEnv
Let’s examine what we now have available:
# See all configured objects
ls(envsetup_environment)
#> [1] "object_metadata"
# Data is now a named list with multiple locations
get_path(data)
#> $DEV
#> [1] "/shared/R_Temp/nmasel/RtmpKbVJua/file1962b12b91affb/demo/DEV/username/project1/data"
#>
#> $PROD
#> [1] "/shared/R_Temp/nmasel/RtmpKbVJua/file1962b12b91affb/demo/PROD/project1/data"
get_path(output)
#> [1] "/shared/R_Temp/nmasel/RtmpKbVJua/file1962b12b91affb/demo/DEV/username/project1/output"
get_path(programs)
#> [1] "/shared/R_Temp/nmasel/RtmpKbVJua/file1962b12b91affb/demo/DEV/username/project1/programs"
get_path(envsetup_environ)
#> [1] "DEV"
The read_path()
function searches through your path list
to find files:
# Create the directory structure
dir.create(file.path(dir, "/demo/DEV/username/project1/data"), recursive = TRUE)
dir.create(file.path(dir, "/demo/PROD/project1/data"), recursive = TRUE)
# Add data only to PROD location
saveRDS(mtcars, file.path(dir, "/demo/PROD/project1/data/mtcars.RDS"))
# read_path() finds the file in PROD
read_path(data, "mtcars.RDS")
#> Read Path:/shared/R_Temp/nmasel/RtmpKbVJua/file1962b12b91affb/demo/PROD/project1/data/mtcars.RDS
#> [1] "/shared/R_Temp/nmasel/RtmpKbVJua/file1962b12b91affb/demo/PROD/project1/data/mtcars.RDS"
When data exists in multiple locations, read_path()
follows the search order:
# Add the same data to DEV location
saveRDS(mtcars, file.path(dir, "/demo/DEV/username/project1/data/mtcars.RDS"))
# Now read_path() returns DEV location (first in search order)
read_path(data, "mtcars.RDS")
#> Read Path:/shared/R_Temp/nmasel/RtmpKbVJua/file1962b12b91affb/demo/DEV/username/project1/data/mtcars.RDS
#> [1] "/shared/R_Temp/nmasel/RtmpKbVJua/file1962b12b91affb/demo/DEV/username/project1/data/mtcars.RDS"
The envsetup_environ
variable controls which paths are
searched:
Let’s add a production configuration that changes the search behavior:
# Update config to include prod environment
file_conn <- file(config_path)
writeLines(
paste0(
"default:
paths:
data: !expr list(DEV = '",dir,"/demo/DEV/username/project1/data', PROD = '",dir,"/demo/PROD/project1/data')
output: '",dir,"/demo/DEV/username/project1/output'
programs: '",dir,"/demo/DEV/username/project1/programs'
envsetup_environ: !expr Sys.setenv(ENVSETUP_ENVIRON = 'DEV'); 'DEV'
prod:
paths:
envsetup_environ: !expr Sys.setenv(ENVSETUP_ENVIRON = 'PROD'); 'PROD'"
), file_conn)
close(file_conn)
# Load production configuration
envsetup_config <- config::get(file = config_path, config = "prod")
rprofile(envsetup_config)
#> Assigned paths to R_GlobalEnv
# Check the environment setting
get_path(envsetup_environ)
#> [1] "PROD"
With the production configuration, path resolution behavior changes:
Here’s how you’d typically use this in your code:
The next guide covers automatic script sourcing, which lets you automatically load custom functions from multiple script libraries across environments.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.