Obviously, knitr's root.dir option (set via rmarkdown::render(knit_root_dir = getwd())) is ignored for resolving relative child document paths and only respected for code evaluation inside code chunks.
Now I wonder: Why?
And more importantly: How can I change the root directory for resolving relative child document paths? I couldn't find anything about this so far...
(The warning saying The chunk 'unnamed-chunk-1' has the 'child' option and this code chunk must be empty only occurs inside the reprex, and I have no clue why.)
I've never heard this issue reported before. I'm not totally sure if it's a bug or feature. To me, root.dir defines the working directory for the code inside code chunks. For package options such as child, I'm not sure if that working directory also applies. Anyway, I just changed the behavior via https://github.com/yihui/knitr/pull/2059 and you may try the dev version of knitr now:
Thank you very much! I can confirm the child option now respects the root.dir option.
Another question that came up when skimming the linked PR: Are there any (remaining) differences between setting opts_knit$set(root.dir = getwd()) and opts_knit$set(knitr.use.cwd = TRUE)?
They have the same effect when root.dir = getwd(). It's just that the former has precedence over the latter, and the latter is probably easier to set. The former option can be used to specify any directory, not limited to getwd(), and the latter is a boolean option.
BTW, it's options(knitr.use.cwd) instead of opts_knit$set(knitr.use.cwd).
BTW, it's options(knitr.use.cwd) instead of opts_knit$set(knitr.use.cwd) .
Oh yeah, of course. That's what I meant, sorry.
They have the same effect when root.dir = getwd() . It's just that the former has precedence over the latter, and the latter is probably easier to set. The former option can be used to specify any directory, not limited to getwd() , and the latter is a boolean option.
Ok, understood. So you might wanna consider deprecating the knitr.use.cwd option to simplify the whole knitr API (to remain backwards compatible, you could internally transform knitr.use.cwd = TRUE into opts_knit$set(root.dir = getwd()) I guess).