title: Source nested R files within Rmarkdown document
body: <p>I am writing a <code>Rmd</code> report with some <code>R</code> code chunks, <em>obviously</em>. My code structure is like the following: </p>
  
  <ul>
  <li>A <code>functions.R</code> script for custom functions</li>
  <li><code>DataDependency.R</code> script for loading packages and my data, this already sources <code>functions.R</code> for exactly these tasks</li>
  <li>Some <code>analysis.R</code> scripts sourcing <code>DataDependency.R</code></li>
  <li>Some more <code>furtheranalyis.R</code> sourcing <code>analysis.R</code>, since then I don't have to write some steps multiple times</li>
  </ul>
  
  <p>Therefore, I am heavily depending on the function to source files in a <em>nested</em> way. However, I am unable to accomplish this in <code>RMarkdown</code> which gives me errors every single time (see below). Am I too stupid or is this functionality missing?! All tries so far resulted in error.</p>
  
  <p>The other questions I saw regarding the topic only included sourcing of <code>.Rmd</code> within <code>.Rmd</code> files (<a href="https://stackoverflow.com/questions/10966109/how-to-source-r-markdown-file-like-sourcemyfile-r">here</a>) and the distinction between <code>source()</code> and <code>read_chunk()</code> (<a href="https://stackoverflow.com/questions/15501622/making-knitr-run-a-r-script-do-i-use-read-chunk-or-source">here</a>). Both do not answer my question.</p>
  
  <p>I already tried to make sure that it is really the <em>nested sourcing</em> that produces the errors. So here is a minimal working example:</p>
  
  <h3>MWE</h3>
  
  <p>File <code>mweA.R</code></p>
  
  <pre><code>x = 1:10
  </code></pre>
  
  <p>and file <code>mweB.R</code></p>
  
  <pre><code>source("./mweA.R")
  y = x * x
  </code></pre>
  
  <p>Now, in my <code>.Rmd</code> file I want to just load file B (or both if I must) and then get on with it:</p>
  
  <pre><code>```{r}
  source("./mweB.R")
  plot(y ~ x)
  ```
  </code></pre>
  
  <p>And even if I do this:</p>
  
  <pre><code>```{r}
  source("./mweA.R")
  source("./mweB.R")
  plot(y ~ x)
  ```
  </code></pre>
  
  <p>the same error occurs, namely:</p>
  
  <pre><code>Error in file(filename, "r", encoding = encoding) : cannot open the connection Calls: &lt;Anonymous&gt; ... source -&gt; withVisible -&gt; eval -&gt; source -&gt; file Execution halted
  </code></pre>
  
  <p>Please note that I don't get an error, if I just do <code>source("./mweA.R")</code> or source any other non-depending <code>R</code> script.</p>
  
  <p>Hopefully, there is a (more or less) secret parameter that you have to specify in the chunk which solves all this. I really have a hard time with Rmarkdown's code chunks and it is often not clear to me, what the error is. This mainly keeps me from switching from <code>latex</code> to <code>RMarkdown</code>...</p>