Has anyone put together/found a good method for listing all the S3 methods available for a given object? The built-in methods()
function will give all available methods for a specified class, or for a specified generic function, but not for an object.
The example I have in mind is a glm
object, which is of (minor?) class "glm"
but also inherits from "lm"
g <- glm(y~x,data=data.frame(x=1:10,y=1:10))
class(g)
## [1] "glm" "lm"
There are 35 methods for class "lm" and 22 for "glm". I'm interested in an answer that combines the results of
lapply(class(g),function(x) methods(class=x))
in a sensible way, so that I can immediately see (for example) that there is a glm
-specific method for add1
, but that the method for alias
is inherited from the lm
class.
Does someone have a slick way to do this, or does it already exist?
PS Steve Walker's S3-S4-reference class glossary shows that this works automatically for reference classes, where we have to use an object to get the methods (x$getRefClass()$methods()
).
Here's an attempt to replicate the "standard" behavior
And then you can try it out with
and that will return
It's not as elegant or short as Josh's, but I think its a good recreation of the default behavior. It's funny to see that the
methods
function is itself mostly just a grep across all known function names. I borrowed thegsub
stuff from there.Here's a function that will at least tell you which S3 methods an object will initially trigger: