title: Sink does not release file
body: <p>I know that the <code>sink()</code> function can be used to divert R output into a file, e.g.</p>
  
  <pre><code>sink('sink-closing.txt')
  cat('Hello world!')
  sink()
  </code></pre>
  
  <p>Is there a simple command to close all outstanding sinks?</p>
  
  <p>Below, I elaborate on my question.</p>
  
  <p>Suppose that my R-script opens a <code>sink()</code> in an R-script, but there is an error in the R-script which occurs before the script closes the <code>sink()</code>. I may run the R-script multiple times, trying to fix the error. Finally, I want to close all the sinks and print to the console. How do I do so?</p>
  
  <p>Finally, in the interest of concreteness, I provide a MWE to illustrate the problem I face.</p>
  
  <p>First, I write an R-script <code>sink-closing.R</code> which has an error in it.</p>
  
  <pre><code>sink('sink-closing.txt')
  
  foo &lt;- function() {
    cat(sprintf('Hello world! My name is %s\n', 
                a.variable.that.does.not.exist))
  }
  
  foo()
  
  sink()
  </code></pre>
  
  <p>Next, I <code>source</code> the R-script multiple times, say 3 times by mistake as I try to find and fix the bug.</p>
  
  <pre><code>&gt; source('~/Dropbox/cookbook/r-cookbook/sink-closing.R')
  Error in sprintf("Hello world! My name is %s\n", a.variable.that.does.not.exist) : 
    object 'a.variable.that.does.not.exist' not found
  </code></pre>
  
  <p>Now, suppose that I am debugging the R-script and want to print to the console. I can call <code>sink()</code> multiple times to close the earlier sinks. If I call it 3 times, then I can finally print to the console as before. But how do I know how many sinks I need to close?</p>