title: Modify external R script in a knitr chunk
body: <p>I'm wondering if it's possible to hook into the code in an external R script that is read by <code>knitr</code>.</p>
  
  <p>Specifically, say that you have the following R file</p>
  
  <h3><code>test.R</code></h3>
  
  <pre class="lang-r prettyprint-override"><code>## ---- CarPlot
  library(ggplot2)
  CarPlot &lt;- ggplot() +
      stat_summary(data = mtcars,
                   aes(x = factor(gear),
                       y = mpg
                       ),
                   fun.y = "mean",
                   geom = "bar"
                   )
  CarPlot
  </code></pre>
  
  <p>Imagine that you wanted to use this graph in multiple reports, but in one of these reports you want the graph to have a title and in the other report you do not.</p>
  
  <p>Ideally, I would like to be able to use the same external R script to be able to do this so that I do not have to make changes to multiple R files in case I decide to change something about the graph.</p>
  
  <p>I thought that one way to perhaps do this would be by setting the <code>fig.show</code> chunk option to <code>hold</code>—since it will "<a href="http://yihui.name/knitr/options/#chunk_options" rel="nofollow">hold all plots and output them in the very end of a code chunk</a>"—and then appending a title to the plot like so:</p>
  
  <h3><code>test.Rnw</code></h3>
  
  <pre><code>\documentclass{article}
  
  \begin{document}
  
  &lt;&lt;external-code, cache=FALSE,echo=FALSE&gt;&gt;=
  read_chunk('./test.R')
  @
  
  &lt;&lt;CarPlot,echo=FALSE,fig.show='hold'&gt;&gt;=
  CarPlot &lt;- CarPlot + ggtitle("Plot about cars")
  @
  
  \end{document}
  </code></pre>
  
  <p>This, however, does not work. Although the plot is printed, the title that I tried to append does not show up.</p>
  
  <p>Is there some way to do what I would like to do?</p>