match.fun
Function Verification for "Function Variables"

Description

Attempts to find the function specified by the FUN argument.

Usage

match.fun(FUN, descend = TRUE)

Arguments

FUN a function, a function name, or a character string.
descend a logical value. If TRUE (the default), the search continues past matching variables that are not functions. If FALSE, only the first matching variable is considered.

Details

Calling match.fun with descend=TRUE is like calling get with mode='function'. However, first it inspects the FUN argument to determine the function name. If FUN is a function, it just returns. If FUN is a character vector with the length one, or if FUN is a symbol, it uses get to find the function in the environment of the parent of the caller. Otherwise, it attempts to evaluate FUN as a symbol (using substitute twice). If the evaluation fails, it generates an error.
See the examples below for some tricky cases it handles.
Value
returns a function, as specified by the FUN argument. If no function could be found, it generates an error.
See Also
get, getFunction.
Examples
f <- function(FUN, ...) { FUN <- match.fun(FUN); FUN(...) }
sum <- 42   # Variable hiding the real function
f('sum', 1,2,3)
f(sum, 1,2,3)
match.fun("ls")
match.fun(ls)
match.fun(c("ls","sum"))
# 'c("ls", "sum")' is not a function, character or symbol

Package base version 6.1.4-13
Package Index