Skip to contents

This function will remove any autos that have been set from the search path

Usage

detach_autos()

Value

Called for its side-effects.

Examples

tmpdir <- tempdir()
print(tmpdir)
#> [1] "/tmp/Rtmp4W5uPx"

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

# Create an example config file\
hierarchy <- paste0("default:
  paths:
    functions: !expr list(DEV = file.path('",tmpdir,"',
                                          'demo',
                                          'DEV',
                                          'username',
                                          'project1',
                                          'functions'),
                          PROD = file.path('",tmpdir,"',
                                           'demo',
                                           'PROD',
                                           'project1',
                                           'functions'))
  autos:
     my_functions: !expr list(DEV = file.path('",tmpdir,"',
                                              'demo',
                                              'DEV',
                                              'username',
                                              'project1',
                                              'functions'),
                              PROD = file.path('",tmpdir,"',
                                               'demo',
                                               'PROD',
                                               'project1',
                                               'functions'))")

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

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

build_from_config(config)
#>  Directories built
#> /tmp/Rtmp4W5uPx/demo/
#> ├── DEV
#> │   └── username
#> │       └── project1
#> │           ├── data
#> │           ├── functions
#> │           ├── output
#> │           └── programs
#> └── PROD
#>     └── project1
#>         ├── data
#>         ├── functions
#>         ├── output
#>         └── programs

# write function to DEV
writeLines("dev_function <- function() {print(environment(dev_function))}",
           file.path(tmpdir, 'demo', 'DEV', 'username', 'project1', 'functions', 'dev_function.r'))

# write function to PROD
writeLines("prod_function <- function() {print(environment(prod_function))}",
           file.path(tmpdir, 'demo', 'PROD', 'project1', 'functions', 'prod_function.r'))

# setup the environment
Sys.setenv(ENVSETUP_ENVIRON = "DEV")
rprofile(config::get(file = file.path(tmpdir, "hierarchy.yml")))
#> Attaching paths to envsetup:paths
#> Attaching functions from /tmp/Rtmp4W5uPx/demo/PROD/project1/functions to autos:my_functions.PROD
#> Attaching functions from /tmp/Rtmp4W5uPx/demo/DEV/username/project1/functions to autos:my_functions.DEV

# show dev_function() and prod_function() are available and print their location
dev_function()
#> <environment: 0x555b01ab7380>
#> attr(,"name")
#> [1] "autos:my_functions.DEV"
prod_function()
#> <environment: 0x555b01b25bb8>
#> attr(,"name")
#> [1] "autos:my_functions.PROD"

# remove autos from search
detach_autos()