题
如何查看S4函数的定义?例如,我想查看包TSDBI包中TSCONNECT的定义。命令
showMethods("TSconnect")
揭示了DRV =“ HistQuotedRiver”的功能,dbName =“ cartare”。
我如何看到此功能的定义?如果它是S3函数,则只有第一个参数可定义(DRV),可以用PRINT(TSCONNECT.HINDQOOTEDRIVER)对其进行检查。
编辑: :从r-forge我找到了所需的输出:
setMethod("TSconnect", signature(drv="histQuoteDriver", dbname="character"),
definition= function(drv, dbname, user="", password="", host="", ...){
# user / password / host for future consideration
if (is.null(dbname)) stop("dbname must be specified")
if (dbname == "yahoo") {
con <- try(url("http://quote.yahoo.com"), silent = TRUE)
if(inherits(con, "try-error"))
stop("Could not establish TShistQuoteConnection to ", dbname)
close(con)
}
else if (dbname == "oanda") {
con <- try(url("http://www.oanda.com"), silent = TRUE)
if(inherits(con, "try-error"))
stop("Could not establish TShistQuoteConnection to ", dbname)
close(con)
}
else
warning(dbname, "not recognized. Connection assumed working, but not tested.")
new("TShistQuoteConnection", drv="histQuote", dbname=dbname, hasVintages=FALSE, hasPanels=FALSE,
user = user, password = password, host = host )
} )
有没有办法从R会话中获得此定义?
解决方案
S4课程相对复杂,所以我建议 阅读此介绍.
在这种情况下,TSDBI是一个通用S4类的示例,该类别由所有特定数据库软件包(例如TSMYSQL,TSPOSTGRESQL等)扩展。 TSDBI中的Tsconnect()方法没有什么比您看到的:drv =“ artial”,dbname =“ artial”是该函数的参数,而不是本身函数。如果您安装了一些特定的数据库软件包,并使用ShowMethods(“ Tsconnect”),则将看到该功能的所有特定实例。如果您使用特定的数据库驱动程序调用TSCONNECT(),它将使用并使用适当的功能。
这也是诸如摘要工作之类的功能。例如,尝试调用 showMethods(summary)
. 。根据加载包装的不同,您应该看到返回的多种方法
您可以在R-Forge上轻松查看它的源代码: http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/tsdbi/r/tsdbi.r?rev=70&root=tsdbi&view = marksup. 。这就是该功能的程度:
setGeneric("TSconnect", def= function(drv, dbname, ...) standardGeneric("TSconnect"))
setMethod("TSconnect", signature(drv="character", dbname="character"),
definition=function(drv, dbname, ...)
TSconnect(dbDriver(drv), dbname=dbname, ...))
不隶属于 StackOverflow