- 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.
Fall 2022
w
for widescreen view.f
for fullscreen view.p
for Presenter Notes.penguins
demo project you downloadedIn this workshop, you will learn:
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.
Working with projects allows you to:
Use:
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.
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
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
AKA “little p projects”
Example of a simple file structure for a project
AKA “Capital P Projects”
When you launch an RStudio Project, RStudio will:
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.
With R Markdown, you can:
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).
here
packagePaths 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)
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.
Reach out to our statistical consultants! statlab@virginia.edu
General Reproducible Research in R and RStudio:
RStudio Projects:
R Markdown:
R Markdown Cheat Sheets:
Bonus: here
package:
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.