Short answer: is.function checks if a variable really contains a function. This does not work on (unvalued) calls, because these are calls. You also need to take care of disguise:
mean <- mean (x)
Longer answer:
IMHO there is a big difference between the two occurrences of this_is_a_function .
In the first case, you assign the function to a variable named this_is_a_function after evaluating the expression. The difference is the same difference as between 2+2 and 4 .
However, simply looking for <- function () does not guarantee that the result is a function:
f <- function (x) {x + 1} (2)
The second occurrence is a syntax function call. You can determine from the expression that a variable named this_is_a_function that contains the function must exist in order for the call to be evaluated correctly. BUT: you do not know if this exists from this statement. however, you can check if such a variable exists and whether it is a function.
The fact that functions are stored in variables, such as other data types, also means that in the first case you can know that the result of function () will be a function, and it follows that immediately after evaluating this expression, a variable named this_is_a_function will contain a function.
However, R is full of names and functions: "->" is the name of the assignment function (a variable containing the assignment function) ...
After evaluating the expression, you can check it for is.function (this_is_a_function) . However, this is by no means the only expression that returns a function: Think about
f <- function () {g <- function (){}} > body (f)[[2]][[3]] function() { } > class (body (f)[[2]][[3]]) [1] "call" > class (eval (body (f)[[2]][[3]])) [1] "function"
all.vars (expr, functions = FALSE) seems to return function declarations (f <- function () {}) in the expression, and when filtering function calls ('+' (1,2), ...).
I would say that it is the other way around: in this expression f there is a variable (name) that will be assigned to the function (after calculating the call). + (1, 2) is evaluated as a number. If you do not.
e <- expression (1 + 2) > e <- expression (1 + 2) > e [[1]] 1 + 2 > e [[1]][[1]] `+` > class (e [[1]][[1]]) [1] "name" > eval (e [[1]][[1]]) function (e1, e2) .Primitive("+") > class (eval (e [[1]][[1]])) [1] "function"