Question

Is there a way in R to get a list of all methods defined on an S4 class, given the name of that class?

Edit: I know that showMethods can show me all the methods, but I want to manipulate the list programmatically, so that's no good.

Was it helpful?

Solution

Maybe this would be useful:

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"     

The original version did not properly extract the quoted non S4 generics in mtext such as:

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

OTHER TIPS

Are you looking for showMethods()?

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

Maybe something like

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

Also stumbled upon it, how about

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

This will directly yield a vector of method names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top