title: Getting the state of variables after an error occurs in R
body: <p>Let's say I have just called a function, <code>f</code>, and an error occurred somewhere in the function.  I just want to be able to check out the values of different variables right before the error occurred.  </p>
  
  <p>Suppose my gut tells me it's a small bug, so I'm too lazy to use <code>debug(f)</code> and too lazy to insert <code>browser()</code> into the part of the function where I think things are going wrong.  And I'm far too lazy to start putting in <code>print()</code> statements.</p>
  
  <p>Here's an example:</p>
  
  <pre><code>x &lt;- 1:5
  y &lt;- x + rnorm(length(x),0,1)
  f &lt;- function(x,y) {
    y &lt;- c(y,1)
    lm(y~x)
  }
  </code></pre>
  
  <p>Calling <code>f(x,y)</code> we get the following error:</p>
  
  <pre><code>Error in model.frame.default(formula = y ~ x, drop.unused.levels = TRUE) : 
    variable lengths differ (found for 'x')
  </code></pre>
  
  <p>In this example, I want grab the state of the environment just before <code>lm()</code> is called; that way I can call <code>x</code> and <code>y</code> and see that their lengths are different.  (This example may be too simple, but I hope it gets the idea across.)</p>