During the uncoast unconf zero day there was a discussion about the here package. The discussion arose during Haley Jeppson’s talk on Project Workflow & Good Practices, in which she showed an example of absolute vs. relative paths (and the relative path example also used
here). It turns out that most of us don’t use the here package.
The here package solves a very specific type of problem. In an old gist by Jenny Bryan, she wrote about working on projects with R scripts and R Markdown files in multiple sub-directories. During development she wanted to work on documents in sub-directories using RStudio (using
compile) but also be able to render/compile them from the project root directory using a Makefile for production. This is the problem the here package solves – it provides a consistent way to declare relative paths from the root directory of the project.
The intention, as far as I can tell, is that you may organize your files in sub-directories (and sub-sub-directories) but have them run as if they were in the root – even when they are executed from within a subdirectory.
What do I mean by all of this? Let’s look at an example. Suppose we have this project:
> tree . ├── sub-dir-1 └── project.Rproj 1 directories, 1 files
This is a very simple project with just a single file,
project.Rproj, and one directory
We can understand what the here package does by invoking the
here function from within the root and
> R --no-init-file -q -e 'here::here()'  "/Users/evalentiner/Desktop/project" > cd sub-dir-1 > R --no-init-file -q -e 'here::here()'  "/Users/evalentiner/Desktop/project"
In both cases
here returns the project root. Contrast this to doing the same thing but using the
> cd .. > R --no-init-file -q -e 'getwd()'  "/Users/evalentiner/Desktop/project" > cd sub-dir-1 > R --no-init-file -q -e 'getwd()'  "/Users/evalentiner/Desktop/project/sub-dir-1"
This time we get two different paths, depending on which directory we are in when we run the command.
Using the here package you can specify all of your paths relative to the root directory and they will always work, even when your actual working directory is a sub-directory. So it makes sense to use the here package when you have scripts in sub-directories.
When does it not make sense to use the here package:
- you have paths that are outside of the project directory