title: How to force an error if non-finite values (NA, NaN, or Inf) are encountered
body: <p>There's a conditional debugging flag I miss from Matlab: <code>dbstop if infnan</code> <a href="http://www.mathworks.com/help/techdoc/ref/dbstop.html" rel="nofollow noreferrer">described here</a>.  If set, this condition will stop code execution when an <code>Inf</code> or <code>NaN</code> is encountered (IIRC, Matlab doesn't have NAs).</p>
  
  <p>How might I achieve this in R in a more efficient manner than testing all objects after every assignment operation?</p>
  
  <p>At the moment, the only ways I see to do this are via hacks like the following:</p>
  
  <ol>
  <li>Manually insert a test after all places where these values might be encountered (e.g. a division, where division by 0 may occur).  The testing would be to use <code>is.finite()</code>, <a href="https://stackoverflow.com/questions/7518245/one-function-to-detect-nan-na-inf-inf-etc">described in this Q &amp; A</a>, on every element.</li>
  <li>Use <code>body()</code> to modify the code to call a separate function, after each operation or possibly just each assignment, which tests all of the objects (and possibly all objects in all environments).</li>
  <li>Modify R's source code (?!?)</li>
  <li>Attempt to use <code>tracemem</code> to identify those variables that have changed, and check only these for bad values.</li>
  <li>(New - see note 2) Use some kind of call handlers / callbacks to invoke a test function.</li>
  </ol>
  
  <p>The 1st option is what I am doing at present.  This is tedious, because I can't guarantee I've checked everything.  The 2nd option will test everything, even if an object hasn't been updated.  That is a massive waste of time.  The 3rd option would involve modifying assignments of NA, NaN, and infinite values (+/- Inf), so that an error is produced.  That seems like it's better left to R Core.  The 4th option is like the 2nd - I'd need a call to a separate function listing all of the memory locations, just to ID those that have changed, and then check the values; I'm not even sure this will work for all objects, as a program may do an in-place modification, which seems like it would not invoke the <code>duplicate</code> function.</p>
  
  <p>Is there a better approach that I'm missing?  Maybe some clever tool by Mark Bravington, Luke Tierney, or something relatively basic - something akin to an <code>options()</code> parameter or a flag when compiling R?</p>
  
  <p><strong>Example code</strong> Here is some very simple example code to test with, incorporating the <code>addTaskCallback</code> function proposed by Josh O'Brien. The code isn't interrupted, but an error does occur in the first scenario, while no error occurs in the second case (i.e. <code>badDiv(0,0,FALSE)</code> doesn't abort).  I'm still investigating callbacks, as this looks promising.</p>
  
  <pre><code>badDiv  &lt;- function(x, y, flag){
      z = x / y
      if(flag == TRUE){
          return(z)
      } else {
          return(FALSE)
      }
  }
  
  addTaskCallback(stopOnNaNs)
  badDiv(0, 0, TRUE)
  
  addTaskCallback(stopOnNaNs)
  badDiv(0, 0, FALSE)
  </code></pre>
  
  <hr>
  
  <p>Note 1.  I'd be satisfied with a solution for standard R operations, though a lot of my calculations involve objects used via <code>data.table</code> or <code>bigmemory</code> (i.e. disk-based memory mapped matrices).  These appear to have somewhat different memory behaviors than standard matrix and data.frame operations.</p>
  
  <p>Note 2.  The callbacks idea seems a bit more promising, as this doesn't require me to write functions that mutate R code, e.g. via the <code>body()</code> idea.</p>
  
  <p>Note 3.  I don't know whether or not there is some simple way to test the presence of non-finite values, e.g. meta information about objects that indexes where NAs, Infs, etc. are stored in the object, or if these are stored in place.  So far, I've tried Simon Urbanek's <code>inspect</code> package, and have not found a way to divine the presence of non-numeric values.</p>
  
  <p><strong>Follow-up:</strong> Simon Urbanek has pointed out in a comment that such information is not available as meta information for objects.</p>
  
  <p>Note 4. I'm still testing the ideas presented. Also, as suggested by Simon, testing for the presence of non-finite values should be fastest in C/C++; that should surpass even compiled R code, but I'm open to anything.  For large datasets, e.g. on the order of 10-50GB, this should be a substantial savings over copying the data.  One may get further improvements via use of multiple cores, but that's a bit more advanced.</p>