Part 37 The working directory and RStudio Projects

When you open R, it “runs” in some folder on your computer. This is the place it will look for files to import and write files as output. Think about where your class files end up when you knit them.

If you have R/RStudio closed, and you open a .R or .Rmd file, R/RStudio will start in the folder holding that file.

If you open R/RStudio from the Windows Start menu, the Mac dock, the Mac Spotlight, etc., R/Studio will start in its default location (probably your user home directory, see Tools → Global Options → General → Default working directory…).

Write all of your R scripts assuming:

  1. You are running them by opening a fresh new R session

    • Don’t use rm(list = ls()) to clean the workspace–the workspace is already clean
    • You need to load required pacakges with library()
    • Don’t work on several different projects in one R session at the same time!
  2. You have all of the necessary packages installed

    • Don’t include install.packages() calls unless they are commented out or otherwise set not to run
  3. The script will run without human input

    • You need to import or load any data you are working with
    • Load data and write output using R commands, not file.choose(), read.clipboard(), the buttons in RStudio, etc.
  4. All of the needed files live in your project folder

    • Write relative paths, rather than absolute paths
    • file.path("data", "cats-data_2020-03-04.xlsx")
    • Not: /Users/brenton/Research/cats_project/data/cats-data_2020-03-04.xlsx or C:\\Users\\brenton\\Research\\cats_project\\data\\cats-data_2020-03-04.xlsx
      orfile.path("C:", "Users", "brenton", "Research", "cats_project", "data", "cats-data_2020-03-04.xlsx")
  5. Your script might run on any system

    • Write the paths to files using file.path() or here::here(), rather than typing out the whole path

    • This is because Windows and Mac/Linux have different syntaxes for file paths:

      • Mac/Linux: path/to/folder
      • Windows: path\\to\\folder

This approach has several advantages:

  1. Frictionless running on different computers
  2. Less breakage (e.g., if you move a folder around, the relative paths will still work)
  3. Less surprising or weird behaviors due to session crud
  4. Easy to tweak data/code and update results