显示与特定类关联的方法

时间:2013-04-16 23:28:46

标签: r class s4 r-s3

我想查看与某个对象类关联的所有方法的列表。例如。如果我看到某个模型拟合例程返回类“foo”的对象,我想知道包(或任何其他包)是否已定义方法,如simulate.fooupdate.foo,{ {1}},coef.foo等。我该怎么做?

我知道print.foo解决了反问题(假设是S3类),但显然我不想搜索已定义methods函数的每个对象以查明是否我感兴趣的课程有一个。我可能在这里忘记了一些非常简单的事情。谢谢!

(理想情况下,看到S3和S4类的解决方案会很好)。

更新:理想情况下,解决方案应该显示隐藏的方法,就像print函数一样。例如,methods显示:

methods("simulate")

因此,在查询与> methods("simulate") [1] simulate.lm* 类关联的方法时,我们希望恢复此模拟方法。

3 个答案:

答案 0 :(得分:6)

我是傻瓜,还是methods(class="foo")你想要的(对于S3方法)???

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适用于S4课程(摘自@JoshO'Brien现已删除的答案,供参考):

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"

答案 1 :(得分:4)

以下示例使用.lm作为apropos内的正则表达式模式,而不是无效地搜索.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"     

还有一些以“lm”开头的方法。所以你可能也想要那些:

> 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"    

如果你想放弃“glm”。方法使用更受限制的正则表达式:

> apropos("^lm\\.")
[1] "lm.fit"         "lm.fit.qr.bare" "lm.gls"         "lm.influence"   "lm.pfit"        "lm.ridge"      
[7] "lm.wfit" 

答案 2 :(得分:2)

对于S3情况,methods为此具有参数class

> 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*