title: How to capture the output of system()
body: <p>This question was motivated by <a href="https://stackoverflow.com/q/27388964/559676">Rmarkdown not outputting results of system command to html file</a>. For some reason, the output of <code>system()</code> in R (or <code>system2()</code>) cannot be captured by <code>sink()</code> or <code>capture.output()</code>, so currently there is no way for <strong>knitr</strong> to record the output. For example, in the R console:</p>
  
  <pre><code>&gt; system('ls')
  DESCRIPTION
  NAMESPACE
  R
  README.md
  inst
  man
  </code></pre>
  
  <p>but in a <strong>knitr</strong> document, you won't see the output, because <code>capture.output(system('ls'))</code> is <code>character(0)</code>, i.e. the output cannot be captured. Of course I can do <code>cat(system('ls', intern = TRUE), sep = '\n')</code> as I mentioned in the answer of that question, but this is kind of awkward. I wonder if it is a way to capture the output of <code>system()</code> without using <code>intern = TRUE</code> and <code>cat()</code>. </p>
  
  <hr>
  
  <p><strong>Update</strong>: see <a href="https://github.com/yihui/knitr/issues/1203" rel="noreferrer">https://github.com/yihui/knitr/issues/1203</a> for a hack that I provided to solve the problem.</p>