title: How to source R Markdown file like `source('myfile.r')`?
body: <p>I often have a main R Markdown file or knitr LaTeX file where I <code>source</code> some other R file (e.g., for data processing). However, I was thinking that in some instances it would be beneficial to have these sourced files be their own reproducible documents (e.g., an R Markdown file that not only includes commands for data processing but also produces a reproducible document that explains the data processing decisions).</p>
  
  <p>Thus, I would like to have a command like <code>source('myfile.rmd')</code> in my main R Markdown file. that would extract and source all the R code inside the R code chunks of <code>myfile.rmd</code>. Of course, this gives rise to an error. </p>
  
  <p>The following command works:</p>
  
  <pre><code>```{r message=FALSE, results='hide'}
  knit('myfile.rmd', tangle=TRUE)
  source('myfile.R')
  ```
  </code></pre>
  
  <p>where <code>results='hide'</code> could be omitted if the output was desired. I.e., knitr outputs the R code from <code>myfile.rmd</code> into <code>myfile.R</code>.</p>
  
  <p>However, it doesn't seem perfect:</p>
  
  <ul>
  <li>it results in the creation of an extra file</li>
  <li>it needs to appear in its own code chunk if control over the display is required.</li>
  <li>It's not as elegant as simple <code>source(...)</code>.</li>
  </ul>
  
  <p>Thus my question:
  <strong>Is there a more elegant way of sourcing the R code of an R Markdown file?</strong></p>