Is there a way to compare whether two function objects are the same?
m <- mean
m == mean ## don\'t work
## this seems not to be the correct way:
function
You can convert the functions to strings, and compare those strings.
equal_functions <- function(f,g)
all(
capture.output(print(f)) ==
capture.output(print(g))
)
equal_functions(function(x) x, function(x) x) # TRUE
But functions that differ for non-essential reasons will be seen as different.
equal_functions(function(x) x, function(u) u) # FALSE
equal_functions(
function(x) x,
function(x)
x
) # FALSE
I use isTRUE(all.equal(function1,function2))
, but this suffers from similar drawbacks to the other methods.
Interestingly though, all.equal
gives a nice summary of how the two operands differ (try all.equal(function1,function2)
.
You can use identical
:
identical(m,mean)