3.3 Set a Working Directory in RStudio
Preferred Method: Creating a Project
Let’s say you’re working on an experiment and you’d like to write a reproducible lab report using Markdown in RStudio. The first step is to create a new R project–which creates a file ending in .Rproj
. A .Rproj
file is a file that sits in the root directory of your project. When you open your RStudio session via this project file, it automatically tells R
to use that root folder as your working directory. In other words, it tells R to look to this root folder for any files or folders you refer to within your R Markdown script.
The biggest benefit to this approach for setting a working directory is that if you decide–or need–to move your project’s root folder to another location on your computer or transfer your project’s root folder to a different computer, all of the file paths specified in your R
script will still be valid and work, because they will all be relative to your project’s root folder.
There are two options to create a new R project:
- Create a new project file within a brand new folder.
- Create a new project file within an existing folder.
So if you have a bunch of files related to your experiment and these are already saved in a folder on your computer, it makes more sense to create a new project within that existing folder. Alternatively, if you are starting from scratch you might prefer to create a new project within a brand new folder.
Let’s go through an example of how to create a new project with each method.
Create a New Project in a New Folder
The first step is to open RStudio and select ‘New Project’ from the ‘File’ drop down menu.
You will then be asked to specify if you’d like to create a ‘New Directory’ or use an ‘Existing Directory’. In this case select ‘New Directory’.
You will then be prompted to choose a project type. Choose ‘New Project’.
Now name what will be your working directory/root folder according to appropriate naming conventions. Click ‘Browse’ and select a location to save this folder to on your computer.
Then click ‘Create Project’.
Finally, R
will bring you back to the main screen and under the ‘Files’ region (bottom right panel) you will see your new project file. Now R
is automatically using your new folder containing the project file as the working directory. As you create new files associated with your experiment, make sure you save them within the root folder that contains this project file.
Create a New Project in an Existing Folder
Recall, that the first step is to open RStudio and select ‘New Project’ from the ‘File’ drop down menu.
You will then be asked to specify if you’d like to create a ‘New Directory’ or use an ‘Existing Directory’. In this case select ‘Existing Directory’.
Select the folder from your computer that you’d like to use as your working directory for the project.
Then click ‘Create Project’.
Finally, R
will bring you back to the main screen and under the ‘Files’ region (bottom right panel) you will see your new project file. Now R
is automatically using your new folder containing the project file as the working directory. Remember, as you create new files associated with your experiment, make sure you save them within the root folder that contains this project file.
If you close RStudio and want to re-open the project you’ll need to tell R that you want to use the root folder with the project file in it as your working directory. To do this, you’ll need to locate the project file on your computer and open it, or open RStudio and select ‘Open Project’ under the ‘File’ drop down menu.
You may have heard of other methods that can be used to set a working directory in R
. In fact, there’s a built in command–setwd()
–to set a working directory other than the one currently being used. If you chose this method over using an R Project file, you would need to set your working directory every time you wanted to work on your project, or, alternatively, have the first line of code in your script set your working directory using an absolute path. The downside of using this method is that it relies on extra steps or absolute paths to access your working directory. Consequently, if you move the project’s root folder–containing all the files related to your experiment–you’ll have to edit your R
script and change the absolute path. This impacts computational reproducibility. If you send your work to a lab partner or colleague they will have to edit the script, updating the absolute path to match where they’ve placed the project folder on their computer. An R project negates this problem by leveraging relative paths.