tidy_capture()
and
tidy_dots()
make it easy to record this information
within formulas.
tidy_capture(x)
tidy_capture()
returns a formula; tidy_dots()
returns a list of formulas, one for each dotted argument.
decorate_nse()
). Providing
a standard evaluation version simplifies programming tasks. Also,
it makes it possible to forward named arguments across function
calls (see below). See vignette("nse")
for more
information on NSE. In addition, note that tidy_capture()
always interpolates
its input to facilitate programming with NSE functions. See
tidy_interp()
and tidy_quote()
.arg_inspect()
for another approach to introspecting
arguments with which it is possible to capture expressions at the
outermost call site. This approach may be harder to reason about
and has some limitations. Dots are different from named arguments in that they are
implicitly forwarded. Forwarding dots does not create a new call
site. The expression is passed on as is. That's why you can
easily capture them with substitute()
. By the
same token, you don't need to capture dots before passing them
along in another introspective function. You do need to be a bit
careful when you rescope expressions captured from dots because
those expressions were not necessarily supplied in the last call
frame. In general, the call site of argument passed through dots
can be anywhere between the current and global frames. For this
reason, it is recommended to always use tidy_dots()
rather than substitute()
and caller_env()
or
parent.frame()
, since the former will encode the
appropriate evaluation environments within the formulas.tidy_dots()
for capturing dots,
expr_label()
and expr_text()
for
capturing labelling information.
# tidy_capture() returns a formula:
fn <- function(foo) tidy_capture(foo)
fn(a + b)
# Capturing an argument only works for the most direct call:
g <- function(bar) fn(bar)
g(a + b)
Run the code above in your browser using DataCamp Workspace