rlang (version 0.0.0.9000)

is_call: Is object a call?

Description

This function tests if x is a call. This is a pattern-matching predicate that will return FALSE if name and n are supplied and the call does not match these properties. is_unary_call() and is_binary_call() hardcode n to 1 and 2.

Usage

is_call(x, name = NULL, n = NULL)
is_unary_call(x, name = NULL)
is_binary_call(x, name = NULL)

Arguments

x
An object to test. If a formula, the right-hand side is extracted.
name
An optional name that the call should match. It is passed to as_name() before matching. This argument is vectorised and you can supply a vector of names to match. In this case, is_call() returns TRUE if at least one name matches.
n
An optional number of arguments that the call should match.

See Also

is_lang()

Examples

Run this code
is_call(quote(foo(bar)))

# Right-hand sides are extracted from formulas:
is_call(~foo(bar))

# You can pattern-match the call with additional arguments:
is_call(~foo(bar), "foo")
is_call(~foo(bar), "bar")
is_call(~foo(bar), quote(foo))

# Match the number of arguments with is_call():
is_call(~foo(bar), "foo", 1)
is_call(~foo(bar), "foo", 2)

# Or more specifically:
is_unary_call(~foo(bar))
is_unary_call(~ +3)
is_unary_call(~ 1 + 3)
is_binary_call(~ 1 + 3)

# Namespaced calls are a bit tricky. Strings won't work because
# as_name("base::list") returns a symbol rather than a namespace
# call:
is_call(~base::list(baz), "base::list")

# However you can use the fact that as_name(quote(base::list()))
# extracts the function identifier as is, and thus returns the call
# base::list:
is_call(~base::list(baz), ~base::list(), 1)


# The name argument is vectorised so you can supply a list of names
# to match with:
is_call(~foo(bar), c("bar", "baz"))
is_call(~foo(bar), c("bar", "foo"))
is_call(~base::list, c("::", ":::", "$", "@"))

Run the code above in your browser using DataCamp Workspace