[英]How to find out which method is dispatched to an S3 object?
How can I find out which method is being dispatched when I apply a function to an S3 object?当我将函数应用于 S3 对象时,如何找出正在调度的方法?
For example, if I run the code例如,如果我运行代码
df <- data.frame(list("a" = c(1,2,3)), "b" = c(7,8,9))
print(df)
I think the method print.data.frame
is being dispatched because it shows up when running methods(class="data.frame")
我认为方法
print.data.frame
正在被调度,因为它在运行methods(class="data.frame")
Is there a way verify this?有没有办法验证这一点? Such as a function that takes in
print
and data.frame
and outputs the method used?比如一个接收
print
和data.frame
并输出使用的方法的函数?
The sloop
package can show you more details. sloop
包可以显示更多细节。 For example例如
sloop::s3_dispatch(print(df))
# => print.data.frame
# * print.default
this shows you all the possible matches, and highlights the one that was actually used ( print.data.frame
)这会向您显示所有可能的匹配项,并突出显示实际使用的匹配项(
print.data.frame
)
But in general you need to look at the class()
of the object you pass to the function.但一般来说,您需要查看传递给函数的对象的
class()
。 The first class that matches up with the methods()
listed for the function will be the one called.与为该函数列出的
methods()
匹配的第一个类将被调用。 If you want to see the code of the function that would be used, you could use如果您想查看将要使用的函数的代码,您可以使用
getS3method("print", "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.