Day 04: here::here()

Begin your journey to project-oriented-workflow enlightenment by using here::here() to build file paths.
Published

December 4, 2022

The titular function of the here package (Müller 2020), here::here(), hasn’t exactly been slept on. Among other things, it’s garnered an excellent illustration by Allison Horst (below), and an ode by Jenny Bryan.

Why all the fuss?

here() is one of the keys to a collaborative, project-oriented workflow. Rather than using setwd() (which usually involves an absolute path to a location on a single user’s machine), you can use here() to build file paths that are relative to the project root.

For example, when reading in data for a project, my script usually looks something like the following:

read_csv(here::here("data", "my_data.csv"))

Since I do almost all of my work in projects with .Rproj files, this means that the data I’m reading is in a subfolder of my project called “data”, and the file is “my_data.csv”.

If you’re not an .Rproj user, here() has a hierarchy of heuristics it uses to determine the project root, including checking for the presence of a file named .here. For more detail, Jenny Bryan’s ode contains an excellent section on the fine print.

References

Müller, Kirill. 2020. here: A Simpler Way to Find Your Files. https://here.r-lib.org.