如何获得R中S4类上定义的所有方法的列表?

时间:2013-10-01 20:05:51

标签: r methods introspection s4

在R中是否有一种方法可以获得在S4类中定义的所有方法的列表,给定该类的名称?

编辑:我知道showMethods可以向我展示所有方法,但我想以编程方式操作列表,这样做并不好。

4 个答案:

答案 0 :(得分:5)

也许这会有用:

mtext <-  showMethods(class="SpatialPolygons", printTo =FALSE )
fvec <- gsub( "Function(\\:\\s|\\s\\\")(.+)(\\s\\(|\\\")(.+$)",
                                      "\\2", mtext[grep("^Function", mtext)] )
fvec
 [1] ".quad"         "["             "addAttrToGeom"
 [4] "area"          "as.data.frame" "click"        
 [7] "coerce"        "coordinates"   "coordnames"   
[10] "coordnames<-"  "coords"        "disaggregate" 
[13] "extract"       "fromJSON"      "isDiagonal"   
[16] "isTriangular"  "isValidJSON"   "jsType"       
[19] "over"          "overlay"       "plot"         
[22] "polygons"      "polygons<-"    "rasterize"    
[25] "recenter"      "spChFIDs"      "spsample"     
[28] "spTransform"   "text"          "toJSON"     

原始版本没有在mtext中正确提取引用的非S4泛型,例如:

 [60] "Function \"jsType\":"                                     
 [61] " <not an S4 generic function>" 

答案 1 :(得分:2)

您在寻找showMethods()吗?

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

答案 2 :(得分:0)

也许像

library(sp)
x=capture.output(showMethods(class="SpatialPolygons"))
unlist(lapply(strsplit(x[grep("Function: ",x,)]," "),function(x) x[2]))

答案 3 :(得分:0)

也偶然发现它,怎么样

library(sp)
attr(methods(class="SpatialPolygons"), "info")$generic
# Alternatively:
# attr(.S4methods(class="SpatialPolygons"), "info")$generic

这将直接产生方法名称的向量。