R Tips: Here package

Download the Rmd file

Introduction

If someone were to ask me: “Quick - give me a useful R package. Go!” I would respond with the here package.

The problem

I’ve been in the position where I am running R code on both Windows and Linux or even reusing a script in a different project. I was in the bad habit of using absolute paths and this made transitioning from Linux to Windows a huge pain. I was on the lookout for a solution that would make switching operating systems seamless and generalizing scripts easy.

I first learned of the here package when I had the opportunity to attend RStudio::conf2018 and take Dr. Jenny Bryan’s “What They Forgot to Teach You About R” workshop. This workshop was mostly about using Git, but we spent a fair amount of time discussing the best practices of project workflow and organization.

One aspect of project workflow best practices is safe paths. Safe paths include:

  • Relative to a stable base
  • File system functions (not paste)

As someone who routinely used paste in my file paths, I was intrigued to hear more. The here package addresses both of these points.

Why is relative important?

An absolute path can be problematic if a project is being worked on in different spaces or different operating systems. For example, in Linux or Mac the path to the “home” directory might be: /home/username and on Windows it may be: C:\Users\username.

If multiple people are contributing to the same GitHub repo or running the same code from different places, the absolute path to my Linux home directory will only work for me on that machine. But if I used the relative path for “home directory”, ~, it would work!

Why is stable base important?

However, you can’t assume everyone will run the code from the same place. This is why a stable base is important and can be solved by using RStudio projects and here!

A stable base could be relative path to the home directory, ~, or even better, relative to a given project. Ideally, one divides up different projects into their own files. In this case, the stable base would be the top level directory of the project.

How does here meet these criteria?

The here package builds your path for you so you don’t even have to think about what operating system you are running the code on. It also automatically sets the path root to the top-level directory of your project which is relative to a stable path (top-level project directory).

After I started using here my life got a lot easier!

Package Background

The here package was written by Kirill Müller and is available on CRAN.

Examples

Install

Install here from CRAN:

install.packages("here")

Usage

When you load the here package it returns in a message, the base directory:

library(here)
## here() starts at C:/Users/danag/Documents/cgrb/blog/cgrb-analyst-blog

The base directory is chosen by:

  • Looking for a .here file
  • If one doesn’t exist, it will look for an RStudio project file *.Rproj
  • There are several other ways but they don’t usually apply to a standard project.

As you can see the default is the R project top-level directory unless there is a .here file otherwise stated. This takes care of the relative to a stable base criteria!

The actual usage of here is easy. Say you wanted to open a file with the following path: C:\Users\danag\Documents\cgrb\projects\cgrb-analyst-blog\content\_index.md

The here way of accessing that file:

here("content","_index.md")
## [1] "C:/Users/danag/Documents/cgrb/blog/cgrb-analyst-blog/content/_index.md"

One can also easily use variables in paths:

con <- "content"
here(con, "_index.md")
## [1] "C:/Users/danag/Documents/cgrb/blog/cgrb-analyst-blog/content/_index.md"

If you want to set your base path to something other than the default, one may use:

set_here(path = "path/to/directory")

I hope this package helps to make your life easier like it did mine!

R-session Information

capture.output(sessionInfo())
##  [1] "R version 3.6.1 (2019-07-05)"                                             
##  [2] "Platform: x86_64-w64-mingw32/x64 (64-bit)"                                
##  [3] "Running under: Windows 10 x64 (build 18362)"                              
##  [4] ""                                                                         
##  [5] "Matrix products: default"                                                 
##  [6] ""                                                                         
##  [7] "locale:"                                                                  
##  [8] "[1] LC_COLLATE=English_United States.1252 "                               
##  [9] "[2] LC_CTYPE=English_United States.1252   "                               
## [10] "[3] LC_MONETARY=English_United States.1252"                               
## [11] "[4] LC_NUMERIC=C                          "                               
## [12] "[5] LC_TIME=English_United States.1252    "                               
## [13] ""                                                                         
## [14] "attached base packages:"                                                  
## [15] "[1] stats     graphics  grDevices utils     datasets  methods   base     "
## [16] ""                                                                         
## [17] "other attached packages:"                                                 
## [18] "[1] here_0.1"                                                             
## [19] ""                                                                         
## [20] "loaded via a namespace (and not attached):"                               
## [21] " [1] Rcpp_1.0.1      bookdown_0.13   rprojroot_1.3-2 digest_0.6.21  "     
## [22] " [5] mime_0.7        backports_1.1.4 magrittr_1.5    evaluate_0.14  "     
## [23] " [9] blogdown_0.15   stringi_1.4.3   rmarkdown_1.15  tools_3.6.1    "     
## [24] "[13] stringr_1.4.0   xfun_0.11.1     yaml_2.2.0      compiler_3.6.1 "     
## [25] "[17] base64enc_0.1-3 htmltools_0.3.6 knitr_1.23     "