1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
on_load <- function(expr, env = parent.frame(), ns = topenv(env)) {
expr <- substitute(expr)
force(env)
callback <- function() eval_bare(expr, env)
ns$.__rlang_hook__. <- c(ns$.__rlang_hook__., list(callback))
}
run_on_load <- function(env = parent.frame()) {
ns <- topenv(env)
hook <- ns$.__rlang_hook__.
env_unbind(ns, ".__rlang_hook__.")
# FIXME: Transform to `while` loop to allow hooking into on-load
# from an on-load hook?
for (callback in hook) {
callback()
}
ns$.__rlang_hook__. <- NULL
}
replace_from <- function(pkg, what, to = topenv(caller_env())) {
if (what %in% getNamespaceExports(pkg)) {
env <- ns_env(pkg)
} else {
env <- to
}
env_get(env, what, inherit = TRUE)
}
|