File Paths

Paths can be relative to the current working directory

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# View the working directory
getwd()
[1] "/Users/chrisralbon/cra/cra_projects/peripheral_brain/notebooks/rstats"
# Set the working directory to the user's home directory
setwd("~")
  • In relative paths, . denotes the currect directory
  • In relative paths, .. denotes the parent directory
  • In relative paths, ~ denotes your home directory
# Path.expand converts relative paths to absolutely paths
path.expand("~")
[1] "/Users/chrisralbon"
# basename() returns the file name without the path
basename("~")
[1] "chrisralbon"
# dirname returns the path without the file name
dirname("~")
[1] "/Users"