Skip to content

Commit

Permalink
Move package load injection to on_load()
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Jul 31, 2024
1 parent 0242c0a commit 6d79379
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 46 deletions.
31 changes: 2 additions & 29 deletions R/aaa.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
# From https://github.com/r-lib/rlang/blob/main/R/aaa.R

on_load <- function(expr, env = parent.frame(), ns = topenv(env)) {
expr <- substitute(expr)
force(env)

callback <- function() {
# Evaluate with promise semantics rather than `base::eval()`
do <- NULL
do.call(delayedAssign, list("do", expr, env))
do
}
ns$.__rlang_hook__. <- c(ns$.__rlang_hook__., list(callback))
}

run_on_load <- function(ns = topenv(parent.frame())) {
hook <- ns$.__rlang_hook__.
rm(envir = ns, list = ".__rlang_hook__.")

# FIXME: Transform to `while` loop to allow hooking into on-load
# from an on-load hook?
for (callback in hook) {
callback()
}
}
the <- new.env()

.onLoad <- function(...) {
run_on_load()
}

the <- new.env()
# From https://github.com/r-lib/rlang/blob/main/R/aaa.R

on_package_load <- function(pkg, expr, env = parent.frame()) {
expr <- substitute(expr)
Expand Down
15 changes: 0 additions & 15 deletions R/defer.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,3 @@ defer_knitr_run <- function(
}
})
}


# Augment rlang with withr features such as knitr support
on_load({
on_package_load("rlang", local({
if (is.null(getOption("withr:::inject_defer_override"))) {
ns <- asNamespace("rlang")

do.call("unlockBinding", list("defer", ns))
defer(lockBinding("defer", ns))

ns$defer <- defer
}
}))
})
15 changes: 13 additions & 2 deletions R/with.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,16 @@
#' Sys.getenv("WITHR")
"_PACKAGE"

# Enable pkgload to hotpatch `::` in detached namespaces
on_load(`::` <- base::`::`)
.onLoad <- function(...) {
# Augment rlang with withr features such as knitr support
on_package_load("rlang", local({
if (is.null(getOption("withr:::inject_defer_override"))) {
ns <- asNamespace("rlang")

do.call("unlockBinding", list("defer", ns))
defer(lockBinding("defer", ns))

ns$defer <- defer
}
}))
}

0 comments on commit 6d79379

Please sign in to comment.