7

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

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

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

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

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

所以在查询与lm类关联的方法时,我们希望恢复这个模拟方法。

4

3 回答 3

7

我是傻子,还是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"
于 2013-04-17T03:04:55.893 回答
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" 
于 2013-04-16T23:45:47.697 回答
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*
于 2013-04-17T03:05:08.733 回答