show source code for a function in a package in R [duplicate]

不问归期 提交于 2019-12-25 03:34:53

问题


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.


回答1:


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"))



回答2:


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.



来源:https://stackoverflow.com/questions/29020797/r-how-to-view-standard-generic-method-when-no-object-is-specified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!