class: center, middle, inverse, title-slide .title[ # RMarkdown for Reproducible Reports ] .subtitle[ ## PHC 6701 - Summer 2022 ] .author[ ### Catalina Cañizares ] .date[ ### May 19, 2022 ] --- # Overview ## We will cover the following: + Why we need reproducible reporting + Installing Packages + A Basic RMarkdown Document + Code Chunks and Output + Annotating your Work + Knit a report + Inline code --- # Why should we care? "Reproducibility is a minimum requirement for a finding to be believable" (Goodman et al, 2016 ) Yet more than `70%` of researchers have reported failure to reproduce another experiment. .center[] *Source*: [1,500 scientists lift the lid on reproducibility](https://www.nature.com/articles/533452a) --- # Why is there a crisis and what can we do? .center[] .center[<mark>Making our research more reproducible and shariable</mark> ] *Source*: [1,500 scientists lift the lid on reproducibility](https://www.nature.com/articles/533452a) --- # The Magic: R Markdown We magically produce `reproducible` reports. Future you and anyone else can pick up your `.Rmd file` and: + Reproduce the same results + Have your code, results and text in the same document + Results, figures and tables automatically generate, so updates are not a nightmare + The file format is future-proof (ask for Dr. Odom's thesis...) .center[] *Source*: [Making your research reproducible](https://hbctraining.github.io/reproducibility-tools/lessons/01-Rmarkdown_basics.html) --- # Installing the *`down` and `knitr` Packages .pull-left[ The `knitr` package:  ] .pull-right[ The `*down` packages: + rmarkdown - basic documents + bookdown - books with chapters + blogdown - blog posts + distill - web format scientific articles + rticles - journal articles + rUM - articles from a template ] ```r install.packages("knitr") ``` ```r install.packages(c("rmarkdown", "rUM")) ``` --- # Why so many packages? .center[] *Source*: [R Markdown Workshop](https://ulyngs.github.io/oxberlin-2021-rmd-workshop/) --- # We are all set to create a .Rmd file ``` (`and pass this class`) ``` .pull-left[ .center[] ] .pull-right[ .center[] ] --- class: segue Inspecting the .Rmd file --- # .Rmd File .center[] --- class: segue YAML --- # The Meta-Data Header: <mark>YAML</mark> .pull-left[ + The first few lines of the document are the document header. + These lines tell R crucial information about how to build your report. + The entire header, and all of the document options in it, are bounded by the three horizontal dashes <mark>(---) </mark> above and below. ] .pull-right[.center[] ] **Currently our header is quite basic. It includes:** + The title of the document; title: "My R Markdown File" + Who wrote it: author: "Catalina Canizares" + Today’s date: date: "2022-05-11" + The output type: output: html_document --- # Different YAML examples .pull-left[ **Word Manuscript**  ] .pull-right[ **html Presentation**  ] .pull-left[ **html Report**  ] .pull-right[ **My pdf CV** ] [Useful YAML options for HTML reports](https://www.r-bloggers.com/2020/08/useful-yaml-options-for-generating-html-reports-in-r/) --- class: segue Code Chunks --- # The Chunks Anatomy of a Chunk .center[ ]  *Source:* [Code Chunks](https://rmarkdown.rstudio.com/lesson-3.html) --- # More about Chunks .center[ **Make chunks like a `pro`**  ] .center[ **Name chunks like a `pro` with <mark>kebab-case</mark>**  ] --- # Code chunk examples  .center[ ** How it should look ** ] --- class: segue Markup Text --- # Markup Text Now that you have a report with a header and some code, you need to explain what your code is doing and why. + This is where the plain text comes in. + Outside of a code chunk, type anything you want. You can even include pictures and tables. .center[ ] --- # Pandoc’s Markdown .center[ ] --- # Exercise .pull-left[ + Open a new `R Markdown` file + Modify the YAML with your information and name your file + Define the setup chunk so we can see the code and the results. + Create a chunk and load `library(palmerpenguins)` and `library(tidyverse)` + Create a chunk and load the penguin data `data("penguins")` + Create another chunk, name it and explore the data with the glimpse function `glimpse(penguins)` + Create another chunk, let's check how many species there are using `penguins %>% count(species)`. Above the chunk comment your finding. ] .pull-right[ + Create another chunk, let's check the flipper length distribution with `ggplot(penguins), aes(flipper_length_mm) + geom_histogram()` Above the chunk comment your finding.  *Source:* [Penguins](https://allisonhorst.github.io/palmerpenguins/articles/examples.html) ] --- class: segue Knit .center[ ] --- # We are ready to knit our report Save your report and... .center[ ] --- # Look at your Console while it knits .pull-left[ .center[ ] ] .pull-right[ .center[ ] ] --- # A little more more of that reproducible magic ```r library(palmerpenguins) data("penguins") ``` Inside your text you can include executable code with the syntax:  For example: There are 344 rows in the `penguins` data set. **The truth:***  --- # Save yourself some work  rUM contains templates to build a good R Markdown. ```r rUM::make_project("./") # ./ means into this folder ```  --- # Save yourself some work II   --- # A final note [Quarto](https://quarto.org/docs/tools/rstudio.html) + Quarto doesn’t have a dependency or requirement for R. + R Markdown is not going away! + Quarto chunk options are typically included in special comments at the top of code chunks  --- # More resources [R Markdown Cheat Sheet](https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf) [R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/) .center[  ] --- class: segue # Questions? .center[  ] --- **This presentation was based on:** 1. Dr. Gabriel Odom's Lesson 3: RMarkdown for Reproducible Reports 2. Dr. Raymond Balise class presentation on Markdown and R Markdown on January 25, 2022 at University of Miami. 3. All the sources posted throughout the presentation