7

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.

Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159

4 Answers4

7

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>" 
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Echoing Josh's comment, the first line could be `mtext <- showMethods(class="SpatialPolygons", printTo=FALSE)` – Brian Diggs Oct 01 '13 at 22:29
  • I suppose that would be better if this will be used in a programming context. The hard part was cobbling together a `regex` strategy that got both the regular S4 functions and the non-S4 functions displayed neatly. – IRTFM Oct 01 '13 at 22:44
  • Is there a good reason for the printTo functionality in showMethods? How does showMethods get the information? Does it do it by capturing the output of a print on objects? Ick. – Spacedman Oct 02 '13 at 07:02
2

Are you looking for showMethods()?

library(sp)
showMethods(class="SpatialPolygons")
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • That prints out some text that describes the methods, but I want something I can handle programatically, like a list or data frame or vector. – Ryan C. Thompson Oct 01 '13 at 20:11
  • 1
    As a terrible hack you can `sp_methods = character(); showMethods(class="SpatialPolygons", where=search(), printTo=textConnection("sp_methods", "a"))` – Martin Morgan Oct 01 '13 at 20:26
  • @MartinMorgan -- Or just `sp_methods <- showMethods(class="SpatialPolygons", printTo=FALSE)` seems to work. – Josh O'Brien Oct 01 '13 at 21:38
0

Maybe something like

library(sp)
x=capture.output(showMethods(class="SpatialPolygons"))
unlist(lapply(strsplit(x[grep("Function: ",x,)]," "),function(x) x[2]))
cryo111
  • 4,444
  • 1
  • 15
  • 37
0

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.

loki
  • 9,816
  • 7
  • 56
  • 82