Learn R Programming

rlang (version 0.1)

is_lang: Is object a call?

Description

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.

Usage

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

Run this code
# NOT RUN {
is_lang(quote(foo(bar)))

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

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

# Or more specifically:
is_unary_lang(quote(foo(bar)))
is_unary_lang(quote(+3))
is_unary_lang(quote(1 + 3))
is_binary_lang(quote(1 + 3))


# By default, namespaced calls are tested unqualified:
ns_expr <- quote(base::list())
is_lang(ns_expr, "list")

# You can also specify whether the call shouldn't be namespaced by
# supplying an empty string:
is_lang(ns_expr, "list", ns = "")

# Or if it should have a namespace:
is_lang(ns_expr, "list", ns = "utils")
is_lang(ns_expr, "list", ns = "base")


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

Run the code above in your browser using DataLab