Arthur Leung

here() as an alternative to using working directories in R

I have a Windows desktop and a macOS laptop. I share files between them using OneDrive, provided by my institution. The root directory of OneDrive on my laptop is /Users/art/Library/CloudStorage/OneDrive, but on my desktop it’s C:/OneDrive. Without having to edit the working directory of my R scripts, how can I switch between the two devices while working on the same script? Or if I am to send an R script to a colleague, how do I ensure it will work regardless of where they put the script?

here package in R

This package makes it easy to work with filepaths relative to the script or a pre-defined project folder.

If you run here(), it will return the directory in which your file is located.

here::here()

## [1] "/Users/art/Documents/Project/Scripts"

String arguments within here() will append those strings to that filepath, separated by / if there are multiple strings (whether or not they exist).

here::here("Folder")
## [1] "/Users/art/Documents/Project/Scripts/Folder"

here::here("Folder", "File.txt")
## [1] "/Users/art/Documents/Project/Scripts/Folder/File.txt"

here::here("Folder/File2.txt")
## [1] "/Users/art/Documents/Project/Scripts/Folder/File2.txt"

This will work if I moved this “Scripts” folder (which contains this R Markdown document).

But let’s say we have a file in Project/Data that we want to access. (Without doing anything else, here() only allows us to work within the Scripts folder.) We can use i_am() from the here package to define a project folder; this kind of like a working directory, but it is relative to the file you are working in. It takes a string that is the filepath relative to your desired project folder.

Here are a few examples, first for illustration defining Documents as the project folder, then defining Project as the project folder.

here::i_am("Project/Scripts/here.Rmd")
## here() starts at /Users/art/Documents

here::here("Data")
## [1] "/Users/art/Documents/Data"

here::i_am("Scripts/here.Rmd")
## here() starts at /Users/art/Documents/Project

here::here("Data")
## [1] "/Users/art/Documents/Project/Data"

We can then work within the Data folder. Here is an example where I make a folder in the Project folder and write/read files to that folder


dir.create(here::here("Data")) # make the Data folder
readr::write_csv(iris, here::here("Data", "iris.csv")) # write iris to a csv
head(readr::read_csv(here::here("Data", "iris.csv"))) # look at the csv

## Rows: 150 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Species
## dbl (4): Sepal.Length, Sepal.Width, Petal.Length, Petal.Width
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

## # A tibble: 6 × 5
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
##          <dbl>       <dbl>        <dbl>       <dbl> <chr>  
## 1          5.1         3.5          1.4         0.2 setosa
## 2          4.9         3            1.4         0.2 setosa
## 3          4.7         3.2          1.3         0.2 setosa
## 4          4.6         3.1          1.5         0.2 setosa
## 5          5           3.6          1.4         0.2 setosa
## 6          5.4         3.9          1.7         0.4 setosa

#Code