Section 15 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.
15.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 sub-folder ofDocuments
calledMAS113
, 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.
15.1.1 Working directories for R Markdown documents
When we run a code chunk inside an R Markdown document, the working directory is set to be directory containing the document (check this by running the command getwd()
inside a code chunk.) If you are using an RStudio project, you may wish your data and R Markdown documents to be in different folders, e.g., if the project directory is myProject
, you may have folders
myProject/data
myProject/reports
If your R Markdown document is in the folder myProject/reports
, and you want to import a file myData.csv
from myProject/data
, specify the (relative) file path in your R Markdown document as
15.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 sub-folder, within your selected folder. The sub-folder will have your project name. The sub-folder 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.
15.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.
15.4 Using your project folder
In your new project folder, you should keep all files associated with that project. You might create further sub-folders 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 sub-folder for R scripts. These will all be sub-folders of the project working directory, which makes them a little easier to work with.