Skip to main content

Section A.6 Getting organised: R Projects

One thing that can help you tremendously is keeping your code organised. RStudio helps with this by virtue of something called R Projects.
Technically, a RStudio project is just a directory with the name of the project, and a few files and folders created by RStudio for internal purposes. This is where you should hold your scripts, your data, and reports. You can manage this folder with your own operating system manager (discussed earlier, e.g., Windows) or through the RStudio file manager (that you access in the bottom-right corner set of windows in RStudio).
When a project is reopened, RStudio opens every file and data view that was open when the project was closed last time around. Trust me, this is a really helpful thing! If you create a project for this module, you can keep everything in one place, and refer back to your old code and your learnings throughout the module.

Subsection A.6.1 Saving your work and projects

First things first, hopefully you have already created a separate folder on your computer. Now save the script you’ve been working on into this folder. By clicking on "File" and "Save as...".
Then navigate to your folder for this module, and for your script make sure to give it some meaningful name like appendixlab.R or something like this. Then click ’save’.
Now, go back to "File" and select "New project..."
Then in the options that appear, choose "Existing Directory". This is because you already have a folder for this work; this is where you saved your script just before. For me this was my folder called ’crime_mapping’.
So select ’Existing Directory’, and on the next page use the "Browse" button to select this folder (directory) where you saved the script earlier. Once you have done this, click on ’Create Project’ on the bottom.
This will now open up a new RStudio window with your project. In the future, you can start right back up where you finished last time by navigating to the .Rproj file, and double clicking it. It helps you keep everything in one place, and lets R read everything from that folder.
With simple projects, a single script file and a data file is all you may have. But with more complex projects, things can rapidly become messy. So you may want to create sub-directories within this project folder. You could use the following structure to put all files of a certain type in the same sub-directory:
  • Scripts and code: Here you put all the text files with the analytic code, including Rmarkdown files which is something we don’t introduce here.
  • Source data: Here you can put the original data. I would tend not to touch this once I have obtained the original data.
  • Documentation: This is the sub-directory where you can place all the data documentation (e.g., codebooks, questionnaires, etc.)
  • Modified data: All analysis involves doing transformations and changing things in the original data files. You don’t want to mess up the original data files, so what you should do is create new data files as soon as you start changing your source data. I would go so far as to place them in a different sub-directory.
  • Literature: Analysis is all about answering research questions. There is always a literature about these questions. I would place the relevant literature for the analytic project I am conducting in this sub-directory.
  • Reports and write-up: Here is where I would file all the reports and data visualisations that are associated with my analysis.
You can read up on why projects are useful in [219].