6 R projects

An R project enables your work to be bundled in a portable, self-contained folder. Within the project, all the relevant scripts, data files, figures/outputs, and history are stored in sub-folders and importantly - the working directory is the project’s root folder.

6.1 Suggested use

A common, efficient, and trouble-free way to use R is to combine these 3 elements. One discrete work project is hosted within one R project. Each element is described in the sections below.

  1. An R project
    • A self-contained working environment with folders for data, scripts, outputs, etc.
  2. The here package for relative filepaths
    • Filepaths are written relative to the root folder of the R project - see Import and export for more information
  3. The rio package for importing/exporting
    • import() and export() handle any file type by by its extension (e.g. .csv, .xlsx, .png)

6.2 Creating an R project

To create an R project, select “New Project” from the File menu.

  • If you want to create a new folder for the project, select “New directory” and indicate where you want it to be created.
  • If you want to create the project within an existing folder, click “Existing directory” and indicate the folder.
  • If you want to clone a Github repository, select the third option “Version Control” and then “Git”. See the page on Version control and collaboration with Git and Github for further details.

The R project you create will come in the form of a folder containing a .Rproj file. This file is a shortcut and likely the primary way you will open your project. You can also open a project by selecting “Open Project” from the File menu. Alternatively on the far upper right side of RStudio you will see an R project icon and a drop-down menu of available R projects.

To exit from an R project, either open a new project, or close the project (File - Close Project).

Switch projects

To switch between projects, click the R project icon and drop-down menu at the very top-right of RStudio. You will see options to Close Project, Open Project, and a list of recent projects.

Settings

It is generally advised that you start RStudio each time with a “clean slate” - that is, with your workspace not preserved from your previous session. This will mean that your objects and results will not persist session-to-session (you must re-create them by running your scripts). This is good, because it will force you to write better scripts and avoid errors in the long run.

To set RStudio to have a “clean slate” each time at start-up:

  • Select “Project Options” from the Tools menu.
  • In the “General” tab, set RStudio to not restore .RData into workspace at startup, and to not save workspace to .RData on exit.

Organization

It is common to have subfolders in your project. Consider having folders such as “data”, “scripts”, “figures”, “presentations”. You can add folders in the typical way you would add a new folder for your computer. Alternatively, see the page on Directory interactions to learn how to create new folders with R commands.

Version control

Consider a version control system. It could be something as simple as having dates on the names of scripts (e.g. “transmission_analysis_2020-10-03.R”) and an “archive” folder. Consider also having commented header text at the top of each script with a description, tags, authors, and change log.

A more complicated method would involve using Github or a similar platform for version control. See the page on Version control and collaboration with Git and Github.

One tip is that you can search across an entire project or folder using the “Find in Files” tool (Edit menu). It can search and even replace strings across multiple files.

6.3 Examples

Below are some examples of import/export/saving using here() from within an R projct. Read more about using the here package in the Import and export page.

Importing linelist_raw.xlsx from the “data” folder in your R project

linelist <- import(here("data", "linelist_raw.xlsx"))

Exporting the R object linelist as “my_linelist.rds” to the “clean” folder within the “data” folder in your R project.

export(linelist, here("data","clean", "my_linelist.rds"))

Saving the most recently printed plot as “epicurve_2021-02-15.png” within the “epicurves” folder in “outputs” folder in your R project.

ggsave(here("outputs", "epicurves", "epicurve_2021-02-15.png"))

6.4 Resources

RStudio webpage on using R projects