Debian Codegraph @

R/standalone-defer.R

raw
1 # ---
2 # repo: r-lib/withr
3 # file: standalone-defer.R
4 # last-updated: 2024-01-15
5 # license: https://unlicense.org
6 # ---
7 #
8 # `defer()` is similar to `on.exit()` but with a better default for
9 # `add` (hardcoded to `TRUE`) and `after` (`FALSE` by default).
10 # It also supports adding handlers to other frames which is useful
11 # to implement `local_` functions.
12 #
13 #
14 # ## Changelog
15 #
16 # 2024-01-15:
17 # * Rewritten to be pure base R.
18 #
19 # nocov start
20
21 defer <- function(expr, envir = parent.frame(), after = FALSE) {
22 thunk <- as.call(list(function() expr))
23 do.call(
24 on.exit,
25 list(thunk, add = TRUE, after = after),
26 envir = envir
27 )
28 }
29
30 # nocov end