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