Kar*_* W. 11 r code-inspection s4
如何查看S4功能的定义?例如,我想在封装TSdbi中看到TSconnect的定义.命令
showMethods("TSconnect")
Run Code Online (Sandbox Code Playgroud)
揭示除了其他之外,还存在drv ="histQuoteDriver",dbname ="character"的函数.
我怎样才能看到这个函数的定义?如果它是S3函数,则只有第一个可定义的参数(drv),可以使用print(TSconnect.histQuoteDriver)进行检查.
编辑:从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 )
} )
Run Code Online (Sandbox Code Playgroud)
有没有办法从R会话中获取此定义?
Sha*_*ane 10
S4课程相对复杂,所以我建议阅读这篇介绍.
在这种情况下,TSdbi是一个通用S4类的示例,它由所有特定数据库包(例如TSMySQL,TSPostgreSQL等)扩展.TSdbi中的TSconnect()方法没有比您所看到的更多:drv ="character",dbname ="character"是函数的参数,而不是函数本身.如果安装某些特定的数据库软件包并使用showMethods("TSconnect"),您将看到该函数的所有特定实例.如果您随后使用特定的数据库驱动程序调用TSconnect(),它将使用相应的函数.
这也是汇总等功能的工作原理.例如,尝试呼叫showMethods(summary)
.根据加载的包,您应该看到返回多个方法
你可以在R-Forge上轻松看到它的源代码:http: //r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/TSdbi/R/TSdbi.R?ev = 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, ...))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1601 次 |
最近记录: |