如何查看使用方法/ MethodUtils包编写的方法的代码?

时间:2013-11-12 20:41:55

标签: r oop

我最近在R中遇到了另一个OO范例。

library(RSQLite)
> dbReadTable
standardGeneric for "dbReadTable" defined from package "DBI"
  defined with value class: "data.frame"

function (conn, name, ...) 
.valueClassTest(standardGeneric("dbReadTable"), "data.frame", 
    "dbReadTable")
<environment: 0x1d252198>
Methods may be defined for arguments: conn, name
Use  showMethods("dbReadTable")  for currently available ones.
> showMethods('dbReadTable')
Function: dbReadTable (package DBI)
conn="SQLiteConnection", name="character"

两个问题:

  • 这是否与新范例listed here相对应?或者这只是一种操纵方式,例如S4班?
  • 如何查看dbReadTable方法的来源?

1 个答案:

答案 0 :(得分:3)

与S4方法一样,只需使用您感兴趣的方法的签名调用getMethod()

## Use showMethods to view signatures of dbReadTable's methods
showMethods('dbReadTable')
# Function: dbReadTable (package DBI)
# conn="SQLiteConnection", name="character"

## getMethod's 2nd argument is a character vector containing method's signature 
getMethod("dbReadTable", c("SQLiteConnection", "character"))
# Method Definition:
# 
# function (conn, name, ...) 
# sqliteReadTable(conn, name, ...)
# <environment: namespace:RSQLite>
# 
# Signatures:
#         conn               name       
# target  "SQLiteConnection" "character"
# defined "SQLiteConnection" "character"

然后在看到上述内容之后,您可能希望查看返回的代码:

sqliteReadTable