Module 4: R Project

Learning Objectives

After module 4, you should be able to…

  • Create an R Project
  • Check you are in the desired R Project
  • Reference the Files pane in RStudio
  • Describe “good” R Project organization

RStudio Project

RStudio “Project” is one highly recommended strategy to build organized and reproducible code in R.

  1. Helps with working directories by easily incorporating relative paths only.
  2. Helps you organize your code, data, and output.
  3. Allows you to open multiple RStudio sessions at once!

RStudio Project Creation

Let’s create a new RStudio Project.

Find the File Menu in the Menu Bar –> New Project –> New Directory –> New Project

Name your Project “IntroToR_RProject”

RStudio Project Organization

This is my personal preference for organizing an R Project. But, for this workshop it will be mandatory as it will help us help you. A critical component of conducting any data analysis is being able to reproduce it! Organizing your code, data, output, and figures is a necessary (although not sufficient) condition for reproducibility.

Create 4 sub-directories with the following names within your “SISMID_IntroToR_RProject” folder:

  • code
  • data
  • output
  • figures

We will be working from this directory for the remainder of the Workshop. Take a moment to move any R scripts you have already created to the ‘code’ sub-directory.

Some things to notice in an R Project

  1. The name of the R Project will be shown at the top of the RStudio Window
  2. If you check the working directory using getwd() you will find the working directory is set to the location where the R Project was saved.
  3. The Files pane in RStudio is also set to the location where the R Project was saved, making it easy to navigate to sub-directories directly from RStudio.

R Project - Common issues

If you simply open RStudio, it will not automatically open your R Project. As a result, when you say run a function to import data using the relative path based on your working directory, it won’t be able to find the data.

To open a previously created R Project, you need to open the R Project (i.e., double click on SISMID_IntroToR_RProject.RProj)

Summary

  • R Projects are really helpful for lots of reasons, including to improve the reproducibility of your work
  • Consistently set up your R Project’s sub-directories so that you can easily navigate the project
  • If you get an error that a file can’t be found, make sure you correctly opened the R Project by looking for the Project name at the top of the RStudio application window.

Mini Exercise

  1. Close R Studio
  2. Reopen your R Project
  3. Check that you are actually in the R Project
  4. Create a new R script and save it in your ‘code’ subdirectory
  5. Create a vector of numbers
  6. Create a vector a character values
  7. Add comment(s) to your R script to explain your code.

Acknowledgements

These are the materials we looked through, modified, or extracted to complete this module’s lecture.