R 检查S4方法

R 检查S4方法,r,s4,code-inspection,R,S4,Code Inspection,如何查看S4函数的定义?例如,我想在包TSdbi中查看TSconnect的定义。命令 showMethods("TSconnect") 显示除其他外,还有一个函数用于drv=“histQuoteDriver”,dbname=“character” 我怎样才能看到这个函数的定义?如果它是S3函数,那么只有第一个可定义参数(drv),可以使用print(TSconnect.histQuoteDriver)检查它 编辑:从r-forge中,我找到了所需的输出: setMethod("TSconnec

如何查看S4函数的定义?例如,我想在包TSdbi中查看TSconnect的定义。命令

showMethods("TSconnect")
显示除其他外,还有一个函数用于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 ) 
   } )
setMethod(“TSconnect”,签名(drv=“histQuoteDriver”,dbname=“character”),
定义=函数(drv、dbname、用户=、密码=、主机=、…){
#用户/密码/主机,以备将来考虑
if(is.null(dbname))stop(“必须指定dbname”)
if(dbname==“yahoo”){

conS4课程相对来说比较复杂,所以我建议

在本例中,TSdbi是一个通用S4类的示例,它由所有特定的数据库包(例如TSMySQL、TSPostgreSQL等)扩展。TSdbi中的TSconnect()方法没有什么比您看到的更重要的了:drv=“character”,dbname=“character”是函数的参数,而不是函数本身的参数。如果安装某些特定的数据库包并使用showMethods(“TSconnect”),您将看到该函数的所有特定实例。如果随后使用特定的数据库驱动程序调用TSconnect(),它将转到并使用相应的函数

这也是summary等函数的工作方式。例如,尝试调用
showMethods(summary)
。根据加载的包,您应该会看到返回的多个方法

您可以很容易地在R-Forge上看到它的源代码:。这是该函数的范围:

setGeneric("TSconnect", def= function(drv, dbname, ...) standardGeneric("TSconnect"))

setMethod("TSconnect",   signature(drv="character", dbname="character"),
   definition=function(drv, dbname, ...)
             TSconnect(dbDriver(drv), dbname=dbname, ...))

S4教程和r-forge存储库的链接非常有用。我对我的问题进行了编辑,以使其更清晰。您可能会发现它们很有用。