Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is your favorite R debugging trick? [duplicate]

Tags:

r

r-faq

debugging

I get an error when using an R function that I wrote:

Warning messages: 1: glm.fit: algorithm did not converge  2: glm.fit: algorithm did not converge  

What I have done:

  1. Step through the function
  2. Adding print to find out at what line the error occurs suggests two functions that should not use glm.fit. They are window() and save().

My general approaches include adding print and stop commands, and stepping through a function line by line until I can locate the exception.

However, it is not clear to me using those techniques where this error comes from in the code. I am not even certain which functions within the code depend on glm.fit. How do I go about diagnosing this problem?

like image 394
David LeBauer Avatar asked Dec 14 '10 18:12

David LeBauer


People also ask

How do I debug a code in R?

The most common (and easiest) way to stop on a line of code is to set a breakpoint on that line. You can do this in RStudio by clicking to the left of the line number in the editor, or by pressing Shift+F9 with your cursor on the desired line. We call this an “editor breakpoint”.

How do I see errors in R?

You can use traceback() to locate where the last error occurred. Usually it will point you to a call you make in your function. Then I typically put browser() at that point, run the function again and see what is going wrong. The number means how deep we are in the nested functions.

How do I exit debug in R?

c to leave the debug mode and continue with the regular execution of the function. Q to stop debug mode, terminate the function, and return to the R prompt.


2 Answers

I'd say that debugging is an art form, so there's no clear silver bullet. There are good strategies for debugging in any language, and they apply here too (e.g. read this nice article). For instance, the first thing is to reproduce the problem...if you can't do that, then you need to get more information (e.g. with logging). Once you can reproduce it, you need to reduce it down to the source.

Rather than a "trick", I would say that I have a favorite debugging routine:

  1. When an error occurs, the first thing that I usually do is look at the stack trace by calling traceback(): that shows you where the error occurred, which is especially useful if you have several nested functions.
  2. Next I will set options(error=recover); this immediately switches into browser mode where the error occurs, so you can browse the workspace from there.
  3. If I still don't have enough information, I usually use the debug() function and step through the script line by line.

The best new trick in R 2.10 (when working with script files) is to use the findLineNum() and setBreakpoint() functions.

As a final comment: depending upon the error, it is also very helpful to set try() or tryCatch() statements around external function calls (especially when dealing with S4 classes). That will sometimes provide even more information, and it also gives you more control over how errors are handled at run time.

These related questions have a lot of suggestions:

  • Debugging tools for the R language
  • Debugging lapply/sapply calls
  • Getting the state of variables after an error occurs in R
  • R script line numbers at error?
like image 117
Shane Avatar answered Sep 21 '22 02:09

Shane


The best walkthrough I've seen so far is:

http://www.biostat.jhsph.edu/%7Erpeng/docs/R-debug-tools.pdf

Anybody agree/disagree?

like image 43
Christopher DuBois Avatar answered Sep 23 '22 02:09

Christopher DuBois