[R] how do you know which functions are being debugged?

Duncan Murdoch murdoch at stats.uwo.ca
Sat Oct 24 01:06:06 CEST 2009

Gabor Grothendieck wrote:
> On Fri, Oct 23, 2009 at 2:52 PM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> On 10/23/2009 2:49 PM, Andrew Yee wrote:
>>> Thanks everyone for the responses.  As a suggestion, wouldn't it be
>>> useful,
>>> that when you run isdebugged() without any parameters, it would just
>>> report
>>> back the functions that are being flagged for debugging?
>> Could you give an example of what the answer would look like?  Not all
>> functions have names, not all names are unique, ...
> One could use the formal argument list of the function, i.e. the
> output of args, when there is no notion of name since that is always
> available.

It would actually be easier to return a copy of the whole function.  But 
this wouldn't tell you how to turn off debugging:  for example, if an 
environment captured a copy of a function with the debugging bit set, 
then you really need to know where it was found, not just what was 
found.  It's tricky to put together examples of this because debug(f) 
doesn't act like a normal function, but here's an example showing some 
of the weird behaviour:

f <- function(x) x^2
g <- f

# now both g and f have the debug bit set.  Should both be returned by 
the "showDebugged" function?


# that unset both.  Now undebug(g) would give a warning.


# that set both

f <- function(x) x^2

# Now g has it set, but f doesn't.

In this example, I was doing explicit assignments, but if f had been 
passed as an argument to a function, an implicit assignment would be 
made to the local variable holding it, and the same sort of behaviour 
would result.  I think this would be very confusing, and not 
particularly helpful, in lots of cases.

I think Steve's suggestion is really the right way to go:  if you're 
going to be setting lots of debug flags, then keep track of them 
yourself, and use your records to unset them. 

Duncan Murdoch

More information about the R-help mailing list