call_name() and call_ns() extract the function name or
namespace of simple calls as a string. They return NULL for
complex calls.
Simple calls:
foo(),bar::foo().Complex calls:
foo()(),bar::foo,foo$bar(),(function() NULL)().
The is_call_simple() predicate helps you determine whether a call
is simple. There are two invariants you can count on:
If
is_call_simple(x)returnsTRUE,call_name(x)returns a string. Otherwise it returnsNULL.If
is_call_simple(x, ns = TRUE)returnsTRUE,call_ns()returns a string. Otherwise it returnsNULL.
Examples
# Is the function named?
is_call_simple(quote(foo()))
#> [1] TRUE
is_call_simple(quote(foo[[1]]()))
#> [1] FALSE
# Is the function namespaced?
is_call_simple(quote(list()), ns = TRUE)
#> [1] FALSE
is_call_simple(quote(base::list()), ns = TRUE)
#> [1] TRUE
# Extract the function name from quoted calls:
call_name(quote(foo(bar)))
#> [1] "foo"
call_name(quo(foo(bar)))
#> [1] "foo"
# Namespaced calls are correctly handled:
call_name(quote(base::matrix(baz)))
#> [1] "matrix"
# Anonymous and subsetted functions return NULL:
call_name(quote(foo$bar()))
#> NULL
call_name(quote(foo[[bar]]()))
#> NULL
call_name(quote(foo()()))
#> NULL
# Extract namespace of a call with call_ns():
call_ns(quote(base::bar()))
#> [1] "base"
# If not namespaced, call_ns() returns NULL:
call_ns(quote(bar()))
#> NULL
