Skip to contents

Autos need to immediately follow the global environment. This wrapper around base::library() will reset the autos after each new library is attached to ensure this behavior is followed.

Arguments

...

pass directly through to base::library

Value

returns (invisibly) the list of attached packages

Examples

# Simple example
library(purrr)

# Illustrative example to show that autos will always remain above attached libraries
tmpdir <- tempdir()
print(tmpdir)
#> [1] "/tmp/RtmpKPwpGu"

# 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/RtmpKPwpGu/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/RtmpKPwpGu/demo/PROD/project1/functions to autos:my_functions.PROD
#> Attaching functions from /tmp/RtmpKPwpGu/demo/DEV/username/project1/functions to autos:my_functions.DEV

# show search
search()
#>  [1] ".GlobalEnv"              "autos:my_functions.DEV" 
#>  [3] "autos:my_functions.PROD" "package:purrr"          
#>  [5] "package:envsetup"        "envsetup:paths"         
#>  [7] "package:stats"           "package:graphics"       
#>  [9] "package:grDevices"       "package:utils"          
#> [11] "package:datasets"        "package:methods"        
#> [13] "Autoloads"               "package:base"           

# now attach purrr
library(purrr)

# see autos are still above purrr in the search path
search()
#>  [1] ".GlobalEnv"              "autos:my_functions.DEV" 
#>  [3] "autos:my_functions.PROD" "package:purrr"          
#>  [5] "package:envsetup"        "envsetup:paths"         
#>  [7] "package:stats"           "package:graphics"       
#>  [9] "package:grDevices"       "package:utils"          
#> [11] "package:datasets"        "package:methods"        
#> [13] "Autoloads"               "package:base"