From jmc at research.bell-labs.com  Tue Aug 12 13:11:27 2003
From: jmc at research.bell-labs.com (John Chambers)
Date: Tue Aug 12 18:11:47 2003
Subject: [Rd] tracing nonstandardGenericFunction
References: <Pine.LNX.3.96.1030812120527.31272D-100000@thorin.ci.tuwien.ac.at>
Message-ID: <3F3911AF.556937DD@research.bell-labs.com>

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@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@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@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@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-devel

-- 
John M. Chambers                  jmc@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


[Rd] tracing nonstandardGenericFunction
John Chambers