7

在给定该类的名称的情况下,R 中有没有办法获取在 S4 类上定义的所有方法的列表?

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

4

4 回答 4

7

也许这会很有用:

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>" 
于 2013-10-01T20:36:14.590 回答
2

你在找showMethods()吗?

library(sp)
showMethods(class="SpatialPolygons")
于 2013-10-01T20:08:24.727 回答
0

也许像

library(sp)
x=capture.output(showMethods(class="SpatialPolygons"))
unlist(lapply(strsplit(x[grep("Function: ",x,)]," "),function(x) x[2]))
于 2013-10-01T20:30:44.640 回答
0

也偶然发现了,怎么样

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

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

于 2017-08-18T08:33:27.520 回答