Skip to contents

Metayer contains useful utility methods:

env

## env_stack - inspect a chain of environments
env_stack(global_env())
 [[1]] $ <env: global>
 [[2]] $ <env: package:rlang>
 [[3]] $ <env: package:magrittr>
 [[4]] $ <env: package:stats>
 [[5]] $ <env: package:graphics>
 [[6]] $ <env: package:grDevices>
 [[7]] $ <env: package:utils>
 [[8]] $ <env: package:datasets>
 [[9]] $ <env: devtools_shims>
[[10]] $ <env: package:metayer>
[[11]] $ <env: package:testthat>
[[12]] $ <env: package:methods>
[[13]] $ <env: Autoloads>
[[14]] $ <env: tools:callr>
[[15]] $ <env: package:base>
[[16]] $ <env: empty>
## env_stack - also works for function execution stacks / dynamic scoping
env_stack(cli_alert, last = global_env())
[[1]] $ <env: wrapped-0x64ccb28f5988>
[[2]] $ <env: namespace:metayer>
[[3]] $ <env: imports:metayer>
[[4]] $ <env: namespace:base>
[[5]] $ <env: global>
## env_rename - create an environment and give it a useful name
env_rename(new_environment(), "fooenv") %>%
  env_stack()
[[1]] $ <env: fooenv>
[[2]] $ <env: empty>

cli

## recover a message from a cli_message

seconds = 3
tryCatch(
  cli_alert("aborting in {seconds} seconds: {NULL}"),
  message = function(msg) {
    conditionMessage(msg)
  }
)
[INFO/global] > aborting in 3 seconds: <null> 
[1] "> aborting in 3 seconds: <null>\n"

other

## update_list - recursive update of lists; the second list replaces any 
## item that it shared with the first list
original <- list(a = 1, b = list(b1 = 4, b2 = 5), c = 3)
refresh <- list(b = list(b2 = 6), c = 8)
update_list(original, refresh)
$a
[1] 1

$b
$b$b1
[1] 4

$b$b2
[1] 6


$c
[1] 8
## mty_uuid - get a uuid
mty_uuid()
[1] "d5ca88e6-6c053-39b1e-e61fe-e3591b5ed9295"
## mty_uuid - use the uuid.generator and uuid.salt options to get reproducible behavior
uuid <- function(salt = NULL) {
  withr::with_options(
    list(
      uuid.generator = test_mty_uuid
    ),
    {
      mty_uuid(salt)
    }
  )
}

u1 <- uuid(12345)
u2 <- uuid()
u3 <- uuid()

u4 <- uuid(12345)
u5 <- uuid()

# check that reseeding restarts the sequence
stopifnot(identical(u1, u4))
stopifnot(identical(u2, u5))
u1
[1] "615618d7-736be-e2d97-7e23a-a492cd8334ab3"

rm.all

Remove everything not on the whitelist; including dotted variables, e.g. “.storage”.

## show the variables in the global environment
ls(all.names = TRUE)
 [1] ".First"       ".Random.seed" ".storage"     "original"     "refresh"     
 [6] "seconds"      "u1"           "u2"           "u3"           "u4"          
[11] "u5"           "uuid"         "workflow"    
## rm.all - everything but 'u2' and 'uuid'
rm.all(exclusions = c("u2", "uuid"))
ls(all.names = TRUE)
[1] "u2"   "uuid"