7

I'd like to see a list of all methods associated with a certain object class. E.g. if I see that some model fit routine returns an object of class "foo", I'd like to know if the package (or any other package) have defined methods such as simulate.foo, update.foo, coef.foo, print.foo etc. How do I do this?

I know that methods solves the inverse problem (assuming an S3 class), but obviously I don't want to search through every object that has defined a print function in order to find out if my class of interest has one. I've probably forgotten something very simple here. thanks!

(Ideally it would be nice to see solutions for S3 and S4 classes).

Update: Ideally a solution should reveal methods that are hidden as well, just as the methods function does. For instance, methods("simulate") shows:

> methods("simulate")
[1] simulate.lm*

so in a query for methods associated with the lm class, we would want to recover this simulate method.

John Paul
  • 12,196
  • 6
  • 55
  • 75
cboettig
  • 12,377
  • 13
  • 70
  • 113

3 Answers3

7

Am I being a bonehead, or is methods(class="foo") what you want (for S3 methods)???

methods(class="lm")
##  [1] add1.lm*           alias.lm*          anova.lm           case.names.lm*    
##  [5] confint.lm*        cooks.distance.lm* deviance.lm*       dfbeta.lm*        
##  [9] dfbetas.lm*        drop1.lm*          dummy.coef.lm*     effects.lm*       
## [13] extractAIC.lm*     family.lm*         formula.lm*        hatvalues.lm      
## [17] influence.lm*      kappa.lm           labels.lm*         logLik.lm*        
## [21] model.frame.lm     model.matrix.lm    nobs.lm*           plot.lm           
## [25] predict.lm         print.lm           proj.lm*           qr.lm*            
## [29] residuals.lm       rstandard.lm       rstudent.lm        simulate.lm*      
## [33] summary.lm         variable.names.lm* vcov.lm*          
## 
##    Non-visible functions are asterisked

showMethods works for S4 classes (taken from @JoshO'Brien's now-deleted answer, for reference):

library(sp)
showMethods(classes="SpatialPolygons")

## Function: [ (package base)
## x="SpatialPolygons"
## 
## Function: addAttrToGeom (package sp)
## x="SpatialPolygons", y="data.frame"
## 
## Function: coerce (package methods)
## from="GridTopology", to="SpatialPolygons"
## from="SpatialGrid", to="SpatialPolygons"
## from="SpatialPixels", to="SpatialPolygons"
## from="SpatialPolygons", to="SpatialLines"
## from="SpatialPolygons", to="SpatialPolygonsDataFrame"
## 
## Function: coordinates (package sp)
## obj="SpatialPolygons"
## 
## Function: coordnames (package sp)
## x="SpatialPolygons"
## 
## Function: coordnames<- (package sp)
## x="SpatialPolygons", value="character"
## 
## Function: over (package sp)
## x="SpatialGrid", y="SpatialPolygons"
## x="SpatialPoints", y="SpatialPolygons"
## x="SpatialPolygons", y="SpatialGrid"
## x="SpatialPolygons", y="SpatialGridDataFrame"
## x="SpatialPolygons", y="SpatialPoints"
## x="SpatialPolygons", y="SpatialPointsDataFrame"
## 
## Function: overlay (package sp)
## x="SpatialGridDataFrame", y="SpatialPolygons"
## x="SpatialGrid", y="SpatialPolygons"
## x="SpatialPointsDataFrame", y="SpatialPolygons"
## x="SpatialPoints", y="SpatialPolygons"
## x="SpatialPolygons", y="SpatialGrid"
## x="SpatialPolygons", y="SpatialPoints"
## 
## Function: plot (package graphics)
## x="SpatialPolygons", y="missing"
## 
## Function: polygons (package sp)
## obj="SpatialPolygons"
## 
## Function: polygons<- (package sp)
## object="data.frame", value="SpatialPolygons"
## 
## Function: recenter (package sp)
## obj="SpatialPolygons"
## 
## Function: spChFIDs (package sp)
## obj="SpatialPolygons", x="character"
## 
## Function: spsample (package sp)
## x="SpatialPolygons"
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
4

Here is an example using .lm as a regex pattern within apropos rather than searching fruitlessly for .foo:

> apropos("\\.lm")
 [1] "anova.lm"        "anova.lmlist"    "hatvalues.lm"    "kappa.lm"        "model.frame.lm"  "model.matrix.lm"
 [7] "panel.lmline"    "plot.lm"         "predict.lm"      "prepanel.lmline" "print.lm"        "residuals.lm"   
[13] "rstandard.lm"    "rstudent.lm"     "summary.lm"     

There are also methods that begin with "lm." so you might want those as well:

> apropos("lm\\.")
 [1] ".__C__anova.glm.null" ".__C__glm.null"       "glm.control"          "glm.convert"         
 [5] "glm.fit"              "glm.nb"               "lm.fit"               "lm.fit.qr.bare"      
 [9] "lm.gls"               "lm.influence"         "lm.pfit"              "lm.ridge"            
[13] "lm.wfit"    

And if you wanted leave out the "glm." methods use the somewhat more restricted regex:

> apropos("^lm\\.")
[1] "lm.fit"         "lm.fit.qr.bare" "lm.gls"         "lm.influence"   "lm.pfit"        "lm.ridge"      
[7] "lm.wfit" 
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Unfortunately it appears this will fail in cases where the class is not exported to the NAMESPACE, e.g. all methods that appear with an asterix * when listed by "methods()". – cboettig Apr 16 '13 at 23:55
2

For the S3 case, methods has an argument class for this:

> methods(class="lm")
 [1] add1.lm*           alias.lm*          anova.lm           case.names.lm*    
 [5] confint.lm*        cooks.distance.lm* deviance.lm*       dfbeta.lm*        
 [9] dfbetas.lm*        drop1.lm*          dummy.coef.lm*     effects.lm*       
[13] extractAIC.lm*     family.lm*         formula.lm*        hatvalues.lm      
[17] influence.lm*      kappa.lm           labels.lm*         logLik.lm*        
[21] model.frame.lm     model.matrix.lm    nobs.lm*           plot.lm           
[25] predict.lm         print.lm           proj.lm*           qr.lm*            
[29] residuals.lm       rstandard.lm       rstudent.lm        simulate.lm*      
[33] summary.lm         variable.names.lm* vcov.lm*
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453