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.

  • By constructing it with base::call(), base::as.call(), or call2(). In this case, you pass the call elements (the function to call and the arguments to call it with) separately.

See section below for the difference between call2() and the base constructors.

call2(.fn, ..., .ns = NULL)

Arguments

.fn

Function to call. Must be a callable object: a string, symbol, call, or a function.

...

Arguments to the call either in or out of a list. These dots support tidy dots features.

.ns

Namespace with which to prefix .fn. Must be a string or symbol.

Difference with base constructors

call2() is more flexible and convenient than base::call():

  • The function to call can be a string or a callable object: a symbol, another call (e.g. a $ or [[ 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))
    
  • The .ns argument is convenient for creating namespaced calls.

    call2("list", 1, 2, .ns = "base")
        ns_call <- as.call(list(as.name("::"), as.name("list"), as.name("base")))
    as.call(list(ns_call, 1, 2))
    
  • call2() has tidy dots support and you can splice lists of arguments with !!!. With base R, you need to use as.call() instead of call() if the arguments are in a list.

    args <- list(na.rm = TRUE, trim = 0)
        call2("mean", 1:10, !!!args)
        as.call(c(list(as.name("mean"), 1:10), args))
    

Life cycle

In rlang 0.2.0 lang() was soft-deprecated and renamed to call2().

In early versions of rlang calls were called "language" objects in order to follow the R type nomenclature as returned by base::typeof(). The goal was to avoid adding to the confusion between S modes and R types. With hindsight we find it is better to use more meaningful type names.

See also

call_modify

Examples

# 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)