如何查看S4函数的定义?例如,我希望看到TSconnect在包TSdbi中的定义。命令
showMethods("TSconnect")揭示出,除其他外,还有一个用于drv="histQuoteDriver“、dbname=”字符“的函数。
我如何才能看到这个函数的定义?如果它是一个S3函数,那么只有第一个参数可定义(drv),可以使用print(TSconnect.histQuoteDriver)检查它。
编辑:从r锻造中,我找到了想要的输出:
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会话中获得这个定义?
发布于 2010-01-31 18:05:29
S4类比较复杂,所以我建议使用读这篇导论。
在本例中,TSdbi是一个由所有特定数据库包(例如TSMySQL、TSPostgreSQL等)扩展的泛型S4类的示例。TSconnect()方法在TSdbi中没有比您看到的更多的东西了:drv=“字符”,dbname=“字符”是函数的参数,而不是函数本身的参数。如果您安装了一些特定的数据库包并使用showMethods("TSconnect"),您将看到该函数的所有特定实例。如果然后使用特定的数据库驱动程序调用TSconnect(),那么它将使用适当的函数。
这也是诸如摘要等函数的工作方式。例如,尝试调用showMethods(summary)。根据加载的包,您应该看到返回了多个方法。
您可以在R:http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/TSdbi/R/TSdbi.R?rev=70&root=tsdbi&view=markup上轻松地看到它的源代码。这就是这一职能的范围:
setGeneric("TSconnect", def= function(drv, dbname, ...) standardGeneric("TSconnect"))
setMethod("TSconnect", signature(drv="character", dbname="character"),
definition=function(drv, dbname, ...)
TSconnect(dbDriver(drv), dbname=dbname, ...))https://stackoverflow.com/questions/2172146
复制相似问题