Skip to main content

12  Create an R Project

One of the basic steps to reproducibility and modern workflows in data analysis is to keep everything contained in a single location. In RStudio, this is done with R Projects.

Important

If you have not used R Projects before, please read all of the section on it from the introduction workshop to learn about R Projects and how they help keep things self-contained. You don’t need to do any of the exercises or activities.

There are several ways to organise a project folder. We’ll be using the structure from the package prodigenr. The project setup can be done by either:

  1. Using RStudio’s New Project menu item: “File -> New Project -> New Directory”, scroll down to “Scientific Analysis Project using prodigenr” and name the project “AdvancedR3” in the Directory Name, saving it to the “Desktop” with Browse. Note: You might need to restart RStudio if you don’t see this option.
  2. Or, running the command prodigenr::setup_project("~/Desktop/AdvancedR3") (or other location like Documents) in the R Console and manually switching to it using: File -> Open Project and navigating to the project location.

When the RStudio Project opens up again, run these commands in the R Console to finish the setup:

Console
# Create a `functions.R` file in the `R/` folder
usethis::use_r("functions", open = FALSE)

Here we use the usethis package to help set things up. usethis is an extremely useful package for managing R Projects and we highly recommend checking it out more to see how you can use it in your own work.

Since we will be using Git a lot for this workshop, do a quick check to make sure your Git is connected to the project and to RStudio. You should see the Git tab in one of the panes in RStudio or the Git icon beside the “Go to” search bar below the menu bar of RStudio. If you don’t see it, try these troubleshooting steps.

12.1 R Markdown and Quarto

We teach and use R Markdown/Quarto because it is one of the very first steps to being reproducible and because it is a very powerful tool to doing data analysis. You may or may not have heard of or used either R Markdown or Quarto.

Quarto is a next generation version of R Markdown and comes bundled with RStudio. Quarto, like R Markdown, is a way of writing text and code together to create reproducible documents of analyses. Quarto uses Markdown syntax and allows you to create many types of output documents (like books, websites, slides). There are many options for customizing these outputs.

Important

If you have not used Quarto or R Markdown before, please read over the Quarto section of the introduction workshop. Don’t do the exercises.

While inside the AdvancedR3 project, run this function in the R Console to create two new Quarto files called sandbox.qmd and report.qmd in the docs/ folder.

Console
r3::create_sandbox_qmd()
r3::create_report_qmd()

You’ll use these two files throughout the workshop. The sandbox.qmd one is where you will write in during the code-alongs and take notes (if you want). For the report.qmd file, this is what will be used within the pipeline as the final document we want our results to be in.