[R] Debugging a hanging function within R

Duncan Murdoch murdoch at stats.uwo.ca
Fri Nov 16 17:27:16 CET 2007


On 11/16/2007 9:48 AM, Yan Wong wrote:
> On 16 Nov 2007, at 14:16, Duncan Murdoch wrote:
> 
>> On 11/16/2007 6:58 AM, Yan Wong wrote:
>>> Hi,
>>> I can't seem to find a way to do this (i.e. interrupt a routine at  
>>> an  arbitrary time during its run, then step into it using the  
>>> debugger).
>>
>> If you prepare in advance by using options(error=recover), it's easy:
>>
>> > options(error=recover)
>> > f <- function() {
>> +  i <- 0
>> +  repeat {
>> +     i <- i + 1
>> +     x <- rnorm(10000)
>> +  }
>> + }
>> > f()
>>
>> # At this point, R appears hung up.  I hit Esc in the Windows  
>> Rgui.  In Unix or Rterm, you'd use Ctrl-C.
> 
> That's exactly what I wanted. Thank you. I didn't realise that  
> sending an interrupt signal was treated as a "normal" error.
> 
> Out of interest, is there any way to resume the routine from where it  
> was interrupted (and ideally do so in debugger mode, so I can step  
> through the lines of the code)?

I don't think so, I think you need to use explicit browser() or 
recover() calls in your code for that. It would be a nice addition to 
have a way to tell the interpreter to insert a recover() call before the 
executing the next statement (like a "Program Pause" command in some 
debuggers), but as far as I know there isn't a way to do that.

Duncan Murdoch



More information about the R-help mailing list