[R] Debugging a hanging function within R

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 16 16:05:13 CET 2007


Try this:

f <- function() {
   for(i in 1:1000) {
     if (i == 50) browser()
     print(i)
  }
}

# enters debugger when i is 50.
# n/c/Q will step one statement/continue/Quit respectively
f()

On Nov 16, 2007 6:58 AM, Yan Wong <h.y.wong at leeds.ac.uk> wrote:
> Hi,
>
> I have an R program which takes several days to run, and sometimes
> hangs while running, presumably stuck in some sort of loop within a
> package function. I don't know where in the program code it is
> hanging: it is likely to be within a routine that is iterated many
> hundreds of thousands of times, each time with different random
> numbers used.
>
> I'd like to debug the R code, but I can't simply set a breakpoint on
> the function, as it works fine for the first n-thousand times. What
> I'd really like to do is to interrupt the running program once it has
> hung, then step into the function, examining the variables as I go.
>
> 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).
> Is it possible?
>
> Apologies if this is answered in the manuals, but I've had a trawl
> through and can't seem to find how to do it. I have tried attaching
> to the process using gdb, but then don't know how to issue gdb
> commands to invoke the R debugger in the stopped R session.
>
> Thanks
>
> Yan
>
> (n.b. R 2.5.1 on OS X, if that is relevant)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list