检查 S4 方法

发布于 2024-08-20 06:06:50 字数 1357 浏览 5 评论 0原文

如何查看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 ) 
   } )

有没有办法从 R 会话中获取此定义?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜访吸血鬼 2024-08-27 06:06:50

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=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, ...))

S4 classes are relatively complicated, so I would suggest reading this introduction.

In this case, TSdbi is an example of a generic S4 class that gets extended by all the specific databases packages (e.g. TSMySQL, TSPostgreSQL, etc.). There is nothing more to the TSconnect() method in TSdbi than what you're seeing: drv="character", dbname="character" are parameters to the function, not functions in and of themselves. If you install some of the specific database packages and use showMethods("TSconnect") you will see all the specific instances of that function. If you then call TSconnect() with a specific database driver it will go and use the appropriate function.

This is how functions such as summary work too. For instance, try calling showMethods(summary). Depending upon which packages are loaded, you should see multiple methods returned

You can easily see the source code for it on R-Forge: http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/pkg/TSdbi/R/TSdbi.R?rev=70&root=tsdbi&view=markup. This is the extent of that function:

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

setMethod("TSconnect",   signature(drv="character", dbname="character"),
   definition=function(drv, dbname, ...)
             TSconnect(dbDriver(drv), dbname=dbname, ...))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文