Module 3: Working Directories

Learning Objectives

After module 3, you should be able to…

  • Understand your own systems’ file structure and the purpose of the working directory
  • Determine the working directory
  • Change the working directory

File Structure

The internal file structure of the computer is completely nested!

knitr::include_graphics(here::here("images", "presentation4.webp"))

Computer scientists call this the “file tree”.

Working Directory – Basic term

  • R “looks” for files on your computer relative to the “working” directory
  • For example, if you want to load data into R or save a figure, you will need to tell R where to look for or store the file
  • Many people recommend not setting a directory in the scripts, rather assume you’re in the directory the script is in

Understanding the working directory

Understanding the working directory

Understanding the working directory

Understanding the working directory

Getting and setting the working directory using code

## get the working directory
getwd()
setwd("~/") 

Setting a working directory

  • Setting the directory can sometimes (almost always when new to R) be finicky
    • Windows: Default directory structure involves single backslashes (“\”), but R interprets these as”escape” characters. So you must replace the backslash with forward slashes (“/”) or two backslashes (“\\”)
    • Mac/Linux: Default is forward slashes, so you are okay
  • Typical directory structure syntax applies
    • “..” - goes up one level
    • “./” - is the current directory
    • “~” - is your “home” directory

Absolute vs. relative paths

From Wiki

  • An absolute or full path points to the same location in a file system, regardless of the current working directory. To do that, it must include the root directory. Absolute path is specific to your system alone. This means if I try your code, and you use absolute paths, it won’t work unless we have the exact same folder structure where R is looking (bad).

  • By contrast, a relative path starts from some given working directory, avoiding the need to provide the full absolute path.

Relative path

You want to set you code up based on relative paths. This allows sharing of code, and also, allows you to modify your own file structure (above the working directory) without breaking your own code.

Setting the working directory using your cursor

Remember above “Many people recommend not setting a directory in the scripts, rather assume you’re in the directory the script is in.” To do so, go to Session –> Set Working Directory –> To Source File Location

RStudio will show the code in the Console for the action you took with your cursor. This is a good way to learn about your file system how to set a correct working directory!

setwd("~/Dropbox/Git/SISMID-2024")

Setting the Working Directory

If you have not yet saved a “source” file, it will set working directory to the default location.Find the Tool Menu in the Menu Bar -> Global Opsions -> General for default location.

To change the working directory to another location, find Session Menu in the Menu Bar –> Set Working Directory –> Choose Directory`

Again, RStudio will show the code in the Console for the action you took with your cursor.

Summary

  • R “looks” for files on your computer relative to the “working” directory
  • Absolute path points to the same location in a file system - it is specific to your system and your system alone
  • Relative path points is based on the current working directory
  • Two functions, setwd() and getwd() are useful for identifying and manipulating the working directory.

Acknowledgements

These are the materials we looked through, modified, or extracted to complete this module’s lecture.