[Rd] tracing nonstandardGenericFunction

John Chambers jmc at research.bell-labs.com
Tue Aug 12 13:11:27 MEST 2003


Torsten Hothorn wrote:
> 
> Hi,
> 
> how can one trace a nonstandardGenericFunction,
> especially "initialize"? An example:
> 
> setClass("dummy", representation(a = "numeric"))
> 
> setMethod("initialize", "dummy",
>           function(.Object, a = 2) { ### I want to trace this function
>               .Object at a <- a
>               .Object
>           })

Your problem is not with nonstandard generic functions.  It comes from
giving setMethod a function whose argument list is NOT identical to the
argument list for the generic function.

In this case, your method has formal arguments (.Object, a), but

R> formalArgs(initialize)
[1] ".Object" "..."    

This is allowed, but technically the method MUST  have the same
arguments as the generic.  So the mechanism used is to turn the supplied
method into a function, .local, in the actual method, and to call this
function to re-match arguments.  Tracing the method traces the method,
not the .local function.

In the future, we can likely make tracing work in the intuitive way for
such methods.

Meanwhile, you need to set things up yourself, by using an explicit
function and having the method call that function.

R> initDummy <- function(.Object, a = 2) {
+              .Object at a <- a
+              .Object
+          }
R> setMethod("initialize", "dummy", function(.Object,
...)initDummy(.Object, ...))
[1] "initialize"
R> trace(initDummy, browser, exit = browser)
[1] "initDummy"
R> new("dummy", 1.5)
Tracing initDummy(.Object, ...) on entry 
Called from: initDummy(.Object, ...)
Browse[1]> objects()
[1] "a"
Browse[1]> a
[1] 1.5



> 
> setMethod("show", "dummy", function(object) print(object at a))
> 
> b <- new("dummy", a = 3)
> 
> trace("show", browser, signature = "dummy") ### trace method "show" to
>                                             ### class "dummy"
> 
> show(b) ### works fine
> 
> trace("initialize", browser, signature = "dummy")
> 
> R> b <- new("dummy") ### does not trace the function of interest ...
> Tracing initialize(value, ...) on entry
> Called from: initialize(value, ...)
> 
> Best,
> 
> Torsten
> 
> ______________________________________________
> R-devel at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc



More information about the R-devel mailing list