Re: [Rd] How to debug reference classes?

From: John Chambers <>
Date: Thu, 07 Apr 2011 18:12:17 -0700

Good to know. However, _please_ don't use the horrible kludge in the attr(..) expression. From my experimenting, it worked fine just to say:

mtrace(edit, from = xx)

and even if that did not work, from = as.environment(xx) is identical in effect to the attr() expression and means something. (Usually the coercion to "environment" happens automatically.)

The attr() expression is strongly deprecated and very much not guaranteed to work. First, it's discouraged to use the .xData slot, which is part of the implementation and not part of the API. And ditto to access _any_ slot by attr(), for the same reason.

However, in my experimenting the technique required xx$edit to have been evaluated at some point before the call to mtrace(). Reference methods are copied to the object when first required (for efficiency). Because my workaround explicitly used xx$edit, the problem didn't arise. Just evaluating xx$edit should be enough.

On the "before instantiated" point. I assume you mean in order to trace the method in all objects generated from the reference class. I had thought about that too. The same mechanism I described in my previous mail works for this as well, but requires a kludge to get the environment containing the methods. The steps I outlined are as before but modified (in the example) as follows:

 > mm =  mEditor$def_at_refMethods
 > edit = mm$edit
 > trace(edit, browser, where = mm)

(mm is the environment with the methods). Then the objects generated by mEditor$new() will have the traced version.

The same technique didn't seem to work for mtrace(), but a modification might.


On 4/7/11 4:56 PM, wrote:
> 'mtrace' will work with reference classes, at least after an object is instantiated. I'm not familiar with the guts of reference classes, but the following quick experiment was successful.. If you run the example in '?ReferenceClasses' up to& including this line :
> xx<- mEditor$new(data = xMat)
> and then do this:
> mtrace( edit, from=attr( xx, '.xData'))
> and then run the next line of the example, which is
> xx$edit(2, 2, 0)
> then the debug window will come up as normal.
> Now, what about if you want to mtrace 'edit' before objects are instantiated? Here the S4 structure defeated me temporarily, but I probably would have been able to beat it if I'd had more time... There are some notes on debugging S4 methods in 'package?debug' (note that '?mtrace' itself is out-of-date on S4-- I have gotten S4 debugging to work, but it's only described in 'package?debug') and that might be enough to get you going.
> Mark ('debug' package author)
> Mark Bravington
> Marine Lab
> Hobart
> Australia
> ________________________________________
> From: [] On Behalf Of A Zege []
> Sent: 08 April 2011 05:00
> To:
> Subject: [Rd] How to debug reference classes?
> How do you debug methods of a reference class? I've been using mtrace, which
> is excellent, but i cannot figure out how to mtrace a reference class
> method. Maybe there is some other way to debug these, for example with
> ordinary trace? for now i am only able to use options(error=recover), which
> is not giving me idea where exactly in the code i am once i am stopped on an
> error.
> --
> View this message in context:
> Sent from the R devel mailing list archive at
> ______________________________________________
> mailing list
> ______________________________________________
> mailing list
> mailing list Received on Fri 08 Apr 2011 - 01:17:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 08 Apr 2011 - 07:00:42 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive