title: How to make code chunks depend on all previous chunks in knitr/rmarkdown?
body: <h1>Goal</h1>
  
  <p>I want to create my data analysis reproducible by making chunks depend on all previous chunks. So, if there are 3 chunks and I change something in the 1st chunk the subsequent 2 chunks should re-run so that they reflect the change made in the outputs. I want to add this condition in the global chunk options at the top of the document so that I don't have to use <code>dependson</code> multiple times.  </p>
  
  <h1>Problems</h1>
  
  <p>The outputs of a chunk don't change if it is not modified and <code>cache=TRUE</code>. For the chunks containing the code, I can make them dependable on all previous ones using following at the top of the document:    </p>
  
  <pre><code>```{r setup, echo=FALSE}
  # set global chunk options: 
  library(knitr)
  opts_chunk$set(cache=TRUE, autodep = TRUE)
  dep_auto()
  ```
  </code></pre>
  
  <p>If any of the above chunks is changed, all subsequent chunks are re-run. But this does not work if I use <code>source()</code> in chunks to read R scripts. Following is an example document:  </p>
  
  <pre><code>---
  title: "Untitled"
  output: html_document
  ---
  ```{r setup, echo=FALSE}
  # set global chunk options: 
  library(knitr)
  opts_chunk$set(cache=TRUE, autodep = TRUE)
  dep_auto()
  ```
  
  
  # Create Data
  ```{r}
  #source("data1.R")
  x &lt;- data.frame(col1 = 4:10, col2 = 6:12)
  x
  ```
  
  # Summaries
  ```{r}
  #source("data2.R")
  
  median1.of.x &lt;- sapply(x, function(x) median(x)-1)
  
  sd.of.x &lt;- sapply(x, sd)
  
  plus.of.x &lt;- sapply(x, function(x) mean(x)+1)
  
  jj &lt;- rbind(plus.of.x, sd.of.x, median1.of.x)
  
  ```
  
  ```{r}
  jj
  ```
  </code></pre>
  
  <p>Now, if I change any of the 1st 2 chunks the third chunk gives correct output after <code>knit</code>ting. But if instead I put the first chunk's code in a source file <code>data1.R</code> and second chunk's in file <code>data2.R</code>, keeping the global chunk options same as before, if I make any changes in source files they are not reflected in the output of third chunk correctly. For example, changing <code>x</code> to <code>x &lt;- data.frame(col1 = 5:11, col2 = 6:12)</code> should yield:  </p>
  
  <pre><code> &gt; jj
                   col1      col2
  plus.of.x    9.000000 10.000000
  sd.of.x      2.160247  2.160247
  median1.of.x 8.000000  9.000000 
  </code></pre>
  
  <p>But with use of <code>source()</code> as discussed above, the <code>knitr</code> document reports: </p>
  
  <pre><code> jj
  ##                col1      col2
  ## mean.of.x  5.000000  9.000000
  ## sd.of.x    2.160247  2.160247
  ## minus.of.x 6.000000 10.000000 
  </code></pre>
  
  <p>What settings do I need to change to use <code>source</code> in <code>knitr</code> docs correctly?</p>