Section 10 RStudio Projects
RStudio Projects can be helpful for organising your files. If you are working on more than one project using R, RStudio Projects make it easier to keep your work for each project separate, and to find the files you need each time you start up R.
10.1 File paths and working directories
We’ll discuss importing data in a later section, but it’s helpful at this point to understand the idea of a working directory in relation to your data files (RStudio Projects make the whole process easier to manage.)
Suppose you have a data file myData.csv
you want to import. How will R find this file? R will have a current working directory, which you can see with the command
If your file myData.csv
is in this folder, you can import it with the command
R will look in the current working directory, and find your file. If your data files is in a different folder, three options are
- Specify the full file path to your data file, e.g. something like
- Change your working directory to the folder containing the data file, using Session > Set Working Directory > Choose Directory… from the RStudio menu bar.
- If possible, specify the file path relative to your current working directory, e.g. if your current working directory is
/Users/Jeremy/Documents
, and the data file is in a subfolder ofDocuments
calledMAS61004
, you could use the command
Option 3 is recommended, as it make things a bit easier if you want to transfer your work between different computers, or share code and data with others. But this all gets messy if you’re working on different projects, and you want to keep your files nicely organised on your computer. As we’ll see, RStudio projects become useful here.
10.2 Creating a new project
You can create different types of projects, but for a ‘basic’ one, go to
File > New Project…> New Directory > New Project > Browse
then choose a folder on your computer in which you wish to locate your project, then choose a project name and click create project.
This will create a subfolder, within your selected folder. The subfolder will have your project name. The subfolder will contain a .Rproj
file, which identifies the project. From a file explorer, you should be able to click on this and launch RStudio with the project open.
You can also use the Project menu of the right hand side of the RStudio window.
10.3 Opening an existing project
You can either go to File > Open Project… and find your .Rproj
file, or you can use the Project menu at the top right.
When you open a project, RStudio will set the working directory to that project folder, and open any files you were working on the last time you worked on that project. The Files tab in RStudio will display your project folder, and there are various tools in this tab for working with your files.
Depending on your settings, RStudio may open your most recent project, if you were working on it the last time you used RStudio. You can close a project using the Project menu.
10.4 Using your project folder
In your new project folder, you should keep all files associated with that project. You might create further subfolders within your project folder. For example, it’s a good idea to have a ‘data’ folder, containing any data sets used in your project. You might have another subfolder for R scripts. These will all be subfolders of the project working directory, which makes them
Exercise 10.1
- If you don’t have one already, create a folder on your computer for this module. (I suggest you name it either MAS61004 or MAS6024).
- Within that folder, create a new RStudio Project with the name
R Tutorial
. In your project folder, create a new folder calleddata
. - Download the tutorial data sets zip file, and put the contents of this zip file in your
data
folder. (Do this in a file explorer, outside of RStudio) - Use the Files tab in RStudio to check that the data files are there.
- In the Console window, check the current working directory that RStudio is using, by running the command
If you’ve completed the exercise correctly you should be able to open your project and see something like the following.