5  R Projects

An R Project is a folder containing .Rproj, data, code, and other files related to a data analysis. The .Rproj file creates an isolated, portable work environment, and sets the working directory to the R Project folder. Consequently, the entire R Project can be moved locally or transferred to another computer without affecting the analysis.

5.1 Directories & File Paths

working directory is the starting location used when R searches for files. When the R Project folder is the working directory, we can import data using relative file paths that can be as short as the file name. Without R Projects, we must import data using an absolute file path, meaning the entire path through (usually) many nested folders to a data file.

Below are relative and absolute paths, for a file on the author’s computer called “Friends_Cholesterol.csv” that is stored inside a “data” folder inside an R Project:

relative: data/Friends_Cholesterol.csv

absolute: /Users/stevenhobbs/Documents/3_Current_Classes/3.5800_SP23 /2.R_Projects/Data Essentials With R/data/Friends_Cholesterol.csv

Relative file paths are clearly shorter and easier to type. More importantly, the entire R project folder can be moved locally or transferred to another computer, and the relative file path remains accurate. By comparison, the absolute file path would fail and require updating anytime the R Project folder is moved.

5.2 Create a New R Project:

Use the steps below to create a new R Project called “Data Essentials With R”. Download Friends_Cholesterol.csv into a folder called “data” inside the R project folder. Make sure to specify .csv when downloading the file, as the default format is .xlsx. Use this R Project to practice coding exercises presented in lessons that follow.

  1. From the RStudio top menu, go to File > New Project…
  2. Create a folder called “data” inside the R Project folder just created.
  3. Download/save all data files in the data folder.
  4. Open a new Quarto file.
  5. Save the Quarto file within the R Project folder.

5.3 Resume an Existing R Project

1. Click on the .Rproj file within the project folder, or within RStudio choose File > Open Project…
2. After the R Project is initiated, open any project-related Quarto files.

WARNING Opening sequence

When a Quarto file is opened before the R Project is active, any relative file paths used to import data will fail. Close the Quarto file (save changes if appropriate), then activate the R Project (File > Open Project…) and lastly re-open the Quarto file.

5.4 Verify an R Project is open

Here are two ways to verify that an R Project is active:

1) The upper right corner of the RStudio window has a pull-down menu that will show the name of an open R Project. This menu also has options to create or open R Projects.

2) Click on the Files button in the bottom right RStudio panel. A blue box with an R at the far right indicates an open R Project. Clicking on the blue box shows files inside the R Project folder.

WARNING R Project vs Quarto

Do not confuse a Quarto file for the R Project. R Projects are initiated by files with a .Rproj ending. Quarto files have a .qmd ending and only contain the code we write for data analysis. Open Quarto files AFTER opening the R Project.