Reproducible Analysis and Documentation

Jennifer Huck

10/13/23

Slides Usage Notes

  • Press s to view Presenter Notes. You might have to allow pop-ups, and press s again to make this work.

  • These slides are licensed as CC-BY.

Before we begin

  • Open RStudio. For best participation results, make sure you have updated RStudio to v2022.07 (or higher). To double check your version, go to Help…About RStudio. The version number is in bold at the top.

  • Be sure you can find the `penguins-quarto` demo project you downloaded.

Learning Objectives

In this workshop, you will learn:

  • How to use RStudio Projects and why you would want to
  • What R Markdown and Quarto documents are and why you would want to use them

Note: This is not a complete workshop on how to use the many options provided within R Markdown or Quarto documents. We will cover some details, but not all.

Why a project-oriented workflow?

Working with projects allows you to:

  • Work on more than one project at a time
  • Easily distribute and collaborate on projects
  • Easily stop and pick up where you left off

How to do a project-oriented workflow?

Use:

  • A dedicated directory
  • RStudio Projects
  • Git repo

Project-oriented workflow with RStudio Projects

What not to do: setwd() and rm(list = ls())

A lot of R scripts start off like this:

setwd("C:/Users/jah2ax.ESERVICES/Box Sync/_R/research/penguins")

You might also see:

rm(list = ls())

Fortunately, a project-oriented workflow designs this away.

What persists after rm(list=ls())?

Option Persists? (Y/N)
library(dplyr)
summary <- head
options(stringsAsFactors = FALSE)
Sys.setenv(LANGUAGE = “fr”)
x <- 1:5
attach(iris)

Examples taken from Pileggi 2022

What persists after rm(list=ls())?

Option Persists?
library(dplyr) Yes
summary <- head No
options(stringsAsFactors = FALSE) Yes
Sys.setenv(LANGUAGE = “fr”) Yes
x <- 1:5 No
attach(iris) Yes

Examples taken from Pileggi 2022

Organize work into projects

AKA “little p projects”

  1. Use a single directory for your research project
  2. Set working directory in R to that main directory
  3. Use relative paths

Organize work into projects

Example of a simple file structure for a project

from https://reproducible-science-curriculum.github.io/organization-RR-Jupyter/slides/02_slideshow_organization.slides.html#/1

What RStudio Projects can do for you

AKA “Capital P Projects”

When you launch an RStudio Project, RStudio will:

  1. Launch R/RStudio.
  2. Set the working directory to the project’s folder.
  3. Provide you with a fresh environment. That means there are no libraries loaded and no objects saved in your environment.

Your Turn: RStudio Projects

As an example, we will imagine you are working on two projects: polar-bears and penguins.

You can designate a new or existing folder as an RStudio Project. All that means is that RStudio leaves behind a helper file, e.g., polar-bears.Rproj. This file stores specific settings for that project.

In RStudio, use File…New Project…New Directory or click on the R cube in the upper right to get started. Create a fresh project called polar-bears.

Now click on the little down arrow next to the R cube in the upper right, click Open Project, and open the penguins-quarto project you downloaded.

Notice that you get a fresh environment when you switch back and forth.

Using Windows Explorer or Mac Finder, go to where you saved the polar-bears project. Double click on the polar-bears.rproj file. RStudio will launch your project, and you can run multiple instances of R at the same time. You should have both polar-bears and penguins-quarto projects open at the same time, using two RStudio instances. They are running in completely separate environments.

Dynamic Documents with Quarto: No more cut and paste!

With Quarto, you can:

  • Compile a single R Markdown document to a report in different formats, such as PDF, HTML, or Word.
  • Create notebooks in which you can directly run code chunks interactively.
  • Make slides for presentations (like this one!).
  • Produce dashboards with flexible, interactive, and attractive layouts.
  • Build interactive applications based on Shiny.
  • Write journal articles.
  • Author books of multiple chapters.
  • Generate websites and blogs.

Demo Quarto

We are going to use the penguins-quarto demo project to explore R Markdown documents. penguins-quarto has the following structure:

C:/Users/jah2ax/OneDrive - University of Virginia/Documents/GitHub/penguins-quarto
├── data
│   └── empty.csv
├── penguins-quarto.Rproj
├── presentation
│   ├── images
│   │   ├── figures_for_slides-01.png
│   │   └── visual_mode_2x.png
│   ├── repro_analysis_R_RStudio.bib
│   ├── repro_analysis_R_RStudio_quarto.html
│   ├── repro_analysis_R_RStudio_quarto.qmd
│   ├── repro_analysis_R_RStudio_quarto.rmarkdown
│   └── repro_analysis_R_RStudio_quarto_files
│       └── libs
│           ├── clipboard
│           │   └── clipboard.min.js
│           ├── quarto-html
│           │   ├── popper.min.js
│           │   ├── quarto-html.min.css
│           │   ├── quarto-syntax-highlighting-dark.css
│           │   ├── quarto-syntax-highlighting.css
│           │   ├── tabby.min.js
│           │   ├── tippy.css
│           │   └── tippy.umd.min.js
│           └── revealjs
│               ├── dist
│               │   ├── reset.css
│               │   ├── reveal.css
│               │   ├── reveal.esm.js
│               │   ├── reveal.esm.js.map
│               │   ├── reveal.js
│               │   ├── reveal.js.map
│               │   └── theme
│               │       ├── fonts
│               │       │   ├── league-gothic
│               │       │   │   ├── league-gothic.css
│               │       │   │   ├── league-gothic.eot
│               │       │   │   ├── league-gothic.ttf
│               │       │   │   ├── league-gothic.woff
│               │       │   │   └── LICENSE
│               │       │   └── source-sans-pro
│               │       │       ├── LICENSE
│               │       │       ├── source-sans-pro-italic.eot
│               │       │       ├── source-sans-pro-italic.ttf
│               │       │       ├── source-sans-pro-italic.woff
│               │       │       ├── source-sans-pro-regular.eot
│               │       │       ├── source-sans-pro-regular.ttf
│               │       │       ├── source-sans-pro-regular.woff
│               │       │       ├── source-sans-pro-semibold.eot
│               │       │       ├── source-sans-pro-semibold.ttf
│               │       │       ├── source-sans-pro-semibold.woff
│               │       │       ├── source-sans-pro-semibolditalic.eot
│               │       │       ├── source-sans-pro-semibolditalic.ttf
│               │       │       ├── source-sans-pro-semibolditalic.woff
│               │       │       └── source-sans-pro.css
│               │       └── quarto.css
│               └── plugin
│                   ├── highlight
│                   │   ├── highlight.esm.js
│                   │   ├── highlight.js
│                   │   ├── monokai.css
│                   │   ├── plugin.js
│                   │   └── zenburn.css
│                   ├── markdown
│                   │   ├── markdown.esm.js
│                   │   ├── markdown.js
│                   │   └── plugin.js
│                   ├── math
│                   │   ├── katex.js
│                   │   ├── math.esm.js
│                   │   ├── math.js
│                   │   ├── mathjax2.js
│                   │   ├── mathjax3.js
│                   │   └── plugin.js
│                   ├── notes
│                   │   ├── notes.esm.js
│                   │   ├── notes.js
│                   │   ├── plugin.js
│                   │   └── speaker-view.html
│                   ├── pdf-export
│                   │   ├── pdfexport.js
│                   │   └── plugin.yml
│                   ├── quarto-line-highlight
│                   │   ├── line-highlight.css
│                   │   ├── line-highlight.js
│                   │   └── plugin.yml
│                   ├── quarto-support
│                   │   ├── footer.css
│                   │   ├── plugin.yml
│                   │   └── support.js
│                   ├── reveal-menu
│                   │   ├── menu.css
│                   │   ├── menu.js
│                   │   ├── plugin.yml
│                   │   ├── quarto-menu.css
│                   │   └── quarto-menu.js
│                   ├── search
│                   │   ├── plugin.js
│                   │   ├── search.esm.js
│                   │   └── search.js
│                   └── zoom
│                       ├── plugin.js
│                       ├── zoom.esm.js
│                       └── zoom.js
├── quarto
│   ├── 01_basics.qmd
│   └── 02_computations.qmd
├── README.md
└── scripts
    └── 01-penguins.R

Note that this implements the essential “little p” project structure, by separating content into clear sub-directories.

Let’s take a look at the penguins-quarto project now. The instructions for downloading the project are in the “Before we begin” section.

Bryan, Jennifer, and Jim Hester. n.d.a. “Chapter 2: Project-Oriented Workflow.” In What They Forgot to Teach You About R.
———. n.d.b. “Chapter 3: Practice Safe Paths.” In What They Forgot to Teach You About R.
Pileggi, Shannon, Jenny Bryan, and E. David Aja. 2022. “What They Forgot to Teach You About R - Rstudio::conf(2022) Workshop - Wtf-Opening.” What They Forgot to Teach You About R. https://rstats-wtf.github.io/wtf-2022-rsc/materials/wtf-opening-2022.pdf.
Wickham, Hadley, and Garrett Grolemund. n.d. “8 Workflow: Projects.” In R for Data Science.