Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. <a href="http://web.archive.org/web/20070607153252/http://www.ibm.com/developerworks/web/library/wa-debug.html" rel="noreferrer">read this nice article</a>). For instance, the first thing is to <em>reproduce the problem</em>...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 <em>reduce</em> it down to the source. </p> <p>Rather than a "trick", I would say that I have a favorite debugging routine:</p> <ol> <li>When an error occurs, the first thing that I usually do is look at the stack trace by calling <code>traceback()</code>: that shows you where the error occurred, which is especially useful if you have several nested functions.</li> <li>Next I will set <code>options(error=recover)</code>; this immediately switches into browser mode where the error occurs, so you can browse the workspace from there.</li> <li>If I still don't have enough information, I usually use the <code>debug()</code> function and step through the script line by line. </li> </ol> <p>The best new trick in R 2.10 (when working with script files) is to use the <code>findLineNum()</code> and <code>setBreakpoint()</code> functions. </p> <p>As a final comment: depending upon the error, it is also very helpful to set <code>try()</code> or <code>tryCatch()</code> 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.</p> <p>These related questions have a lot of suggestions:</p> <ul> <li><a href="https://stackoverflow.com/questions/1169480/debugging-tools-for-the-r-language">Debugging tools for the R language</a></li> <li><a href="https://stackoverflow.com/questions/1395622/debugging-lapply-sapply-calls">Debugging lapply/sapply calls</a></li> <li><a href="https://stackoverflow.com/questions/1412582/getting-the-state-of-variables-after-an-error-occurs-in-r">Getting the state of variables after an error occurs in R</a></li> <li><a href="https://stackoverflow.com/questions/1445964/r-script-line-numbers-at-error">R script line numbers at error?</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload