class: center, middle, inverse, title-slide # Introduction to the Tidyverse ## reproducible research with R Markdown ### Malcolm Barrett ### June 18, 2019 --- class: inverse, center # Game plan: ## `readr/haven` ## transforming data: `dplyr` ## visualizing data: `ggplot2` ## data viz exercise ## **reproducible reports: `R Markdown`** --- background-image: url(http://hexb.in/hexagons/rmarkdown.png) background-position: 93% 10% ## <span style = 'color:#E69F00'>R Markdown</span> <br><br><br> -- ## Authoring framework: code and text in same document -- ## Reproducible: re-run your analysis -- ## Flexible: Output to different formats easily --- class: middle <img src="/Users/malcolmbarrett/Google Drive/Active/SER-tidyverse-2019//Images/rmarkdown_components.png" width="130%" height="130%" style="display: block; margin: auto;" /> --- background-image: url(https://lovecrafts.cdn.prismic.io/lovecrafts/01ed5aac2502cbbc4a262429747693741283bb8e_handsknitting6.jpg) background-position: 50% 50% class: middle, center <br> # <span style = 'color:#474747'>knitting</span> --- # Code chunks ```` ```{r} gapminder %>% select(year, country) ``` ```` --- # Chunk options Option | Effect -------------- | -------------------------- `include = FALSE` | run the code but don't print it or results `eval = FALSE` | don't evaluate the code `echo = FALSE` | run the code and output but don't print code `message = FALSE` | don't print messages (e.g. from a function) `warning = FALSE` | don't print warnings `fig.cap = "Figure 1` | caption output plot with "Figure 1" -- ## See the [knitr web page](https://yihui.name/knitr/options/) --- # Code chunks ```` ```{r, echo = FALSE, warning = FALSE} gapminder %>% select(year, country) ``` ```` --- # Insert code chunks with `cmd/ctrl` + `alt/option` + `I` <img src="/Users/malcolmbarrett/Google Drive/Active/SER-tidyverse-2019/Images/rmarkdown_knit.png" width="30%" height="30%" style="display: block; margin: auto;" /> --- ## Your turn 1 ### Create a code chunk. You can type it in manually, use the keyboard short-cut (Cmd/Ctrl + Option/Alt + I), or use the "Insert" button above. Put the following code in it: ```r gapminder %>% slice(1:5) %>% knitr::kable() ``` ### Knit the document --- ## Your turn 2 ### Add `echo = FALSE` to the code chunk above and re-knit ### Remove `echo = FALSE` from the code chunk and move it to `knitr::opts_chunk$set()` in the `setup` code chunk. Re-knit. What's different about this? -- ### **Make sure to remove `knitr::opts_chunk$set(echo = FALSE)`** --- # Inline Code <img src="/Users/malcolmbarrett/Google Drive/Active/SER-tidyverse-2019/Images/inline_code.png" width="536" style="display: block; margin: auto;" /> -- ## **Goes in text, not in code chunks!** --- ### Your turn 3 ### Remove `eval = FALSE` so that R Markdown evaluates the code. ### Use `summarize()` and `n_distinct()` to get the the number of unique years in gapminder and save the results as `n_years`. ### Use inline code to describe the data set in the text below the code chunk and re-knit. --- # R Markdown Basic Syntax ``` *italic* **bold** _italic_ __bold__ ``` --- # R Markdown Basic Syntax ``` # Header 1 ## Header 2 ### Header 3 ``` --- # R Markdown Basic Syntax ``` * Item 1 * Item 2 + Item 2a + Item 2b 1. Item 1 2. Item 2 ``` --- # R Markdown Basic Syntax ``` http://example.com [linked phrase](http://example.com) ``` --- # R Markdown Basic Syntax ``` ![](http://example.com/logo.png) ![optional caption text](figures/img.png) ``` --- # R Markdown Basic Syntax ``` $equation$ $$ equation $$ ``` --- # R Markdown Basic Syntax ``` superscript^2^ ~~strikethrough~~ ``` --- ## Your turn 4 ### Use Markdown syntax to stylize the text from the [Gapminder website](https://www.gapminder.org/data/documentation/gd001/) below. Experiment with bolding, italicizing, making lists, etc. --- # Output formats Function | Outputs -------------- | -------------------------- `html_document()` | HTML `pdf_document()` | PDF `word_document()` | Word .docx `odt_document()` | .odt `rtf_document()` | .rtf `md_document()` | Markdown `slidy_presentation()` | Slidy Slides (HTML) `beamer_presentation()` | Beamer Slides (PDF) `ioslides_presentation()` | ioslides (HTML) `powerpoint_presentation()` | Powerpoint Slides --- # YAML Metadata ```yaml --- output: html_document --- ``` --- # YAML Metadata ```yaml --- output: html_document: toc: true pdf_document: default --- ``` -- ## Check e.g. `?html_document()` for YAML options --- # YAML Metadata ```yaml --- author: Malcolm Barrett date: "2019-06-12" output: html_document: toc: true pdf_document: default --- ``` --- ## Your turn 5 ### Set figure options such as `dpi`, `fig.width`, and `fig.height`. ### Change the YAML header above from `output: html_document` to another output type like`pdf_document` or `word_document`. ### Add your name to the YAML header using `author: Your Name`. --- # Parameters ```yaml --- params: param1: x param2: y data: df --- ``` --- # Parameters ```r params$param1 params$param2 params$data ``` --- ### Your turn 6 ### Change the `params` option in the YAML header to use a different continent. Re-knit ```r gapminder %>% filter(continent == params$continent) %>% ggplot(aes(x = year, y = lifeExp, group = country, color = country)) + geom_line(lwd = 1, show.legend = FALSE) + scale_color_manual(values = country_colors) + theme_minimal(14) + theme(strip.text = element_text(size = rel(1.1))) + ggtitle(paste("Continent:", params$continent)) ``` --- # Bibliographies and citations ## Bibliography files: `.bib`, End Note, others ## Citation styles: `.csl` ## `[@citation-label]` --- ```yaml --- bibliography: file.bib csl: file.csl --- ``` --- ### Your turn 7 ### Cite the Causal Inference book in text below in the format ```[@citation-label]```. The label for the citation is `hernan_causal_2019` ### Add the American Journal of Epidemiology CSL to the YAML using `csl: aje.csl` --- class: middle # Check out the citr package for easy citation insertion and .bib management --- # Make cool stuff in R Markdown! ## bookdown ## blogdown ## these slides! --- class: inverse, center # Resources ## [R Markdown](https://bookdown.org/yihui/rmarkdown/): A comprehensive but friendly introduction to R Markdown and friends. Free online. ## [R for Data Science](http://r4ds.had.co.nz/): A comprehensive but friendly introduction to the tidyverse. Free online. ## [RStudio Primers](https://rstudio.cloud/learn/primers): Free interactive courses in the Tidyverse --- class: inverse, center, middle ### Thank you! ![](https://media.giphy.com/media/3oz8xIsloV7zOmt81G/giphy.gif) ###
[malcolmbarrett](https://github.com/malcolmbarrett/) ###
[@malco_barrett](https://twitter.com/malco_barrett) Slides created via the R package [xaringan](https://github.com/yihui/xaringan).