Search code examples
rdirectoryr-markdown

Working directory of rmarkdown chunk is not where the markdown lives


The rmarkdown directory is where the .Rmd file lives. So the expected output of getwd() inside a markdown chunk or in a knitted markdown file should be the file path of the markdown file.

As you can see on the attached image the output of getwd() inside a markdown chunk is not as expected. The markdown lies in C:/temp, so the output of getwd() in the chunk and also in the knitted markdown file should be C:/temp. But unexpectedly the output is my default wd of R which is C:/DefaultWD. What I am missing? What I am doing wrong?

enter image description here


Solution

  • If your R Markdown document isn't looking within the folder where that document is stored, check to make sure your settings in RStudio are set to this.

    In RStudio go to Tools > Global Options > R Markdown and the look at the setting for "Evaluate chunks in directory". This is usually "Document" but has two other options. If it is something else, change to "Document" to fix your problem.

    Here's an unfortunately ugly screenshot to show the options: enter image description here

    I read about this in section 16.6 in the R Markdown Cookbook, starting about a quarter of the way down the page.