Create a callSource:
Quoted function calls are one of the two types of symbolic objects in R. They represent the action of calling a function, possibly with arguments. There are two ways of creating a quoted call:
By quoting it. Quoting prevents functions from being called. Instead, you get the description of the function call as an R object. That is, a quoted function call.
See section below for the difference between
call2() and the base
Function to call. Must be a callable object: a string, symbol, call, or a function.
<dynamic> Arguments for the function call. Empty arguments are preserved.
Namespace with which to prefix
.fn. Must be a string or symbol.
call2() is more flexible than
The function to call can be a string or a callable object: a symbol, another call (e.g. a
[[call), or a function to inline.
base::call()only supports strings and you need to use
base::as.call()to construct a call with a callable object.
call2(list, 1, 2) as.call(list(list, 1, 2))
.nsargument is convenient for creating namespaced calls.
call2("list", 1, 2, .ns = "base") # Equivalent to <- call("::", as.symbol("list"), as.symbol("base")) ns_call as.call(list(ns_call, 1, 2))
call2()has dynamic dots support. You can splice lists of arguments with
!!!or unquote an argument name with glue syntax.
<- list(na.rm = TRUE, trim = 0) args call2("mean", 1:10, !!!args) # Equivalent to as.call(c(list(as.symbol("mean"), 1:10), args))
call2() makes it possible to inline objects in calls, both in
function and argument positions. Inlining an object or a function
has the advantage that the correct object is used in all
environments. If all components of the code are inlined, you can
even evaluate in the empty environment.
However inlining also has drawbacks. It can cause issues with NSE
functions that expect symbolic arguments. The objects may also leak
in representations of the call stack, such as
# fn can either be a string, a symbol or a call call2("f", a = 1) #> f(a = 1) call2(quote(f), a = 1) #> f(a = 1) call2(quote(f()), a = 1) #> f()(a = 1) #' Can supply arguments individually or in a list call2(quote(f), a = 1, b = 2) #> f(a = 1, b = 2) call2(quote(f), !!!list(a = 1, b = 2)) #> f(a = 1, b = 2) # Creating namespaced calls is easy: call2("fun", arg = quote(baz), .ns = "mypkg") #> mypkg::fun(arg = baz) # Empty arguments are preserved: call2("[", quote(x), , drop = ) #> x[, drop = ]