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

孤街浪徒 提交于 2019-11-29 07:51:02

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

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.

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