RStudio Rmarkdown chunks


I just can't figure out how to set the evaluation directory of code chunks to the document directory. I set the corresponding option to Knit/Knit Directory/Document Directory (default) but when I use a minimal chunk with


I still get the current working directory when executing the chunk within RStudio (kniting the document works fine and prints the document's directory). I am working with 1.1.375 on Win 10, any ideas?

Did you tried Rstudio new version which a feature that could help you:

Source :

Otherwise, you can change the directory to use as root for evaluation code chunk if you set knitr global option root.dir . By default, it is NULL and document evaluates chunks in current document directory

opts_chunk$set(root.dir = "<dir>")
1 Like

Hey cderv,

yeah, and it works for setting the path during knitting. What I was hoping for was an option having RStudio execute code chunks from an .Rmd in the document directory instead of the current working directory. I guess that's not possible then? This is the behavior that I would expect as the default is to also knit the entire document from the document folder.



I think it should be the case. It is what I meant: I need to set root.dir only if I want to change that. Default is NULL for current document directory.

I use also the here package to help with finding the root directory from any file. And for creating file path in a document within a subdirectory

1 Like

I heartily endorse @cderv's recommendation for the here package and it's more powerful friend rprojroot.

Always work inside "projects" (conceptually and literally: dedicated directory = RStudio Project = Git repo), leave working directory in RStudio set to top-level of Project at all times, and create resilient paths using here::here(). If you or collaborator or some automated process ends up with working directory set elsewhere in the Project, these paths will still work.