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

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

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

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

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

因此,在查询与lm类关联的方法时,我们希望恢复此模拟方法。

===============>>#1 票数: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"

===============>>#2 票数: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" 

===============>>#3 票数: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*

  ask by cboettig translate from so

未解决问题?本站智能推荐:

关注微信公众号