[R] Debug S4 Methods

John Chambers jmc at research.bell-labs.com
Fri Jul 30 15:46:38 CEST 2004


Gunnar Wrobel wrote:
> 
> Dear list,
> 
> is there any way to debug the S4 methods using the debug() function in
> R1.9.1?
> 
> I am able to use browser() inside a S4 method but I wondered if there is
> a way to debug the function without recompiling the package.

Yes.  Use the trace() function to insert calls to browser or other
debugging code into the method.

trace() takes an argument signature= corresponding to the signature in
the call to setMethod for the method you want to debug.

See the documentation for trace.

As an example, if you had set methods for plot as in the example of the
setMethod documentation, the following will insert  calls to browser in
the plot method shown, one call on entry and one on exit:

R> setMethod("plot", signature(x = "track", y = "missing"), 
  ......

R> trace("plot", browser, exit=browser, signature = c("track",
"missing"))
R> plot(t1)
Tracing plot(t1) on entry 
Called from: plot(t1)
Browse[1]> objects()
[1] "x" "y"
   ....

As an alternative to browser, the function recover works well with
trace(), if you want to browse in other calls in addition to the method
itself.  You might also experiment with options(error=recover).


> 
> Thanks!
> 
> Gunnar
> 
> --
> ----------------------------------------------
> Dr. Gunnar Wrobel
> 
> Divisions of Bioinformatics and Biochemistry
> Swiss Institute of Bioinformatics/Biozentrum
> Klingelbergstrasse 50/70
> CH-4056 Basel / Switzerland
> Tel.: +41 61 267 1579
> Fax:  +41 61 267 3398
> e-mail: work at gunnarwrobel.de
> e-mail: gunnar.wrobel at unibas.ch
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
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-help mailing list