I am writing a Rmd
report with some R
code chunks, obviously. My code structure is like the following:
- A
functions.R
script for custom functions DataDependency.R
script for loading packages and my data, this already sourcesfunctions.R
for exactly these tasks- Some
analysis.R
scripts sourcingDataDependency.R
- Some more
furtheranalyis.R
sourcinganalysis.R
, since then I don't have to write some steps multiple times
Therefore, I am heavily depending on the function to source files in a nested way. However, I am unable to accomplish this in RMarkdown
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.
The other questions I saw regarding the topic only included sourcing of .Rmd
within .Rmd
files (here) and the distinction between source()
and read_chunk()
(here). Both do not answer my question.
I already tried to make sure that it is really the nested sourcing that produces the errors. So here is a minimal working example:
MWE
File mweA.R
x = 1:10
and file mweB.R
source("./mweA.R")
y = x * x
Now, in my .Rmd
file I want to just load file B (or both if I must) and then get on with it:
```{r}
source("./mweB.R")
plot(y ~ x)
```
And even if I do this:
```{r}
source("./mweA.R")
source("./mweB.R")
plot(y ~ x)
```
the same error occurs, namely:
Error in file(filename, "r", encoding = encoding) : cannot open the connection Calls: <Anonymous> ... source -> withVisible -> eval -> source -> file Execution halted
Please note that I don't get an error, if I just do source("./mweA.R")
or source any other non-depending R
script.
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 latex
to RMarkdown
...