# ========================================# R Working Directories Comprehensive Companion Script# ========================================# Think of your working directory as R's "current folder".# The working directory is the default location where R will look for files you want to load# and where it will save files you create.# ----------------------------------------# Setting and Checking Working Directory# ----------------------------------------# Check current working directorygetwd()# Set the working directorysetwd("C:/MES503")# Adjust this path as needed# List files in current working directorylist.files()# Multiple Choice Q1: What function would you use to change your current working directory?# a) change.wd()# b) set.wd()# c) setwd()# d) changedir()# ----------------------------------------# Understanding File Paths# ----------------------------------------# Two types of file paths:# 1. Absolute paths (start from the root directory)# Windows: C:\Users\YourUsername\Documents\file.txt# Mac/Linux: /Users/YourUsername/Documents/file.txt# 2. Relative paths (start from the current working directory)# data/file.txt (a subdirectory of the current directory)# ../file.txt (one directory up from the current directory)# Multiple Choice Q2: Which of the following is an example of a relative path?# a) C:\Users\YourUsername\Documents\file.txt# b) /Users/YourUsername/Documents/file.txt# c) data/file.txt# d) None of the above# ----------------------------------------# Best Practices# ----------------------------------------# 1. Use relative paths for code portability# 2. Organize projects with a clear structure# 3. Run code from the project root directory# 4. Understand path notation:# '.' represents the current directory# '..' represents the parent directory# 5. Choose based on context: absolute paths for precise locations, relative paths for flexibility# ----------------------------------------# Loading Data# ----------------------------------------# Load a file from the working directorydata<-read.csv("data.csv")# Load from a subfolder within the working directorydata<-read.csv("subfolder/data.csv")# Load from a folder one level up from the working directorydata<-read.csv("../data.csv")# Multiple Choice Q4: If your working directory is 'C:/Projects/Analysis' and you want to load a file located at 'C:/Projects/Data/survey.csv', which of the following would you use?# a) read.csv("survey.csv")# b) read.csv("Data/survey.csv")# c) read.csv("../Data/survey.csv")# d) read.csv("C:/Projects/Data/survey.csv")# ----------------------------------------# Saving Data# ----------------------------------------# Save a data frame as a CSV file in the working directorywrite.csv(data, "saved_data.csv")# Multiple Choice Q5: After running the code 'write.csv(data, "output/results.csv")', where will the file be saved?# a) In the root directory of your computer# b) In your home directory# c) In a folder named 'output' within your current working directory# d) In your downloads folder# ----------------------------------------# Practice with File Paths# ----------------------------------------# This code creates a project structure in a temporary directorytemp_dir<-tempdir()base_path<-file.path(temp_dir, "my_r_project")dir.create(base_path, recursive =TRUE, showWarnings =FALSE)dir.create(file.path(base_path, "data"), showWarnings =FALSE)dir.create(file.path(base_path, "output"), showWarnings =FALSE)file.create(file.path(base_path, "analysis.R"))file.create(file.path(base_path, "data", "survey_responses.csv"))file.create(file.path(base_path, "data", "demographics.csv"))file.create(file.path(base_path, "output", "results.csv"))# Function to display directory structurelist_files<-function(path, indent=""){files<-list.files(path, full.names =TRUE)for(fileinfiles){cat(indent, basename(file), "\n", sep ="")if(dir.exists(file)){list_files(file, paste0(indent, " "))}}}# Display the created structurecat("\nProject structure created in temp directory:\n")list_files(base_path)# Multiple Choice Q6: Based on the directory structure created above, which of the following# would be the correct relative path to access 'demographics.csv' from the project root?# a) "demographics.csv"# b) "data/demographics.csv"# c) "../demographics.csv"# d) "my_r_project/data/demographics.csv"# ----------------------------------------# Food for Thought# ----------------------------------------# 1. How might using consistent directory structures across different projects benefit your workflow?# ----------------------------------------# Challenges# ----------------------------------------# 1. Create a new directory called 'project' in your current working directory. Inside 'project',# create subdirectories for 'data', 'scripts', and 'output'.# 2. Create a small data frame and save it as a CSV file in the 'data' subdirectory you just created.
Tip
Think of your working directory as R’s “current folder”.
The working directory is the default location where R will look for files you want to load and where it will save files you create.
Setting and Checking Working Directory
You can set the working directory using the setwd() function:
To quickly see your current working directory and the files it contains, navigate to the Files tab in RStudio. You can check that this is your working directory by clicking More - Go to Working Directory .
Understanding File Paths
File paths are used to locate files on your computer. There are two types:
Absolute paths start from the root directory:
Windows: C:\Users\YourUsername\Documents\file.txt
Mac/Linux: /Users/YourUsername/Documents/file.txt
Relative paths start from the current working directory:
data/file.txt (a subdirectory of the current directory)
../file.txt (one directory up from the current directory)
Best Practices
Use relative paths for code portability: This makes your code more flexible and easier to move between different environments or share with collaborators.
Organize projects with a clear structure: Use subdirectories for code, data, and output, making it easier to use relative paths consistently.
Run code from the project root directory: This allows you to use relative paths for both code and data files, enhancing portability.
Understand path notation:
. represents the current directory
.. represents the parent directory
Choose based on context: Use absolute paths when you need to specify a precise location regardless of the current directory, and use relative paths for more flexible, portable references within a project.
By understanding and appropriately using both absolute and relative paths, you can create more organized, portable, and efficient code and file management systems.
Loading Data
Once you’ve set your working directory, you can load files using relative paths:
Understanding working directories and file paths will help you organize your R projects and efficiently manage your data files.
File path practice No. 1
Still having trouble? Unfold the script below to run the script to practice working with directories.
show R code
#This creates a project structure in a temporary directory and displays the directory structure. Temporary directories are locations in the file system meant for short-term storage. They're ideal for practice exercises, unit tests, or any scenario where you need to create files that you don't intend to keep long-term. R provides built-in functions to work with temp directories, making them perfect for this exercise. The OS typically clears temp directories periodically, so you don't have to worry about cleanup.# Get the system's temp directory and create a subdirectory for our projecttemp_dir<-tempdir()base_path<-file.path(temp_dir, "my_r_project")# Create the main project directorydir.create(base_path, recursive =TRUE, showWarnings =FALSE)# get_home_dir <- getwd()# setwd(base_path)# Create subdirectoriesdir.create(file.path(base_path, "data"), showWarnings =FALSE)dir.create(file.path(base_path, "output"), showWarnings =FALSE)# Create empty filesfile.create(file.path(base_path, "analysis.R"))file.create(file.path(base_path, "data", "survey_responses.csv"))file.create(file.path(base_path, "data", "demographics.csv"))file.create(file.path(base_path, "output", "results.csv"))# Function to display directory structurelist_files<-function(path, indent=""){files<-list.files(path, full.names =TRUE)for(fileinfiles){cat(indent, basename(file), "\n", sep ="")if(dir.exists(file)){list_files(file, paste0(indent, " "))}}}# Display the created structurecat("\nProject structure created in temp directory:\n")list_files(base_path)# Display the full path to the projectpaste("Full path to the project: ", base_path)# Note: This structure is created in a temporary directory and will be automatically cleaned up by your operating system eventually. If you want to keep these files, you should copy them to a permanent location.# To explore the files you just made, go to the Files tab in RStudio and navigate to More (the gear icon) - > Go to Working Directory. This will open the directory in the Files pane. You can see the relation of files with respect to each other. Note the absolute path will be different from the structure given above. # should uncomment and run this script if want to return to home_dir. # setwd(get_home_dir)