title: Include a specific chunk from one markdown document in another document
body: <p>I want to include the code from a specific chunk of a one markdown document into a second markdown doc. I want to do it by referring to the chunk by name (no <a href="https://stackoverflow.com/a/17605475/1036500">hacky references to line-numbers</a>, please). I don't want to run all the code in the child because some of it is rather time-consuming. </p>
  
  <p>Here's what I've tried. We have <a href="http://yihui.name/knitr/demo/externalization/" rel="nofollow noreferrer"><code>read_chunk</code></a> for including plain <code>R</code> script in markdown docs. There is <a href="http://www.inside-r.org/packages/cran/knitr/docs/run_chunk" rel="nofollow noreferrer"><code>run_chunk</code></a> but it's not clear if this can be used with external docs (I haven't had any luck so far).</p>
  
  <p>And we can do <a href="https://stackoverflow.com/a/17605475/1036500">this</a> to get an entire markdown doc to run inside another one:</p>
  
  <pre><code>```{r child='first.Rmd'}
  ```
  </code></pre>
  
  <p>But how can I get just one specific chunk from a child document into another doc? Here's a small example:</p>
  
  <p>This is <code>test-main.Rmd</code></p>
  
  <pre><code>```{r pick-up-the-kid, child='test-child.Rmd'}
  ```
  </code></pre>
  
  <p>And this is <code>test-child.Rmd</code></p>
  
  <pre><code>Hi, there. I'm a child.
  
  ```{r test-child-1}
  1+1
  dnorm(0)
  ```
  
  ```{r test-child-2}
  2+2
  dnorm(0)
  ```
  </code></pre>
  
  <p>When we run <code>test-main.Rmd</code> we get this:</p>
  
  <pre><code>Hi, there. I’m a child.
  
  1+1
  ## [1] 2
  dnorm(0)
  ## [1] 0.3989
  2+2
  ## [1] 4
  dnorm(0)
  ## [1] 0.3989
  </code></pre>
  
  <p>One method that almost does it is <a href="http://yihui.name/knitr/demo/reference/" rel="nofollow noreferrer"><code>ref.label</code></a>. If we edit <code>test-main.Rmd</code> like so:</p>
  
  <pre><code>```{r pick-up-the-kid, child='test-child.Rmd', ref.label='test-child-2'}
  ```
  </code></pre>
  
  <p>The output has the only the desired chunk, but it's duplicated, which is no good:</p>
  
  <pre><code>Hi, there. I’m a child.
  
  2+2
  ## [1] 4
  dnorm(0)
  ## [1] 0.3989
  2+2
  ## [1] 4
  dnorm(0)
  ## [1] 0.3989
  </code></pre>
  
  <p>One <a href="http://yihui.name/knitr/demo/reference/#comment-723275447" rel="nofollow noreferrer">solution</a> to the duplication is to use <code>eval = FALSE, echo = FALSE</code> in the chunk options in the child document:</p>
  
  <pre><code>```{r test-child-2, eval = FALSE, echo = FALSE}
  2+2
  dnorm(0)
  ```
  </code></pre>
  
  <p>which gives the desired result:</p>
  
  <pre><code>2+2
  ## [1] 4
  dnorm(0)
  </code></pre>
  
  <p>But this is not convenient to alter the chunk in the child document, since that chunk is needed by other chunks in the child doc, and I don't want to change with a few chunks in the child doc each time I run the main doc, it's not good for reproducibility. </p>
  
  <p><strong>How can I get <em>only</em> chunk <code>test-child-2</code> from <code>test-child.Rmd</code> into <code>test-main.Rmd</code> by reference to the chunk name (and without duplicates or fiddling with the chunk options)?</strong></p>
  
  <p>I'm looking for a function that could be called<code>child_chunk</code> where I can give the child doc name and chunk name and apply chunk options to it in the main doc that are independent of the chunk options in the child doc.</p>
  
  <p>Or is the only solution to move code into <code>R</code> script files and share them between the two markdown docs?</p>