Skip to content
Prev 39698 / 63421 Next

How to debug reference classes?

This is a good wish-list item.  The natural mechanism would be a version 
of the standard trace() function as a reference method with the same 
arguments as the current trace(), minus those that make no sense.  So:
    xx$trace(edit, browser)
for example, to trace execution of the reference method "edit" defined 
in the class of xx.  The mechanism does not exist now, and will require 
some modifications or extensions to the existing trace() implementation.

Meanwhile, the following slightly ugly workaround seems to apply trace() 
to a reference method.  Here, xx is the object created in the example 
for ReferenceClasses, with method $edit().  [Actually running the 
example removes the class definition, so this was done from a copy of 
the source code for the example.]

The steps in the workaround:

- make a copy of the method,
   edit <- xx$edit

- arrange to trace it in xx:
   trace(edit, browser, where = xx)
(this produces a note and a warning, but they seem harmless)

- remove the copy (just to be safe)
   rm(edit)

- now run things with whatever trace action you speficied.

- if needed after debugging, untrace() the method.
   untrace("edit", where = xx)

An example is below.

Of course, one could also just define xx$edit to call a regular 
function, say myEdit() and trace that.  But the workaround doesn't 
require changing the existing definition.

Suggestions for a better or less ugly workaround are welcome.  I'll look 
at fixing up a trace() method for 2.13.1

John

==============================
 > edit <- xx$edit
 > trace(edit, browser, where = xx)
Constructing traceable class "refMethodDefWithTrace"
Environment of class "refMethodDef" is locked; using global environment 
for new class
Tracing function "edit" in package "2011-04-07 14:34:43"
[1] "edit"
Warning message:
In getPackageName(whereF) :
   Created a package name, "2011-04-07 14:34:43", when none found
 > rm(edit)
 > xx$edit(1,2,3)
Tracing xx$edit(1, 2, 3) on entry
Called from: eval(expr, envir, enclos)
Browse[1]> objects()
[1] "i"     "j"     "value"
Browse[1]> j
[1] 2
Browse[1]> j <- 3
Browse[1]>
 > xx$data
      [,1] [,2] [,3]
[1,]    1    5    3
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
 > untrace("edit", where = xx)
Untracing function "edit" in package "2011-04-07 14:34:43"
 > xx$edit(1,2,-1)
 > xx$data
      [,1] [,2] [,3]
[1,]    1   -1    3
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

============================================
On 4/7/11 12:00 PM, A Zege wrote: