Testing equality of two functions in R [duplicate]

时间:2022-03-15 16:54:24

This question already has an answer here:

这个问题在这里已有答案:

Is there a way to determine if the text of two different functions is identical?

有没有办法确定两个不同功能的文本是否相同?

x <- function(x) print(x + 2)
y <- function(x) print(x + 2)
identical(x, y)
[1] FALSE
identical(mget("x"), mget("y"))
[1] FALSE
identical(unname(mget("x")), unname(mget("y")))
[1] FALSE

2 个解决方案

#1


10  

I think this is a good method. It works for many different objects:

我认为这是一个很好的方法。它适用于许多不同的对象:

all.equal(x,y)
[1] TRUE

#2


3  

Using diffobj package:

使用diffobj包:

library(diffobj)

x <- function(x) print(x + 2)
y <- function(x) print(x + 2)

diffPrint(target = x, current = y)

Testing equality of two functions in R [duplicate]

Wrapping it in any() will give TRUE/FALSE:

将它包装在任何()中将给出TRUE / FALSE:

any(diffPrint(target = x, current = y))
# FALSE

#1


10  

I think this is a good method. It works for many different objects:

我认为这是一个很好的方法。它适用于许多不同的对象:

all.equal(x,y)
[1] TRUE

#2


3  

Using diffobj package:

使用diffobj包:

library(diffobj)

x <- function(x) print(x + 2)
y <- function(x) print(x + 2)

diffPrint(target = x, current = y)

Testing equality of two functions in R [duplicate]

Wrapping it in any() will give TRUE/FALSE:

将它包装在任何()中将给出TRUE / FALSE:

any(diffPrint(target = x, current = y))
# FALSE