11

How can I view the definition of a S4 function? For instance, I would like to see the definition of TSconnect in package TSdbi. The command

showMethods("TSconnect")

reveals that there is, among others, a function for drv="histQuoteDriver", dbname="character".

How can I see the definition of this function? If it were a S3 function, there would be only the first argument definable (drv), which could be inspected with print(TSconnect.histQuoteDriver).

Edit: From r-forge I found out the desired output:

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 ) 
   } )

Is there a way to get this definition from within an R session?

4

1 回答 1

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?rev=70&root= tsdbi&view=标记。这是该功能的范围:

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

setMethod("TSconnect",   signature(drv="character", dbname="character"),
   definition=function(drv, dbname, ...)
             TSconnect(dbDriver(drv), dbname=dbname, ...))
于 2010-01-31T18:05:29.947 回答