Debian Codegraph @

R/import-standalone-defer.R

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