The setwd() function changes where R looks for files and saves output on your computer
When you open R or RStudio, the program starts in a default folder — usually your Documents folder or home directory. If your data files are somewhere else, or you want R to save results to a specific location, you need to change the working directory. The simplest way is the setwd() function, which tells R exactly which folder to use.
Your working directory matters because it determines where R searches when you ask it to open a file by name alone, and where it saves files you create. If you tell R to open "mydata.csv" without a full path, R looks in the current working directory first. Setting the right directory saves you from typing long file paths repeatedly.
Key Takeaways
- Use setwd("/path/to/folder") to change your working directory, replacing the path with your actual folder location.
- On Windows, use forward slashes (/) or double backslashes (\\) in paths, not single backslashes.
- Check your current working directory anytime with getwd(), which prints the full path.
- RStudio's Files pane lets you navigate to a folder and set it as working directory without typing code.
- R projects in RStudio automatically set the working directory to the project folder when you open the project.
Using setwd() with the correct path format
The setwd() function takes one argument: the path to your folder. On macOS and Linux, paths look like /Users/yourname/Documents/myproject. On Windows, the path might be C:/Users/yourname/Documents/myproject or C:\\Users\\yourname\\Documents\\myproject. Notice that Windows paths use forward slashes or double backslashes in R — a single backslash will cause an error because R treats it as an escape character.
To change to your Documents folder on Windows, you would type:
setwd("C:/Users/yourname/Documents")
On macOS, the same command looks like:
setwd("/Users/yourname/Documents")
After you run the command, R returns to the prompt with no message if it succeeds. If the folder does not exist or the path is wrong, R prints an error like "cannot change working directory".
Finding your folder path without typing it manually
If you are not sure of the exact path, you can find it by navigating to the folder in your file manager, then copying the path from the address bar. On Windows 10 and 11, open File Explorer, navigate to your folder, click the address bar, and copy the text. On macOS, open Finder, navigate to the folder, then press Command+Option+C to copy the path.
Paste that path into your setwd() command, but remember to convert single backslashes to forward slashes on Windows. If your folder name contains spaces, the whole path must be in quotes — which it should be anyway.
A faster method in RStudio: use the Files pane on the lower right. Navigate to your folder by clicking through the directory tree, then click the gear icon and select "Set As Working Directory". RStudio writes the setwd() command for you in the console.
Checking your current working directory
At any point, type getwd() and press Enter to see where R is currently working. The output is the full path as a string. This is useful when you are not sure whether a previous setwd() command worked, or when you are troubleshooting why R cannot find a file you thought you saved.
If you change directories multiple times in a session, getwd() always shows the most recent one. It does not show a history of past directories — only the current one.
Using R projects to avoid changing directories repeatedly
RStudio offers a cleaner approach: R projects. When you create a new project, RStudio builds a folder structure and a .Rproj file. Every time you open that project, RStudio automatically sets the working directory to the project folder. You never type setwd() again for that project.
To create a project, go to File > New Project in RStudio, choose "New Directory", pick a project type (usually "New Project"), name it, and choose where to save it. RStudio creates the folder and opens it as your working directory. From then on, opening the .Rproj file sets everything up for you.
Projects also keep your scripts, data, and output organized in one place, which matters when you share work with others or return to a project months later.
Handling relative paths within your working directory
Once your working directory is set, you can use relative paths to refer to files and folders inside it. If your working directory is /Users/yourname/myproject and you have a subfolder called data with a file sales.csv, you can load it with:
read.csv("data/sales.csv")
You do not need the full path. R starts from the working directory and looks for the data folder inside it, then the file inside that. This makes your code portable — if someone else runs your script on their computer after moving the whole project folder, the relative paths still work as long as the folder structure is the same.
To go up one level from your working directory, use ../. For example, read.csv("../other_project/data.csv") looks in a sibling folder called other_project.
Common mistakes and how to fix them
The most frequent error is forgetting to quote the path. setwd(C:/Users/yourname/Documents) without quotes fails because R tries to interpret the path as code. Always wrap the path in quotes: setwd("C:/Users/yourname/Documents").
On Windows, using single backslashes causes errors too. setwd("C:\Users\yourname\Documents") fails because \U and \D are escape sequences R does not recognize. Use forward slashes instead: setwd("C:/Users/yourname/Documents").
If R says the directory does not exist, check that the path is spelled correctly and that the folder actually exists. Typos in folder names are the usual culprit. Use getwd() to confirm where you are, then navigate from there using relative paths if the target folder is nearby.
Frequently Asked Questions
Does setwd() change the directory permanently or just for this R session?
Only for the current session. When you close R or RStudio, the working directory resets to the default. If you want to set it every time you work on a project, either use an R project (which handles it automatically) or put setwd() at the top of your script so it runs each time you open the file.
Can I use ~ to mean my home directory?
Yes. setwd("~/Documents") works on macOS and Linux. On Windows, the tilde does not always work in older R versions, so forward slashes from the C: drive are more reliable. In RStudio on any system, the tilde usually works fine.
What happens if I try to set a working directory that does not exist?
R prints an error: "cannot change working directory". The working directory stays where it was before. Check the path spelling and make sure the folder exists before running setwd().
Can I have multiple working directories open at once?
Not in a single R session. R has one working directory at a time. If you need to work with files in different folders, use relative paths, create subfolders within one project, or open separate R sessions — though RStudio projects are the cleaner approach.