This function tests if x is a call (or language object). 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_lang() and is_binary_lang() hardcode n to 1 and 2.

is_lang(x, name = NULL, n = NULL, ns = NULL)

is_unary_lang(x, name = NULL, ns = NULL)

is_binary_lang(x, name = NULL, ns = 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 sym() before matching. This argument is vectorised and you can supply a vector of names to match. In this case, is_lang() returns TRUE if at least one name matches.

n

An optional number of arguments that the call should match.

ns

The namespace of the call. If NULL, the namespace doesn't participate in the pattern-matching. If an empty string "" and x is a namespaced call, is_lang() returns FALSE. If any other string, is_lang() checks that x is namespaced within ns.

See also

is_expr()

Examples

is_lang(quote(foo(bar)))
#> [1] TRUE
# You can pattern-match the call with additional arguments: is_lang(quote(foo(bar)), "foo")
#> [1] TRUE
is_lang(quote(foo(bar)), "bar")
#> [1] FALSE
is_lang(quote(foo(bar)), quote(foo))
#> [1] TRUE
# Match the number of arguments with is_lang(): is_lang(quote(foo(bar)), "foo", 1)
#> [1] TRUE
is_lang(quote(foo(bar)), "foo", 2)
#> [1] FALSE
# Or more specifically: is_unary_lang(quote(foo(bar)))
#> [1] TRUE
is_unary_lang(quote(+3))
#> [1] TRUE
is_unary_lang(quote(1 + 3))
#> [1] FALSE
is_binary_lang(quote(1 + 3))
#> [1] TRUE
# By default, namespaced calls are tested unqualified: ns_expr <- quote(base::list()) is_lang(ns_expr, "list")
#> [1] TRUE
# You can also specify whether the call shouldn't be namespaced by # supplying an empty string: is_lang(ns_expr, "list", ns = "")
#> [1] FALSE
# Or if it should have a namespace: is_lang(ns_expr, "list", ns = "utils")
#> [1] FALSE
is_lang(ns_expr, "list", ns = "base")
#> [1] TRUE
# The name argument is vectorised so you can supply a list of names # to match with: is_lang(quote(foo(bar)), c("bar", "baz"))
#> [1] FALSE
is_lang(quote(foo(bar)), c("bar", "foo"))
#> [1] TRUE
is_lang(quote(base::list), c("::", ":::", "$", "@"))
#> [1] TRUE