[R] FW: Debug package question

Matthew Dowle mdowle at concordiafunds.com
Mon Oct 23 15:24:35 CEST 2006


Dear list,

I received the response below from the package author of 'debug'.  I
post it to the list, with Mark's approval, in case it is useful to
others too.

Regards, Matthew


-----Original Message-----
From: Mark.Bravington at csiro.au [mailto:Mark.Bravington at csiro.au] 
Sent: 22 October 2006 23:53
To: Matthew Dowle
Cc: Mark.Bravington at csiro.au
Subject: RE: Debug package question


Hi Matthew

That's an interesting point. If it's your own code, my recommendation
would be to predefine the function and then mtrace it, as you suggest.
But I can see that this isn't always easy, e.g. if you're trying to
debug a system function.

Because every little operation in R is actually a function call, it's
probably not very useful for 'mtrace' to have a step-into mode. [Anyway,
whether useful or not, it doesn't have one! ;)] 

The simplest way to get what you're after-- but it might not work--
would be to 'mtrace(apply)' and then call 'mtrace( FUN)' manually when
the debug window for 'apply' appears. The snag is that 'apply' is quite
likely to feature somewhere in the workings of 'debug' (I can't
remember!) so you might hang the thing altogether. Also, if your own
function happens to call 'apply' (directly or indirectly) you will get
stuck with a lot of debug windows.

A more robust, complicated, and devious way to get what you're after, is
to write a version of 'apply' that automatically calls 'mtrace' on its
function-argument and then invokes the real 'apply'. Something like
this:

mtrace.apply <- function( X, MARGIN, FUN, ...) {
  FFF <- FUN # don't know whether this is necessary
  mtrace( FFF) # naughty-- should really arrange for name to be unique
  mc <- match.call( expand.dots=TRUE)
  mc$FUN <- FFF
  mc[[1]] <- quote( apply)
  eval( mc, envir=parent.frame())
}

Then call 'mtrace.apply' instead of 'apply'.

BTW: if your pass-in function isn't crashing the first time it's called,
you might find it useful to use the 'bp(1,F)' trick the first time it
appears in a debug window-- this means that it won't pop the window up
and wait for input until/unless there's a crash.

Hope this helps-- and glad you like the package

Mark

Mark Bravington
CSIRO Mathematical & Information Sciences
Marine Laboratory
Castray Esplanade
Hobart 7001
TAS

ph (+61) 3 6232 5118
fax (+61) 3 6232 5012
mob (+61) 438 315 623
 

> -----Original Message-----
> From: Matthew Dowle [mailto:mdowle at concordiafunds.com]
> Sent: Friday, 20 October 2006 8:20 PM
> To: Bravington, Mark (CMIS, Hobart)
> Subject: Debug package question
> 
> 
> > Hi Mark,
> > 
> > I've been using your excellent debug for a while now.  Its truly
> > excellent.
> > 
> > The most common reason I find it useful is when R returns
> "subscript
> > out of bounds error".  R provides no context information in the
> > message so I just mtrace() the function and your package 
> tells me the
> > line straight away.  Perfect.
> > 
> > I have one question ... when I use the apply() family to apply a
> > function and the error occurs inside the function *defined* 
> inside the
> > function,  e.g. :
> > 
> > 	X = matrix(...)
> > 	apply(X, 2, function(x) {
> > 		< lots of lines >
> > 		... M[m,] ...
> > 		< lots of lines >
> > 		... N[n,] ...
> > 		< lots of lines >
> > 		... O[o,] ...
> > 		< lots of lines >
> > 	})
> > 
> > Where it is the M[m,],  N[n,] or O[o,]  which generates the "out of
> > bounds" on a particular iteration,  but you don't know which one.
> > 
> > Mtrace() will skip over the apply() and say the same as R,
> that an out
> > of bounds has occurred somewhere, but not where,  since
> mtrace has not
> > been called on the "function(x)".
> > 
> > One way to work around this, is to define the sub-function
> first with
> > a name,   call mtace() on it,  then execute the apply.    
> Is that the
> > only way?   Is there a way to set mtrace() to automatically mtrace()
> > any function it calls,  like "step into" and "step over" in other
> > languages ?
> > 
> > Regards,
> > Matthew
> > 
> > 
> > > version
> >          _              
> > platform i386-pc-mingw32
> > arch     i386           
> > os       mingw32        
> > system   i386, mingw32  
> > status                  
> > major    2              
> > minor    1.1            
> > year     2005           
> > month    06             
> > day      20             
> > language R              
> > > 
> > 
> > and also
> > 
> > > version
> >          _                       
> > platform x86_64-unknown-linux-gnu
> > arch     x86_64                  
> > os       linux-gnu               
> > system   x86_64, linux-gnu       
> > status                           
> > major    2                       
> > minor    1.1                     
> > year     2005                    
> > month    06                      
> > day      20                      
> > language R                       
> > > 
> > 
> > 
> 
>



More information about the R-help mailing list