R Markdown not finding the correct working directory (using here::here())

I have three R Markdown files saved in their own, distinct directories (and that read files saved in their own directories) that I am rendering to HTML.

For all three, I follow the same pattern: I use the here::here() function to provide the paths to the files to be read.

For two of the three R Markdown files, the files read the files correctly and render them. For the one with an issue, it seems to think that the working directory is in the same directory as the R Markdown file itself, even though I use here::set_here() to the same directory that the other two use (and I checked that it worked as such using here::dr_here().

When I move the R Markdown file that causes an issue to the directory I passed to here::set_here(), it works just fine. In short, the file seems to think the working directory is somewhere else, and so the file doesn't render quite correctly.

How can I think about trouble-shooting this? Thank you very much.

One quick question - are you using these in conjunction with a .Rproj file?

Based on your description, I'm having a hard time visualizing the directory structure and how here() is helping out with it. It sounds like it looks like this:

| rootDir/
|---- doc1/
|---- ---- data1.csv
|---- ---- doc1.Rmd
|
|---- doc2/
|---- ---- data2.csv
|---- ---- doc2.Rmd
|
|---- doc3/
|---- ---- data3.csv
|---- ---- doc3.Rmd

Does that reflect how things are set up?

2 Likes

Yes, there is an .Rproj file in the root directory.

Thank you for being willing to try to figure things out (and for asking me for some more clarity!). So, this is basically it. The only difference is that the data are in a sub-directory, but I think that's a minor/trivial issue:

| rootDir/
|---- doc1/
|---- ---- data/
|---- ---- ---- data1.csv
|---- ---- doc1.Rmd
|
|---- doc2/
|---- ---- data/
|---- ---- ---- data2.csv
|---- ---- doc2.Rmd
|
|---- doc3/
|---- ---- data/
|---- ---- ---- data3.csv
|---- ---- doc3.Rmd

My pleasure @jmichaelrosenberg. My initial reaction is that a flatter file system might be better:

| rootDir/
|
|---- rootDir.Rproj
|
|---- data/
|---- ---- data1.csv
|---- ---- data2.csv
|---- ---- data3.csv
|
|---- docs/
|---- ---- doc1.Rmd
|---- ---- doc2.Rmd
|---- ---- doc3.Rmd

This file system is based on some sage wisdom about project organization.

From any of those notebooks, a call like this should work as you want it to in my suggested file system:

df <- readr::read_csv(here("data", "data1.csv"))

I would specify the file paths directly in those calls rather than using here::set_here() - I suspect that you are getting some conflicts between your here::set_here() instructions and where the .Rproj file is. here() looks for that .Rproj file and uses that as its way of identifying which directory is the root.

If you want the more complex file system for whatever reason, a call like this should also work:

df <- readr::read_csv(here("doc1", "data", "data1.csv"))

Another thing that might be complicating things is if you are using knitr functions to try and set the directories your .Rmd files are looking in (something like knitr::opts_knit$set(root.dir = here::here())). As I found out the hard way, here() does not play well with those.

3 Likes