title: Intelligently cache in R Markdown
body: <p>Knitr provides the option to cache the output of chunks using the <code>cache=TRUE</code> option. This is very useful if a script contains large amount of processing which doesn't need to be updated that often.</p>
  
  <p>However, the cache will only be updated if the code in the chunk is changed, or the cache file is deleted. This is not always ideal, as there may be a slow process which relies on options specified outside of the chunk.</p>
  
  <p>Is it possible to have the cache checks to see the input parameters used within the chunk, and updates if these have been changed?</p>
  
  <h2>Example</h2>
  
  <p>Here is a minimal example:</p>
  
  <pre><code>---
  output: pdf_document
  ---
  
  ```{r}
  x &lt;- "Title 1"
  ```
  
  ```{r cars, cache=TRUE}
  plot(cars, main = x)
  ```
  </code></pre>
  
  <p>When you run this the first time, it will output the figure below. If we change the value of <code>x</code> to something else, such as <code>Title 2</code>, it will not update the title of the plot on the second run.</p>
  
  <p><a href="https://i.stack.imgur.com/MMfaV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MMfaV.png" alt="enter image description here"></a></p>
  
  <p>In my ideal solution, <code>knitr</code> would detect that the value of <code>x</code> changes, and therefore refresh the cache.</p>
  
  <p>From what I can see, nothing is mentioned along these lines in the documentation: <a href="https://yihui.name/knitr/options/#cache" rel="nofollow noreferrer">https://yihui.name/knitr/options/#cache</a></p>