title: Can the R console support background tasks or interrupts (event-handling)?
body: <p>While working in an R console, I'd like to set up a background task that monitors a particular connection and when an event occurs, another function (an alert) is executed.  Alternatively, I can set things up so that an external function simply sends an alert to R, but this seems to be the same problem: it is necessary to set up a listener.</p>
  
  <p>I can do this in a dedicated process of R, but I don't know if this is feasible from within a console.  Also, I'm not interested in interrupting R if it is calculating a function, but alerting or interrupting if the console is merely waiting on input.</p>
  
  <p>Here are three use cases:</p>
  
  <ol>
  <li><p>The simplest possible example is watching a file.  Suppose that I have a file called "latestData.csv" and I want to monitor it for changes; when it changes, <code>myAlert()</code> is executed.  (One can extend it to do different things, but just popping up with a note that a file has changed is useful.)</p></li>
  <li><p>A different kind of monitor would watch for whether a given machine is running low on RAM and might execute a <code>save.image()</code> and terminate.  Again, this could be a simple issue of watching a file produced by an external monitor that saves the output of <code>top</code> or some other command.</p></li>
  <li><p>A different example is like another recent SO question, about : <a href="https://stackoverflow.com/questions/7273616/have-r-halt-the-ec2-machine-its-running-on">have R halt the EC2 machine it&#39;s running on</a>.  If an alert from another machine or process tells the program to save &amp; terminate, then being able to listen for that alert would be great.</p></li>
  </ol>
  
  <p>At the moment, I suspect there are two ways of handling this: via Rserve and possibly via <code>fork</code>.  If anyone has examples of how to do this with either package or via another method, that would be great.  I think that solving any of these three use cases would solve all of them, modulo a little bit external code.</p>
  
  <hr>
  
  <p>Note 1: I realize, <a href="https://stackoverflow.com/questions/2689900/pass-commands-to-an-running-r-runtime/2693059#2693059">per this answer to another SO question</a> that R is single threaded, which is why I suspect <code>fork</code> and Rserve may work.  However, I'm not sure about feasibility if one is interfacing with an R terminal.  Although R's REPL is attached to the input from the console, I am trying to either get around this or mimic it, which is where <code>fork</code> or <code>Rserve</code> may be the answer.</p>
  
  <p>Note 2: For those familiar with event handling / eventing methods, that would solve everything, too.  I've just not found anything about this in R.</p>
  
  <hr>
  
  <p>Update 1: I've found that the manual for writing R extensions <a href="http://cran.r-project.org/doc/manuals/R-exts.html#Meshing-event-loops" rel="nofollow noreferrer">has a section referencing event handling</a>, which mentions the use of <code>R_PolledEvents</code>.  This looks promising.</p>