| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/defer.R |
| 3 |
\name{defer} |
| 4 |
\alias{defer} |
| 5 |
\alias{defer_parent} |
| 6 |
\alias{deferred_run} |
| 7 |
\alias{deferred_clear} |
| 8 |
\title{Defer Evaluation of an Expression} |
| 9 |
\usage{ |
| 10 |
defer(expr, envir = parent.frame(), priority = c("first", "last")) |
| 11 |
|
| 12 |
defer_parent(expr, priority = c("first", "last")) |
| 13 |
|
| 14 |
deferred_run(envir = parent.frame()) |
| 15 |
|
| 16 |
deferred_clear(envir = parent.frame()) |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{expr}{\verb{[expression]}\cr An expression to be evaluated.} |
| 20 |
|
| 21 |
\item{envir}{\verb{[environment]}\cr Attach exit handlers to this environment. |
| 22 |
Typically, this should be either the current environment or |
| 23 |
a parent frame (accessed through \code{\link[=parent.frame]{parent.frame()}}).} |
| 24 |
|
| 25 |
\item{priority}{\verb{[character(1)]}\cr Specify whether this handler should |
| 26 |
be executed \code{"first"} or \code{"last"}, relative to any other |
| 27 |
registered handlers on this environment.} |
| 28 |
} |
| 29 |
\description{ |
| 30 |
Similar to \code{\link[=on.exit]{on.exit()}}, but allows one to attach |
| 31 |
an expression to be evaluated when exiting any frame currently |
| 32 |
on the stack. This provides a nice mechanism for scoping side |
| 33 |
effects for the duration of a function's execution. |
| 34 |
} |
| 35 |
\details{ |
| 36 |
\code{defer()} works by attaching handlers to the requested environment (as an |
| 37 |
attribute called \code{"handlers"}), and registering an exit handler that |
| 38 |
executes the registered handler when the function associated with the |
| 39 |
requested environment finishes execution. |
| 40 |
|
| 41 |
Deferred events can be set on the global environment, primarily to facilitate |
| 42 |
the interactive development of code that is intended to be executed inside a |
| 43 |
function or test. A message alerts the user to the fact that an explicit |
| 44 |
\code{deferred_run()} is the only way to trigger these deferred events. Use |
| 45 |
\code{deferred_clear()} to clear them without evaluation. The global environment |
| 46 |
scenario is the main motivation for these functions. |
| 47 |
} |
| 48 |
\section{Running handlers within \code{source()}}{ |
| 49 |
|
| 50 |
withr handlers run within \code{source()} are run when \code{source()} exits |
| 51 |
rather than line by line. |
| 52 |
|
| 53 |
This is only the case when the script is sourced in \code{globalenv()}. |
| 54 |
For a local environment, the caller needs to set |
| 55 |
\code{options(withr.hook_source = TRUE)}. This is to avoid paying the |
| 56 |
penalty of detecting \code{source()} in the normal usage of \code{defer()}. |
| 57 |
} |
| 58 |
|
| 59 |
\examples{ |
| 60 |
# define a 'local' function that creates a file, and |
| 61 |
# removes it when the parent function has finished executing |
| 62 |
local_file <- function(path) { |
| 63 |
file.create(path) |
| 64 |
defer_parent(unlink(path)) |
| 65 |
} |
| 66 |
|
| 67 |
# create tempfile path |
| 68 |
path <- tempfile() |
| 69 |
|
| 70 |
# use 'local_file' in a function |
| 71 |
local({ |
| 72 |
local_file(path) |
| 73 |
stopifnot(file.exists(path)) |
| 74 |
}) |
| 75 |
|
| 76 |
# file is deleted as we leave 'local' local |
| 77 |
stopifnot(!file.exists(path)) |
| 78 |
|
| 79 |
# investigate how 'defer' modifies the |
| 80 |
# executing function's environment |
| 81 |
local({ |
| 82 |
local_file(path) |
| 83 |
print(attributes(environment())) |
| 84 |
}) |
| 85 |
|
| 86 |
# Note that examples lack function scoping so deferred calls are |
| 87 |
# generally executed immediately |
| 88 |
defer(print("one")) |
| 89 |
defer(print("two")) |
| 90 |
} |
| 91 |
\concept{local-related functions} |