class: title-slide, right, top # An Introduction to R Markdown ## Dr. Alison Hill · RStudio #### <https://rstd.io/rmd4cdc> --- class: middle, center # <i class="fas fa-cloud"></i> # Go here and log in (free): https://rstd.io/rmd4medicine-cloud --- layout: true <div class="my-footer"><span>https://rstd.io/rmd4cdc</span></div> --- class: inverse, middle, center # What is R Markdown? --- # What is R Markdown? -- 1. An authoring framework for data science. -- 1. A document format (`.Rmd`). -- 1. An R package named `rmarkdown`. -- 1. A file format for making dynamic documents with R. -- 1. A tool for integrating prose, code, and results. -- 1. A computational document. -- 1. Wizardry. --- # What is R Markdown? 1. ["An authoring framework for data science."](https://rmarkdown.rstudio.com/lesson-1.html) (✔️) 1. [A document format (`.Rmd`).](https://bookdown.org/yihui/rmarkdown/) (✔️) 1. [An R package named `rmarkdown`.](https://rmarkdown.rstudio.com/docs/) (✔️) 1. ["A file format for making dynamic documents with R."](https://rmarkdown.rstudio.com/articles_intro.html) (✔️) 1. ["A tool for integrating text, code, and results."](https://r4ds.had.co.nz/communicate-intro.html) (✔️) 1. ["A computational document."](http://radar.oreilly.com/2011/07/wolframs-computational-documen.html) (✔️) 1. Wizardry. (🧙♂️) --- background-image: url(images/rmarkdown_wizards.png) background-size: contain .footnote[Art by [Allison Horst](https://twitter.com/allison_horst?lang=en)] --- class: inverse, middle, center # Why R Markdown? --- class: center background-image: url(https://media.giphy.com/media/11fDMHAzihB8D6/source.gif) # Reproducibility crisis? ??? We are not here today to solve science or medicine's reproducibility crisis. What I do think R Markdown can help you with today when you leave here is to tackle two other crises: one I think of as the confidence crisis... --- class: center background-image: url(https://media.giphy.com/media/OPZZ6KWJOZ3AA/source.gif) # Confidence crisis --- class: center background-image: url(https://dickbrouwer.com/img/meeting_cheese.png) background-size: contain # 🧀 Cheese crisis 🧀 .footnote[https://dickbrouwer.com/blog/a-guide-to-better-meetings/] --- class: center background-image: url(images/remote-crisis.jpg) background-size: cover # 🚨 Current crisis 🚨 --- class: middle, center # Change your mental model .pull-left[ ### Source ↔ output <img src="images/word.png" width="50%" /> ] .pull-right[ ### Source → output <img src="images/rmd-file.png" width="50%" /> ] --- class: middle, center # Change your mental model .pull-left[ ### Source ↔ output <img src="images/haba-elise.jpg" width="50%" /> ] .pull-right[ ### Source → output <img src="images/doll.png" width="50%" /> ] --- background-image: url(images/welliewishers/Slide1.png) background-size: cover class: top, center .footnote[https://www.americangirl.com/shop/c/welliewishers] --- background-image: url(images/welliewishers/Slide2.png) background-size: cover class: top, center .footnote[https://www.americangirl.com/shop/c/welliewishers] --- class: middle, inverse, center # Same ~~doll~~ source -- # Different ~~outfits~~ outputs --- class: middle, center # `html_document` <img src="images/doll.png" width="40%" /> --- class: middle, center # What is inside? ![](images/doc-make.jpg) --- name: card0 background-image: url(images/card0.png) background-size: contain .footnote[https://www.dear-data.com/] --- name: card1 background-image: url(images/card1.png) background-size: contain .footnote[https://www.dear-data.com/] --- # metadata: YAML .pull-left[ _"YAML Ain't Markup Language"_ ```yaml --- key: value --- ``` ] .pull-right[ <img src="images/orchestra.jpg" width="75%" style="display: block; margin: auto;" /> ] --- class: middle, center # Output options --- # Save output options in your YAML .pull-left[ ```yaml --- author: Your name here title: Your title here output: html_document --- ``` ```yaml --- author: Your name here title: Your title here output: html_document: toc: true toc_float: true theme: flatly --- ``` ] .pull-right[ <img src="images/orchestra.jpg" width="75%" style="display: block; margin: auto;" /> ] --- background-image: url(images/Single-rmd.png) background-size: contain --- background-image: url(images/Single-rmd1.png) background-size: contain --- background-image: url(images/Single-rmd2.png) background-size: contain --- background-image: url(images/Single-rmd3.png) background-size: contain --- class: top center <video width="1530" height="610" controls> <source src="images/single-doc-knit.mp4" type="video/mp4"> </video> --- class: middle center background-image: url(images/Single-knit1.png) background-size: contain ??? Here is what we knit. -- <img src="images/Single-knit2.png" style=" position: absolute; width: 91%; top: -53px; right: 5px; "> ??? Using the `THEME:` key in our YAML, we changed our font and colors. -- <img src="images/Single-knit3.png" style=" position: absolute; width: 97%; top: -45px; right: -51px; "> ??? And we have this nice table of contents floating off to the side... --- class: middle # Check-in .pull-left[ How do you use only the **default** output options? (a) ```yaml --- output: html_document() --- ``` (b) ```yaml --- output: html_document --- ``` ] -- .pull-right[ How do you add an output **option** to a format in your YAML? (a) ```yaml --- output: html_document: toc: true --- ``` (b) ```yaml --- output: html_document(toc=true) --- ``` ] --- class: middle, center # Output formats --- class: middle, center <a href="https://rmarkdown.rstudio.com/docs/reference/index.html#section-output-formats" target="_blank"><img src="images/rmdbase-formats.png" width="40%" /></a> https://rmarkdown.rstudio.com/docs/reference/index.html#section-output-formats --- # 🧶 Knit .pull-left[ <img src="images/knit-dropdown.png" width="60%" style="display: block; margin: auto;" /> ] .pull-right[ ```yaml --- author: Your name here title: Your title here output: html_document: toc: true toc_float: true theme: flatly word_document: default pdf_document: default --- ``` ] ??? This is a great way to "control" your knit button! Notice that when you knit, it respects those output options in your YAML. This way you "save" your output options --- class: middle # Check-in .pull-left[ How do you add another output **format** to your YAML? (a) ```yaml --- output: html_document: default word_document: default --- ``` (b) ``` --- output: html_document() word_document() --- ``` ] -- .pull-right[ How do you now add output **options** to your YAML? (a) ```yaml --- output: html_document: toc: true word_document: default --- ``` (b) ``` --- output: html_document(toc=true) word_document(default) --- ``` ] --- class: middle, center # Built-in output formats <img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/rmarkdown.png" width="32%" /> --- class: middle, center <img src="images/outputs.png" width="50%" /> ??? Built in formats (in the `rmarkdown` package) versus extensions (different packages that extend output formats available to you) --- class: middle, center # Extension output formats <img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/flexdashboard.png" width="32%" /><img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/bookdown.png" width="32%" /><img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/xaringan.png" width="32%" /> --- class: middle, left <iframe src="https://rstudio.github.io/distill/" width="100%" height="400px"></iframe> --- class: middle .center[ # Use an extension package ] .pull-left[ ```yaml --- author: Your name here title: Your title here output: distill::distill_article: default bookdown::html_document2: default --- ``` ] .pull-right[ ```yaml --- author: Your name here title: Your title here output: distill::distill_article: toc: true bookdown::html_document2: toc: true theme: flatly toc_float: true --- ``` ] --- template: card0 --- template: card1 --- name: card2 background-image: url(images/card2.png) background-size: contain .footnote[https://www.dear-data.com/] --- class: center, middle # Do this! 👇 # https://commonmark.org/help/tutorial/ --- # <center>Headers</center> -- .pull-left[ ```markdown # HEADER 1 ## HEADER 2 ### HEADER 3 #### HEADER 4 ##### HEADER 5 ###### HEADER 6 ``` ] -- .pull-right[ # HEADER 1 ## HEADER 2 ### HEADER 3 #### HEADER 4 ##### HEADER 5 ###### HEADER 6 ] --- ![](images/rmd-nav.png) --- # <center>Text</center> -- .pull-left[ ```markdown Childhood **vaccines** are one of the _great triumphs_ of modern medicine. ``` ] -- .pull-right[ Childhood **vaccines** are one of the _great triumphs_ of modern medicine. ] --- class: top # <center>Lists</center> -- .pull-left[ ```markdown Indeed, parents whose children are vaccinated no longer have to worry about their child's death or disability from: - whooping cough, - polio, - diphtheria, - hepatitis, or - a host of other infections. Vaccines are the most cost-effective health care interventions there are. We have three new, extremely effective vaccines to roll out in developing countries: 1. pneumonia 1. rotavirus 1. meningitis A ``` ] -- .pull-right[ Indeed, parents whose children are vaccinated no longer have to worry about their child's death or disability from: - whooping cough, - polio, - diphtheria, - hepatitis, or - a host of other infections. Vaccines are the most cost-effective health care interventions there are. We have three new, extremely effective vaccines to roll out in developing countries: 1. pneumonia 1. rotavirus 1. meningitis A ] --- # <center>Images</center> -- .pull-left[ ```markdown Receiving rubella vaccination, Brazil, 2008. ![](http://bit.ly/VaccineBySandraRugio) ``` ] -- .pull-right[ Receiving rubella vaccination, Brazil, 2008. ![](http://bit.ly/VaccineBySandraRugio) ] --- # <center>Links</center> -- .pull-left[ ```markdown [Photo](https://en.wikipedia.org/wiki/Vaccine) Receiving rubella vaccination, Brazil, 2008. ![](http://bit.ly/VaccineBySandraRugio) ``` ] -- .pull-right[ [Photo](https://en.wikipedia.org/wiki/Vaccine) Receiving rubella vaccination, Brazil, 2008. ![](http://bit.ly/VaccineBySandraRugio) ] --- name: 03-formative class: middle # Check-in -- .pull-left[ How do you add headers in Markdown? `! Header` `- Header` `# Header` `1. Header` ] -- .pull-right[ What about lists? Bulleted? Numbered? `! Item 1` `- Item 1` `# Item 1` `1. Item 1` ] --- template: card0 --- template: card1 --- template: card2 --- name: card3 background-image: url(images/card3.png) background-size: contain --- class: middle, center, inverse # `code` -> plots --- # Code chunks .pull-left[ ```` ```{r} mockdata %>% distinct(arm) ``` ```` What is the fate of this chunk? ] -- .pull-right[ ```r mockdata %>% distinct(arm) #> # A tibble: 3 x 1 #> arm #> <chr> #> 1 A: IFL #> 2 F: FOLFOX #> 3 G: IROX ``` ] --- # Code chunks .pull-left[ ```` ```{r} n_sites <- n_distinct(mockdata$site) ``` ```` What fate do you predict here? ] -- .pull-right[ ```r n_sites <- n_distinct(mockdata$site) ``` ] --- # Code chunks .pull-left[ ```` ```{r} n_sites <- n_distinct(mockdata$site) n_sites ``` ```` ] -- .pull-right[ ```r n_sites <- n_distinct(mockdata$site) n_sites #> [1] 1 ``` ] --- # Including plots .pull-left[ ```r # PLOT: Age distribution ------------------------------------------------------- ggplot(mockdata, aes(age)) + geom_histogram(color = 'white', fill = scico(1, begin = .3, palette = "berlin"), bins = 20) + labs(x = "Age", y = "Count") + scale_y_continuous( breaks = scales::pretty_breaks() ) ``` Will this print? ] -- .pull-right[ <img src="index_files/figure-html/unnamed-chunk-23-1.png" width="504" /> ] --- # Including plots .pull-left[ ```r # PLOT: Age distribution ------------------------------------------------------- age_histogram <- ggplot(mockdata, aes(age)) + geom_histogram(color = 'white', fill = scico(1, begin = .3, palette = "berlin"), bins = 20) + labs(x = "Age", y = "Count") + scale_y_continuous( breaks = scales::pretty_breaks() ) ``` What about this? ] -- .pull-right[ ] --- # Including plots .pull-left[ ```r # PLOT: Age distribution ------------------------------------------------------- age_histogram <- ggplot(mockdata, aes(age)) + geom_histogram(color = 'white', fill = scico(1, begin = .3, palette = "berlin"), bins = 20) + labs(x = "Age", y = "Count") + scale_y_continuous( breaks = scales::pretty_breaks() ) age_histogram ``` ] -- .pull-right[ <img src="index_files/figure-html/unnamed-chunk-25-1.png" width="504" /> ] ??? so, how did we get a figure into R Markdown? Answer: it has to print! --- # Chunk options for plots - fig resolution - fig size - [fig "device"](https://r4ds.had.co.nz/graphics-for-communication.html#other-important-options) .footnote[https://yihui.name/knitr/options/#plots] --- # out.width .pull-left[ ```` ```{r out.width="70%"} age_histogram ``` ```` <img src="index_files/figure-html/unnamed-chunk-26-1.png" width="70%" /> ] -- .pull-right[ ```` ```{r out.width="10%"} age_histogram ``` ```` <img src="index_files/figure-html/unnamed-chunk-27-1.png" width="10%" /> See also: + `fig.width` + `fig.height` + `fig.asp` https://r4ds.had.co.nz/graphics-for-communication.html#figure-sizing ] --- # fig.path ```` ```{r fig.path="figs/", echo=FALSE} ae_pct_plot ``` ```` <img src="index_files/figure-html/unnamed-chunk-28-1.png" width="70%" /> --- # fig.path ```` ```{r ae-plot, fig.path="figs/", echo=FALSE} ae_pct_plot ``` ```` <img src="index_files/figure-html/ae-plot-1.png" width="70%" /> --- # Chunk labels .pull-left[ ```` ```{r peek} glimpse(mockdata) ``` ```` ] .pull-right[ + Place between curly braces<br>`{r label}` + Separated options with commas<br>`{r label, option1=value}` + Careful! No duplicate chunk labels ```` ```{r peek} head(mockdata) ``` ```` ``` Error in parse_block(g[-1], g[1], params.src) : duplicate label 'peek' Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block Execution halted ``` ] ??? how can we make it easier on ourselves to explore the code in here? show how to add chunk labels and view in IDE interactively --- class: top, center background-image: url(images/plants.jpg) background-size: 70% background-color: #fff5ec # Chunks as crops or houseplants? .footnote[https://masalmon.eu/2017/08/08/chunkpets/] --- class: middle, center # A good chunk label .pull-left[ ### Good `my-plot` `myplot` `myplot1` `myplot-1` `MY-PLOT` ] .pull-right[ ### Bad `my_plot` `my plot` everything else! ] --- class: middle, center # A good chunk label Think: kebabs, not snakes --- background-image: url(images/pour.jpg) background-position: left background-size: contain class: middle, center .pull-right[ ![](images/figs-label.png) ] --- # fig.cap ```` ```{r age-plot, echo=FALSE, fig.cap='Age distributions'} age_histogram ``` ```` .center[ <img src="index_files/figure-html/age-plot-1.png" width="25%" /> **Fig. 1:** Age distributions ] --- # Cross-reference a figure .pull-left[ See Figure `\@ref(fig:age-plot)`. ```` ```{r age-plot, echo=FALSE, fig.cap='Age distributions'} age_histogram ``` ```` ] .pull-right[ See Figure [1](). .center[ <div class="figure"> <img src="index_files/figure-html/unnamed-chunk-29-1.png" alt="Age distributions" width="70%" /> <p class="caption">Age distributions</p> </div> ] ] .footnote[Cross-referencing only works in these output packages: distill, bookdown, blogdown] --- # Key takeaway Once you have: ✔️ A labeled chunk that produces a plot ✔️ A figure caption using `fig.cap` Then you can have: ### Cross-references!* .footnote[\* and you are using distill, bookdown, or blogdown output formats] --- # message / warning ![](https://www.tidyverse.org/images/tidyverse_1.2.0/tidyverse_1-2-0_pkg_load.gif) --- # message / warning .pull-left[ ```` ```{r message=FALSE, warning=FALSE} library(tidyverse) ``` ```` ] .pull-right[ ```r library(tidyverse) ``` ] --- # Other useful chunk options .pull-left[ ```` ```{r} glimpse(mockdata) ``` ```` ] .pull-right[ ```r glimpse(mockdata) #> Rows: 1,499 #> Columns: 26 #> $ case <dbl> 110754, 99706, 105271, 105001, 112263, 86205, 99508, 90… #> $ age <dbl> 67, 74, 50, 71, 69, 56, 50, 57, 51, 63, 61, 59, 61, 59,… #> $ arm <chr> "F: FOLFOX", "A: IFL", "A: IFL", "G: IROX", "F: FOLFOX"… #> $ sex <chr> "Male", "Female", "Female", "Female", "Female", "Male",… #> $ race <chr> "Caucasian", "Caucasian", "Caucasian", "Caucasian", "Ot… #> $ fu_time <dbl> 922, 270, 175, 128, 233, 120, 369, 421, 387, 363, 168, … #> $ fu_stat <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2… #> $ ps <dbl> 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0… #> $ hgb <dbl> 11.5, 10.7, 11.1, 12.6, 13.0, 10.2, 13.3, 12.1, 13.8, 1… #> $ bmi <dbl> 25.09861, 19.49786, NA, 29.42922, 26.35352, 19.03673, 2… #> $ alk_phos <dbl> 160, 290, 700, 771, 350, 569, 162, 152, 231, 492, 740, … #> $ ast <dbl> 35, 52, 100, 68, 35, 27, 16, 12, 25, 18, 45, 16, 50, 80… #> $ mdquality_s <dbl> NA, 1, 1, 1, NA, 1, 1, 1, 1, 1, NA, NA, 1, 0, 1, 1, 0, … #> $ age_ord <chr> "60-69", "70-79", "40-49", "70-79", "60-69", "50-59", "… #> $ site <chr> "Portland", "Portland", "Portland", "Portland", "Portla… #> $ country <chr> "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA",… #> $ ethnicity <chr> "White (not Hispanic)", "White (not Hispanic)", "White … #> $ name <chr> "Barrett Wafford", "Shelby Moore", "Farrah Morgan", "Br… #> $ first_name <chr> "Barrett", "Shelby", "Farrah", "Brianna", "Kareema", "J… #> $ last_name <chr> "Wafford", "Moore", "Morgan", "Rasico", "el-Saleh", "Pi… #> $ ae_low_wbc <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ ae_neuropathy <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ ae_diarrhea <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ ae_vomiting <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ ae_blood_clot <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ fu_fct <fct> Died, Died, Died, Died, Died, Died, Died, Died, Died, D… ``` ] --- # echo .pull-left[ ```` ```{r echo=FALSE} glimpse(mockdata) ``` ```` ] .pull-right[ ``` #> Rows: 1,499 #> Columns: 26 #> $ case <dbl> 110754, 99706, 105271, 105001, 112263, 86205, 99508, 90… #> $ age <dbl> 67, 74, 50, 71, 69, 56, 50, 57, 51, 63, 61, 59, 61, 59,… #> $ arm <chr> "F: FOLFOX", "A: IFL", "A: IFL", "G: IROX", "F: FOLFOX"… #> $ sex <chr> "Male", "Female", "Female", "Female", "Female", "Male",… #> $ race <chr> "Caucasian", "Caucasian", "Caucasian", "Caucasian", "Ot… #> $ fu_time <dbl> 922, 270, 175, 128, 233, 120, 369, 421, 387, 363, 168, … #> $ fu_stat <dbl> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2… #> $ ps <dbl> 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0… #> $ hgb <dbl> 11.5, 10.7, 11.1, 12.6, 13.0, 10.2, 13.3, 12.1, 13.8, 1… #> $ bmi <dbl> 25.09861, 19.49786, NA, 29.42922, 26.35352, 19.03673, 2… #> $ alk_phos <dbl> 160, 290, 700, 771, 350, 569, 162, 152, 231, 492, 740, … #> $ ast <dbl> 35, 52, 100, 68, 35, 27, 16, 12, 25, 18, 45, 16, 50, 80… #> $ mdquality_s <dbl> NA, 1, 1, 1, NA, 1, 1, 1, 1, 1, NA, NA, 1, 0, 1, 1, 0, … #> $ age_ord <chr> "60-69", "70-79", "40-49", "70-79", "60-69", "50-59", "… #> $ site <chr> "Portland", "Portland", "Portland", "Portland", "Portla… #> $ country <chr> "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA",… #> $ ethnicity <chr> "White (not Hispanic)", "White (not Hispanic)", "White … #> $ name <chr> "Barrett Wafford", "Shelby Moore", "Farrah Morgan", "Br… #> $ first_name <chr> "Barrett", "Shelby", "Farrah", "Brianna", "Kareema", "J… #> $ last_name <chr> "Wafford", "Moore", "Morgan", "Rasico", "el-Saleh", "Pi… #> $ ae_low_wbc <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ ae_neuropathy <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ ae_diarrhea <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ ae_vomiting <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ ae_blood_clot <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0… #> $ fu_fct <fct> Died, Died, Died, Died, Died, Died, Died, Died, Died, D… ``` ] --- # eval .pull-left[ ```` ```{r eval=FALSE} glimpse(mockdata) ``` ```` ] .pull-right[ ```r glimpse(mockdata) ``` ] --- # include .pull-left[ ```` ```{r include=FALSE} glimpse(mockdata) ``` ```` ] .pull-right[ ] --- # Using chunk options .pull-left[ ```` ```{r echo=TRUE, results='hide'} glimpse(mockdata) ``` ```` ] .pull-right[ ```r glimpse(mockdata) ``` ] + Place between curly braces<br>`{r option=value}` + Multiple options separated by commas<br>`{r option1=value, option2=value}` + Careful! The `r` part is the **code engine** (other engines possible) --- # A global `setup` chunk 🌍 One chunk to rule them all .pull-left[ ```` ```{r setup, include = FALSE} knitr::opts_chunk$set( warning = FALSE, message = FALSE, echo = FALSE, fig.path = "alison-figs/" ) ``` ```` ] .pull-right[ + A special chunk label: `setup` + Typically the first chunk + All following chunks will use these options (i.e., sets global chunk options) + **Tip**: set `include=FALSE` + You can (and should) use individual chunk options too ] --- .left-column[ # Default options ] .right-column[ ```r str(knitr::opts_chunk$get()) #> List of 53 #> $ eval : logi TRUE #> $ echo : logi TRUE #> $ results : chr "markup" #> $ tidy : logi FALSE #> $ tidy.opts : NULL #> $ collapse : logi TRUE #> $ prompt : logi FALSE #> $ comment : chr "#>" #> $ highlight : logi TRUE #> $ strip.white : logi TRUE #> $ size : chr "normalsize" #> $ background : chr "#F7F7F7" #> $ cache : logi FALSE #> $ cache.path : chr "index_cache/html/" #> $ cache.vars : NULL #> $ cache.lazy : logi TRUE #> $ dependson : NULL #> $ autodep : logi FALSE #> $ cache.rebuild: logi FALSE #> $ fig.keep : chr "high" #> $ fig.show : chr "asis" #> $ fig.align : chr "default" #> $ fig.path : chr "index_files/figure-html/" #> $ dev : chr "png" #> $ dev.args : NULL #> $ dpi : num 72 #> $ fig.ext : NULL #> $ fig.width : num 7 #> $ fig.height : num 7 #> $ fig.env : chr "figure" #> $ fig.cap : NULL #> $ fig.scap : NULL #> $ fig.lp : chr "fig:" #> $ fig.subcap : NULL #> $ fig.pos : chr "" #> $ out.width : NULL #> $ out.height : NULL #> $ out.extra : NULL #> $ fig.retina : num 1 #> $ external : logi TRUE #> $ sanitize : logi FALSE #> $ interval : num 1 #> $ aniopts : chr "controls,loop" #> $ warning : logi TRUE #> $ error : logi FALSE #> $ message : logi TRUE #> $ render : NULL #> $ ref.label : NULL #> $ child : NULL #> $ engine : chr "R" #> $ split : logi FALSE #> $ include : logi TRUE #> $ purl : logi TRUE ``` ] --- template: card0 --- template: card1 --- template: card2 --- template: card3 --- background-image: url(images/card4.png) background-size: contain --- class: center background-image: url(images/typewriter.jpg) background-size: contain background-color: #f6f6f6 # Markdown tables --- class: center # Markdown tables .pull-left[ ```markdown | Time | Session | Topic | |:--------------|:-------:|---------:| | _left_ | _center_| _right_ | | 01:00 - 01:50 | 1 | Anatomy | | 01:50 - 02:00 | | *Break* | | 02:00 - 02:45 | 2 | Tables | | 02:45 - 03:00 | | *Break* | ``` ] -- .pull-right[ | Time | Session | Topic | |:--------------|:-------:|---------:| | _left_ | _center_| _right_ | | 01:00 - 01:50 | 1 | Anatomy | | 01:50 - 02:00 | | *Break* | | 02:00 - 02:45 | 2 | Tables | | 02:45 - 03:00 | | *Break* | ] --- class: middle, center, inverse # Tables ![](https://media.giphy.com/media/i9c6lFmOSXKZW/giphy.gif) --- class: center, middle # Two table types .pull-left[ ### for you <img src="images/tables-you.jpg" width="3691" /> ] -- .pull-right[ ### for everyone else <img src="images/tables-them.jpg" width="3691" /> ] --- .pull-left[ ![](images/imani-_TuIdedBPwQ-unsplash.jpg) ] .pull-right[ ![](images/gear_shift.jpg) ] .footnote[Images from [Unsplash](https://unsplash.com/photos/_TuIdedBPwQ) and [Wikipedia](https://commons.wikimedia.org/wiki/File:FIAT_500L_gear_shift.jpg)] --- # Let's try using data now ```r mockdata <- read_csv(here::here("mockpaper/data/mockdata.csv")) %>% mutate(fu_fct = fct_recode(as.factor(fu_stat), "Lived" = "1", "Died" = "2")) %>% mutate_at(vars(starts_with("ae_")), ~as.factor(.)) #> Parsed with column specification: #> cols( #> .default = col_double(), #> arm = col_character(), #> sex = col_character(), #> race = col_character(), #> age_ord = col_character(), #> site = col_character(), #> country = col_character(), #> ethnicity = col_character(), #> name = col_character(), #> first_name = col_character(), #> last_name = col_character() #> ) #> See spec(...) for full column specifications. ``` --- class: middle .left-column[ ![](https://raw.githubusercontent.com/sfirke/janitor/master/man/figures/logo_small.png) ] .right-column[ # the janitor package <http://sfirke.github.io/janitor/> ] --- # 1-way frequency `tabyl()` .pull-left[ ```r mockdata %>% tabyl(arm) #> arm n percent #> A: IFL 428 0.2855237 #> F: FOLFOX 691 0.4609740 #> G: IROX 380 0.2535023 ``` ] -- .pull-right[ ```r mockdata %>% tabyl(arm) %>% adorn_totals("row") %>% adorn_pct_formatting() #> arm n percent #> A: IFL 428 28.6% #> F: FOLFOX 691 46.1% #> G: IROX 380 25.4% #> Total 1499 100.0% ``` ] --- # 2-way frequency `tabyl()` .pull-left[ ```r mockdata %>% tabyl(arm, fu_fct) #> arm Lived Died #> A: IFL 18 410 #> F: FOLFOX 99 592 #> G: IROX 26 354 ``` ] -- .pull-right[ ```r mockdata %>% tabyl(arm, fu_fct) %>% adorn_totals("row") %>% adorn_percentages() %>% adorn_pct_formatting() %>% adorn_ns() #> arm Lived Died #> A: IFL 4.2% (18) 95.8% (410) #> F: FOLFOX 14.3% (99) 85.7% (592) #> G: IROX 6.8% (26) 93.2% (354) #> Total 9.5% (143) 90.5% (1356) ``` ] --- class: middle .left-column[ <img src="https://raw.githubusercontent.com/rstudio/gt/master/man/figures/logo.svg" width="50%" /> ] .right-column[ # the gt package <https://gt.rstudio.com/> ] --- background-image: url(https://gt.rstudio.com/reference/figures/gt_parts_of_a_table.svg) background-size: contain --- background-image: url(https://gt.rstudio.com/reference/figures/gt_workflow_diagram.svg) background-size: contain --- .pull-left[ ```r mockdata %>% tabyl(arm, fu_fct) %>% gt::gt() ```
arm
Lived
Died
A: IFL
18
410
F: FOLFOX
99
592
G: IROX
26
354
] -- .pull-right[ ```r mockdata %>% tabyl(arm, fu_fct) %>% adorn_totals("row") %>% adorn_percentages() %>% adorn_pct_formatting() %>% adorn_ns() %>% gt::gt() ```
arm
Lived
Died
A: IFL
4.2% (18)
95.8% (410)
F: FOLFOX
14.3% (99)
85.7% (592)
G: IROX
6.8% (26)
93.2% (354)
Total
9.5% (143)
90.5% (1356)
] --- class: middle .left-column[ ![](http://www.danieldsjoberg.com/gtsummary/reference/figures/logo.png) ] .right-column[ # the gtsummary package <http://www.danieldsjoberg.com/gtsummary/index.html> ] --- class: middle # A `tbl_summary()` .left-column[ ```r library(gtsummary) mockdata %>% select(arm, sex) %>% tbl_summary(by = arm) %>% add_p() %>% add_overall() ``` ] .right-column[
Characteristic
Overall
, N = 1499
A: IFL
, N = 428
1
F: FOLFOX
, N = 691
1
G: IROX
, N = 380
1
p-value
2
sex
0.2
Female
583 (39%)
151 (35%)
280 (41%)
152 (40%)
Male
916 (61%)
277 (65%)
411 (59%)
228 (60%)
1
Statistics presented: n (%)
2
Statistical tests performed: chi-square test of independence
] --- class: middle # A bigger `tbl_summary()` .left-column[ ```r library(gtsummary) mockdata %>% select(arm, starts_with("ae")) %>% tbl_summary(by = arm) ``` ] .right-column[
Characteristic
A: IFL
, N = 428
1
F: FOLFOX
, N = 691
1
G: IROX
, N = 380
1
ae_low_wbc
0
354 (83%)
551 (80%)
347 (91%)
1
74 (17%)
140 (20%)
33 (8.7%)
ae_neuropathy
0
354 (83%)
580 (84%)
347 (91%)
1
74 (17%)
111 (16%)
33 (8.7%)
ae_diarrhea
0
339 (79%)
546 (79%)
333 (88%)
1
89 (21%)
145 (21%)
47 (12%)
ae_vomiting
0
339 (79%)
571 (83%)
333 (88%)
1
89 (21%)
120 (17%)
47 (12%)
ae_blood_clot
0
411 (96%)
658 (95%)
365 (96%)
1
17 (4.0%)
33 (4.8%)
15 (3.9%)
1
Statistics presented: n (%)
] --- class: middle, center # Take-aways -- ✔️ **Document your document:** use YAML to set up meaningful metadata -- ✔️ **Style your document:** use YAML to add options to your chosen output format -- ✔️ **Organize your text:** use markdown headers with `#` -- ✔️ **Organize your code:** use `knitr` chunk labels -- ✔️ **Style your text:** use markdown **bold**, _italics_, <i class="fas fa-list-ul"></i> bullets, and <i class="fas fa-list-ol"></i> lists -- ✔️ **Style your output:** use `knitr` chunk options -- 🧶 early, 🧶 often --- class: top # How does R Markdown work? 1. 🤷 1. I press knit, a document appears, and I believe that anything happening in between could be actual magic. 1. `knitr` executes the code and converts `.Rmd` to `.md`; **Pandoc** renders the `.md` file to the output format you want. --- class: top # How *does* R Markdown work? 1. 🤷 (✔️) 1. ["I press knit, a document appears, and I believe that anything happening in between could be actual magic."](https://twitter.com/allison_horst/status/1070323369600442368?s=20) (🧙♂️) 1. `knitr` executes the code and converts `.Rmd` to `.md`; **Pandoc** renders the `.md` file to the output format you want. (✔️) --- class: middle, center <img src="https://raw.githubusercontent.com/rstudio/hex-stickers/master/PNG/rmarkdown.png" width="25%" style="display: block; margin: auto;" /> > "In a nutshell, R Markdown stands on the shoulders of `knitr` and **Pandoc**. -- > The former executes the computer code embedded in Markdown, and converts R Markdown to Markdown. -- > The latter renders Markdown to the output format you want (such as PDF, HTML, Word, and so on)." -- > — [R Markdown: The Definitive Guide](https://bookdown.org/yihui/rmarkdown/) --- # Check-in Which of the following is **NOT** a part of R Markdown basic anatomy? 1. Metadata 1. Text 1. Data 1. Code 1. Output --- # Mad libs check-in ```markdown ___________ is written at the top between `---` in ________. noun syntax ______ is written in Markdown. noun ______ is written in R (or other programming language). noun The ______ package evaluates the code and produces the ______. noun noun ``` --- # Mad libs check-in ```markdown Metadata is written at the top between `---` in YAML. (noun) (syntax) Text is written in Markdown. (noun) Code is written in R (or other programming language). (noun) The knitr package evaluates the code and produces the output. (noun) (noun) ``` --- # Projects --- class: center, middle # R projects <img src="images/rproj.png" width="171" /> --- class: middle, center .pull-left[ # <i class="fas fa-mouse-pointer"></i> _File > New Project..._ <img src="images/rstudio-new.png" width="2056" /> ] -- .pull-right[ # <i class="fas fa-mouse-pointer"></i> Pick one <img src="images/rstudio-newproj.png" width="1773" /> ] --- class: middle, center .pull-left[ # <i class="fas fa-mouse-pointer"></i> _File > New File > R Markdown..._ <img src="images/rstudio-newfile.png" width="2037" /> ] -- .pull-right[ # <i class="fas fa-mouse-pointer"></i> Fill in, can leave as HTML <img src="images/rstudio-newrmd.png" width="1768" /> ] --- class: middle, center .pull-left[ # <i class="fas fa-trash-alt"></i> ![](images/rstudio-freshrmd.png) ] -- .pull-right[ # <i class="fas fa-save"></i> ![](images/rstudio-blankrmd.png) ] --- class: middle # Working with data .pull-left[ ### <i class="far fa-thumbs-down"></i> ```r "~/mm/nope.csv" setwd("/haha/good/luck/") setwd('c:\iamyournightmare\!') setwd("C:\user\oh\no") setwd("path/that/only/works/on/my/machine") ``` ### <i class="far fa-thumbs-up"></i> ```r mockdata <- read_csv( here::here("data/mockdata.csv") ) ``` ] .pull-right[ <div class="figure" style="text-align: center"> <img src="images/horst_here.png" alt="Art by Allison Horst" width="90%" /> <p class="caption">Art by Allison Horst</p> </div> ] https://whattheyforgot.org/project-oriented-workflow.html#setwd --- # Extension formats --- class: middle, center # Dashboards <a href="https://jenthompson.me/examples/progressdash.html" target="_blank"><img src="https://jenthompson.me/images/flexdash_example.png" width="50%" /></a> https://jenthompson.me/examples/progressdash.html https://jenthompson.me/2018/02/09/flexdashboards-monitoring/ --- class: middle, center <https://bookdown.org/yihui/bookdown/> <iframe src="https://bookdown.org/yihui/bookdown/" height="600" width="1200" frameborder="0"></iframe> --- class: center, middle <https://bookdown.org/yihui/blogdown/> <iframe src="https://academic-demo.netlify.com/" height="600" width="1200" frameborder="0"></iframe> --- class: middle, center, inverse <span class="fa-stack fa-4x"> <i class="fa fa-circle fa-stack-2x" style="color: #fff;"></i> <strong class="fa-stack-1x" style="color:#2f5275;">!</strong> </span> # Cheatsheets! --- class: middle .left-column[ # 📌 ] .right-column[ _Help > Cheatsheets > RStudio IDE_ <a href="https://www.rstudio.com/resources/cheatsheets/#ide" target="_blank"><img src="https://d33wubrfki0l68.cloudfront.net/0dc0efccfaf638de8be4ce43e2bab8a91541d3d2/c4fdc/wp-content/uploads/2018/08/rstudio-ide.png" width="50%" /></a> ] .footnote[https://www.rstudio.com/resources/cheatsheets/#ide] --- class: middle .left-column[ # 📌 ] .right-column[ _Help > Cheatsheets > R Markdown Cheat Sheet_ <a href="https://www.rstudio.com/resources/cheatsheets/#rmarkdown" target="_blank"><img src="https://d33wubrfki0l68.cloudfront.net/65dffd1bdcaa0025006262164d98e8068e8b4387/c3895/wp-content/uploads/2018/08/rmarkdown-2.0.png" width="50%" /></a> ] .footnote[https://www.rstudio.com/resources/cheatsheets/#rmarkdown] --- class: middle .left-column[ # 📌 ] .right-column[ _Help > Cheatsheets > R Markdown Reference Guide_ <a href="https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf" target="_blank"><img src="https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference-guide.png" width="40%" /></a> ] .footnote[https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf] --- class: middle, center, inverse <span class="fa-stack fa-4x"> <i class="fa fa-circle fa-stack-2x" style="color: #fff;"></i> <strong class="fa-stack-1x" style="color:#2f5275;">!</strong> </span> # Book! --- class: middle .left-column[ # 📖 ] .right-column[ <a href="https://bookdown.org/yihui/rmarkdown/" target="_blank"><img src="https://bookdown.org/yihui/rmarkdown/images/cover.png" width="40%" /></a> ] .footnote[https://bookdown.org/yihui/rmarkdown/] --- class: middle, center, inverse <span class="fa-stack fa-4x"> <i class="fa fa-circle fa-stack-2x" style="color: #fff;"></i> <strong class="fa-stack-1x" style="color:#2f5275;">!</strong> </span> # Websites! --- class: middle .left-column[ # 🔗 ] .right-column[ <a href="https://rmarkdown.rstudio.com/" target="_blank"><img src="images/rmarkdown-site.png" width="80%" /></a> ] .footnote[https://rmarkdown.rstudio.com/] --- class: middle .left-column[ # 🔗 ] .right-column[ <a href="https://rmarkdown.rstudio.com/docs/" target="_blank"><img src="images/rmarkdowndocs-site.png" width="80%" /></a> ] .footnote[https://rmarkdown.rstudio.com/docs/] --- class: middle .left-column[ # 🎯 ] .right-column[ <a href="https://community.rstudio.com/c/R-Markdown" target="_blank"><img src="images/rmarkdowncommunity-site.png" width="80%" /></a> ] .footnote[https://community.rstudio.com/c/R-Markdown] --- class: middle .left-column[ # 🤖 ] .right-column[ <a href="https://rmarkdown.rstudio.com/lesson-6.html" target="_blank"><img src="https://raw.githubusercontent.com/rstudio/rmarkdown/gh-pages/lesson-images/params-1-hawaii.png" width="80%" /></a> ] .footnote[https://bookdown.org/yihui/rmarkdown/parameterized-reports.html,<br> https://rmarkdown.rstudio.com/lesson-6.html] --- class: inverse, middle, center # Thank you!