title: Redirect all plot output to specific file
body: <p><strong>I want to automatically redirect <em>all</em> plotting to a file</strong> (reason: see below). Is there a non-hacky way of accomplishing that?</p>
  
  <p>Lacking that, I’m actually not afraid of overriding the built-in functions, I’m <em>that</em> desperate. The easiest way I can think of is to hook into the fundamental plot-window creation function and calling <code>pdf(…)</code> and then hooking into the plot-finalising function and calling <code>dev.off()</code> there.</p>
  
  <p>But what are these functions? Through debugging I’ve tentatively identified <code>dev.hold</code> and <code>dev.flush</code> – but is this actually true universally? Can I hook into those functions? I cannot override them with <code>R.utils</code>’ <code>reassignInNamespace</code> because they are locked, and just putting same-name functions into the global namespace doesn’t work (they’re ignored by <code>plot</code>).</p>
  
  <hr>
  
  <p>So, <strong>why</strong> would I want to do something so horrible?</p>
  
  <p>Because I’m working on a remote server and despite my best attempts, and long debugging sessions with our systems support, I cannot get X11 forwarding to work reliably. Not being able to preview a plot is making my workflow horribly inefficient. I’ve given up on trying to get X11 to work so now I’m creating PDFs in my <code>public_html</code> folder and just refresh the browser.</p>
  
  <p>This works quite well – except that it‘s really annoying and quite time-consuming to always have to surround your plotting function calls with <code>pdf(…) … dev.off()</code>, especially in interactive sessions where you want to quickly create a plot while in a meeting with collaborators. In fact, it’s really annoying and they (understandably) haven’t got the patience for that.</p>
  
  <p>For now I’m helping myself with the following function definition:</p>
  
  <pre><code>preview &lt;- function (.expr, ...) {
      on.exit(dev.off())
      pdf(PREVIEW_FILE_NAME, ...)
      eval(substitute(.expr))
  }
  </code></pre>
  
  <p>Which is used like this:</p>
  
  <pre><code>preview(plot(1:100, rnorm(100) * 1:100))
  </code></pre>
  
  <p>That works a-ok. But <em>this workflow is a real bottleneck</em> in meetings, and I’d like to get rid of the <code>preview</code> call to streamline it as much as possible.</p>
  
  <p>Any chance at all?</p>