Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inspect S4 methods

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?

like image 977
Karsten W. Avatar asked Jan 31 '10 15:01

Karsten W.


1 Answers

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, ...))
like image 67
Shane Avatar answered Sep 28 '22 07:09

Shane