How to set working directory in RMarkdown child document?

Hi there,

I have a large report which I preparing using RMarkdown (specifically using {bookdown} package but it is probably irrelevant to my question).
As it is large report, I'm using child Rmd documents to make it more manageable. I put my child Rmd's in subfolder to make it more organized (and to prevent {bookdown} to use them as chapters of the report). In the first chunk of the child Rmd I'm setting a root.dir knitr::opts_knit$set(root.dir = dirname(dirname(getwd()))) which is main directory of the project 2 steps above the subfolder with child Rmd. It works fine when I knit or build a report, but I can't figure out how to set a working directory to play with the code in the "notebook" format (i.e. executing chunks one-by-one, editing, re-executing, etc.).
knitr::opts_knit$set(root.dir = dirname(dirname(getwd())))does not affect working directory in such case while setwd() works only for a chunk where it was called and reset for the all other chunks to the default (i.e. document directory):

The working directory was changed to XXXX inside a notebook chunk. The working directory will be reset when the chunk is finished running. Use the knitr root.dir option in the setup chunk to change the working directory for notebook chunks.

setwd() from the console does not work at all.

Any ideas how I can make it work without adding setwd() in each chunk?

You could be interesting by this chapter in the R Markdown Cookbook

I would also suggest to look at the here package if you really need to tweak paths

You should only modify working directory when needed. Especially with child document it could become tricky.

We may need to improve the handling also - if you come up with a reproducible example we could look into it.

Thank you for suggestions.
As a workaround I've glued "here()" to the all file paths, thus they are working in any instance. But it is obviously not so convenient.

I've deposited a simple reprex on github: https://github.com/Dobrokhotov1989/Reprex

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.