5

Possible Duplicate:
R: show source code of an S4 function in a package

I downloaded a package (GEOquery) and was playing with some of the functions. One of them is called Table, which, to my understanding, is able to tabulate an S4 dataset.

E.g.

> summary(GDS2853) # GDS2853 is a dataset I downloaded from NCBI 
Length  Class   Mode 
     1    GDS     S4 

getAnywhere(Table) shows

> getAnywhere(Table)
A single object matching ‘Table’ was found
It was found in the following places
  package:GEOquery
  namespace:GEOquery
with value

function (object) 
standardGeneric("Table")
<environment: 0x06ad5268>
attr(,"generic")
[1] "Table"
attr(,"generic")attr(,"package")
[1] "GEOquery"
attr(,"package")
[1] "GEOquery"
attr(,"group")
list()
attr(,"valueClass")
character(0)
attr(,"signature")
[1] "object"
attr(,"default")
`\001NULL\001`
attr(,"skeleton")
function (object) 
stop("invalid call in method dispatch to \"Table\" (no default method)", 
    domain = NA)(object)
attr(,"class")
[1] "standardGeneric"
attr(,"class")attr(,"package")
[1] "methods"

I'd like to learn the code of Table so that I could know how to tabulate a GDS dataset, as data.frame and as.list couldn't coerce an S4 class - although I could tabulate GDS dataset by, for example,

GDS_table=Table(GDS2853)[1:20000,1:20] #GDS2853 contains 20 columns
and approx 17000 rows 

I tried the getMethods as suggested in other posts but below is what I got

> getMethod("Table")
Error in getMethod("Table") : 
  No method found for function "Table" and signature

I also tried to specify the "where" by putting in package=:GEOquery but apparently package is an unused argument.

Wonder what I did wrong so as to fail to see the source code for Table.

Community
  • 1
  • 1
B Chen
  • 923
  • 2
  • 12
  • 21

2 Answers2

9

From the output you posted, it looks like Table is an S4 generic.

To view a list of its S4 methods, use showMethods(). To view a particular method, use getMethod(), passing in the 'signature' of the method you want along with the name of the function. (A 'signature' is a character vector composed of the class(es) of the argument(s) according to which the generic Table performs its method dispatch. i.e. if you will be doing Table(GDS2853), the signature will likely be class(GDS2835))

Here's an example that gets the code for an S4 method in the sp package:

library(sp)

showMethods("overlay")
# Function: overlay (package sp)
# x="SpatialGrid", y="SpatialPoints"
# x="SpatialGrid", y="SpatialPolygons"
# x="SpatialGridDataFrame", y="SpatialPoints"
# x="SpatialGridDataFrame", y="SpatialPolygons"
# x="SpatialPixels", y="SpatialPoints"
# x="SpatialPixelsDataFrame", y="SpatialPoints"
# x="SpatialPoints", y="SpatialPolygons"
# x="SpatialPointsDataFrame", y="SpatialPolygons"
# x="SpatialPolygons", y="SpatialGrid"
# x="SpatialPolygons", y="SpatialPoints"

getMethod("overlay", signature=c("SpatialGrid", "SpatialPoints"))
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • Thanks a lot guys. I must confess I still don't understand exactly what the "signature" entails. The description from ?getMethod didn't help me much either. But I tried and found that, like Joshua mentioned, with signature="GEOData", getMethod did get me something to further follow up. – B Chen Oct 02 '12 at 03:54
  • a side note- just noticed that (plus someone else's posting) both showMethods and getMethod seem to be about "method" - not so much about "function". As GDS download is mostly a S4 class, it seems that I would need a "method", not a "function" to manage the dataset. Is this correct? – B Chen Oct 02 '12 at 04:18
  • S4 methods are function, just like S3 methods are functions. You might be able to better get your head around this if you start by looking at a few S3 methods. Try, for example, typing: `methods("print")`, and then pick a few familiar ones, and look at them -- perhaps `print.data.table` and `print.lm`. What makes those two functions "methods" is that they can both be invoked by typing `print(obj)`: *which* function gets used depends solely on the class of `obj`. (We say that they, along with the 100+ other functions returned by `methods("print")` are "print methods".) HTH. – Josh O'Brien Oct 02 '12 at 04:35
3

In your example, it would be:

getMethod("Table", "GEOData")

You may also be interested in how to get the help documentation for S4 methods, which has an equally unusual invocation required:

method?Table("GEOData")

Generally, with S4, you will need

  • the function name
  • the class (signature) of objects it is for

If you are lost as to the latter:

class(object)

will return the class, and you can also do:

showMethods("Table")

to show all currently available methods. Alternatively, I find I often use:

findMethods("Table")

and the reason is the findMethods returns a list of all the methods for a particular function. Classes can have long names and I find I mistype/miscapitalize them often so as a quick hack, findMethods("functionname") is handy. Of course, it can also bite you for generic functions with many methods as the printed list may be quite long.

Joshua
  • 686
  • 3
  • 7