Fall 2022

Slides Usage Notes

  • Recommended to press w for widescreen view.
  • Also recommended to press f for fullscreen view.
  • Press p for Presenter Notes.
  • These slides are licensed as CC-BY.

Before we begin

  • Open RStudio. Make sure updated to v 1.4 if using Visual Editor
  • Be sure you can find the penguins 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 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 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

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 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 projects open at the same time, using two RStudio instances. They are running in completely separate environments.

Dynamic Documents with R Markdown: No more cut and paste!

With R Markdown, 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.

R Markdown Visual Editor

R Markdown Visual Editor brings some of the features and ease-of-use that GUIs provide, while still always maintaining the source code that is the .Rmd file (R Markdown file).

Bonus: here package

Paths created relative to the top level directory.

The here package enables easy file referencing with project-oriented workflows. Paths are created relative to the top-level directory. It is most useful when you work with R Markdown documents a lot, as it smoothes out some of the idiosyncrasies of how R Markdown looks for files.

The here package always points to the root directory, which is typically where we set the working directory with our RStudio Project.

## here() starts at C:/Users/jah2ax/OneDrive - University of Virginia/Documents/GitHub/reproAnalysis/slides

References: [Barrett (2018)](Richmond, n.d.)[Müller and Bryan (n.d.a)](Müller and Bryan, n.d.b)

Demo R Markdown

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

## C:/Users/jah2ax/OneDrive - University of Virginia/Documents/GitHub/penguins
## ├── data
## │   └── empty.csv
## ├── documents
## │   ├── apa.csl
## │   ├── references.bib
## │   ├── report.html
## │   └── report.Rmd
## ├── penguins.Rproj
## ├── 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 project now. The instructions for downloading the project are in the “Before we begin” section.

Recommended for further learning

References

Barrett, Malcolm. 2018. “Why Should I Use the Here Package When I’m Already Using Projects?” Malcolm Barrett. https://malco.io/2018/11/05/why-should-i-use-the-here-package-when-i-m-already-using-projects/.

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.

Grolemund, Garrett, J. J. Allaire. n.d. R Markdown: The Definitive Guide.

Müller, Kirill, and Jennifer Bryan. n.d.a. “Here Vignette.” https://cran.r-project.org/web/packages/here/vignettes/here.html.

———. n.d.b. “Using Here with Rmarkdown Vignette.” https://cran.r-project.org/web/packages/here/vignettes/rmarkdown.html.

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.

Richmond, Jenny. n.d. “How to Use the ‘Here‘ Package.” http://jenrichmond.rbind.io/post/how-to-use-the-here-package/.

RStudio. n.d.a. “R Markdown: Introduction.” https://rmarkdown.rstudio.com/lesson-1.html.

RStudio. n.d.b. “Visual R Markdown.” https://rstudio.github.io/visual-markdown-editing/#/.

RStudio. n.d.c. “Visual R Markdown: Citations.” https://rstudio.github.io/visual-markdown-editing/#/citations.

Wickham, Hadley, and Garrett Grolemund. n.d. “8 Workflow: Projects.” In R for Data Science.