The setwd() function changes where R looks for files and saves output
When you open R, it starts in a default directory — usually your Documents folder or home directory depending on your system. To work with files in a different location, you use the setwd() function to tell R where to look. This is one of the first things you do in any R session if your data or scripts live somewhere other than the default spot.
The command is straightforward: setwd("/path/to/your/folder"). On Windows, use forward slashes or double backslashes in the path. On Mac or Linux, use forward slashes. After you run setwd(), R will read and write files from that location until you change it again or close R.
Key Takeaways
- Use setwd() with the full path to your folder to change where R reads and writes files in your current session.
- On Windows, write paths with forward slashes (/) or double backslashes (\\), not single backslashes.
- Check your current working directory anytime with getwd(), which prints the path R is using right now.
- The working directory resets to the default when you close and reopen R unless you save it in a project file.
- RStudio projects automatically manage working directories, so you do not have to run setwd() every session.
Finding and checking your current working directory
Before you change directories, it helps to know where you are. Type getwd() into the console and press Enter. R will print the full path of the folder it is currently using. This is useful when you are not sure whether a file you are trying to load is in the right place.
On Windows, the path might look like C:/Users/YourName/Documents. On Mac, it might be /Users/YourName/Documents. On Linux, it could be /home/YourName/Documents. The exact path depends on your system and how your folders are organized.
How to write the setwd() command correctly
The most common mistake is using backslashes on Windows. If you copy a path from Windows File Explorer, it will have single backslashes like C:\Users\YourName\Documents. R does not understand single backslashes in file paths because it treats them as escape characters. You must convert them.
Here are the three ways that work on Windows:
- Forward slashes: setwd("C:/Users/YourName/Documents")
- Double backslashes: setwd("C:\\Users\\YourName\\Documents")
- Use the file.choose() function: setwd(file.choose()) opens a dialog box where you click the folder you want, and R writes the path for you.
On Mac and Linux, always use forward slashes: setwd("/Users/YourName/Documents") or setwd("/home/YourName/Documents").
Using RStudio projects to avoid resetting the working directory
If you use RStudio (the graphical interface for R), you can create a project file that remembers your working directory. When you open the project, RStudio automatically sets the working directory to the project folder without you typing anything.
To create a project, go to File > New Project in RStudio. Choose where you want the project folder to live, and RStudio will create a .Rproj file there. From then on, open that .Rproj file instead of opening R directly, and your working directory will be set correctly every time.
This is much faster than typing setwd() at the start of every session, especially if you work on multiple projects in different folders.
What happens when you change the working directory mid-session
You can run setwd() as many times as you want in a single R session. Each time you run it, R switches to the new folder. Any files you load or save after that point will use the new location.
This is useful if you need to read data from one folder, process it, and save results to another. Just run setwd() again before you save. However, if you close R and reopen it, the working directory resets to the default unless you are using an RStudio project.
Listing files in your working directory
Once you have set your working directory, you can see what files are in that folder without opening File Explorer or Finder. Type list.files() or dir() into the console. R will print the names of all files and folders in your current working directory.
This is helpful when you are not sure whether a file you want to load is actually in the folder you think it is. If you do not see the file listed, you are in the wrong directory or the file is in a subfolder.
Frequently Asked Questions
What if I get an error saying the file does not exist?
First, run getwd() to confirm you are in the right folder. Then run list.files() to see what is actually there. If the file is not listed, either you are in the wrong directory or the filename is spelled differently than you thought. Check the exact spelling and file extension.
Can I use a relative path instead of the full path?
Yes. If you want to move to a subfolder inside your current directory, you can use setwd("subfolder_name"). You can also go up one level with setwd(".."). This is useful in scripts that you share with others, because relative paths work regardless of where the main folder is located on someone else's computer.
Do I have to set the working directory every time I open R?
Only if you are not using RStudio projects. If you use projects, opening the .Rproj file sets the directory automatically. If you use R directly, the directory resets each time you close and reopen it, so you will need to run setwd() again or add it to the top of your script.
What is the difference between setwd() and getwd()?
setwd() changes the working directory to a new location. getwd() shows you where you currently are without changing anything. Use getwd() to check, and setwd() to move.