The goal of the here package is to enable easy file referencing. In
contrast to using setwd()
, which is fragile and dependent on the way
you organize your files, here uses the top-level directory of a project
to easily build paths to files.
Install the released version of here from CRAN:
install.packages("here")
Or install the development version from GitHub with:
devtools::install_github("r-lib/here")
The here package creates paths relative to the top-level directory. The
package displays the top-level of the current project on load or any
time you call here()
:
library(here)
#> here() starts at /Users/sharla/github/tidy-dev-day-2020/here
here()
#> [1] "/Users/sharla/github/tidy-dev-day-2020/here"
You can build a path relative to the top-level directory in order to read or write a file:
here("files", "data", "iris.csv")
write.csv(iris, here("files", "data", "iris.csv"))
These relative paths work regardless of where the associated source file lives inside your project, like analysis projects with data and reports in different subdirectories.
Illustration by Allison Horst