title: How to stop a function in R that is taking too long and give it an alternative?
body: <p>I'm trying to do a thing "the right way".  Sometimes "the right way" takes too long, depending on the inputs.  I can't really know a priori when this will be.  When "the right way" is taking too long, I want to go to "the hackish way".  How do I make R monitor how long a particular task as taken, and give it something else to do if a threshold has passed?  I'd imagine that this will be part of the <code>try</code> family, but I'm not quite sure what to call it or google for.</p>
  
  <p>Dummy example below.  When <code>slow.func</code> takes too long, I want <code>interuptor</code> to stop it and call <code>fast.func</code> instead.</p>
  
  <pre><code>slow.func &lt;- function(x){
      Sys.sleep(x)    
      print('good morning')
  }
  
  fast.func &lt;- function(x){
      Sys.sleep(x/10) 
      print('hit snooze')
  }
  
  interuptor = function(FUN,args, time.limit, ALTFUN){
  #   START MONITORING TIME HERE
      do.call(FUN,args)
  #   IF FUN TAKES TOO LONG, STOP IT, CALL A
      do.call(ALTFUN,args)
  }
  
  interuptor(slow.func, list(x = 2), time.limit = 1, fast.func)
  </code></pre>