class: title-slide, center, middle <span class="fa-stack fa-4x"> <i class="fa fa-circle fa-stack-2x" style="color: #ffffffcc;"></i> <strong class="fa-stack-1x" style="color:#3b4245;">02</strong> </span> # Outputs & Tables ## R Markdown for Medicine ### Alison Hill · RStudio #### [rmd4medicine.netlify.com/](https://rmd4medicine.netlify.com/) --- 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/rmd4medicine-cloud</span></div> --- .left-column[ <span class="fa-stack fa-4x"> <i class="fa fa-circle fa-stack-2x" style="color: #2f5275;"></i> <strong class="fa-stack-1x" style="color: #fff;">?</strong> </span> ] .right-column[ # 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 .left-column[ <span class="fa-stack fa-4x"> <i class="fa fa-circle fa-stack-2x" style="color: #2f5275;"></i> <strong class="fa-stack-1x" style="color: #fff;">?</strong> </span> ] .right-column[ # 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/) --- class: center, middle # Foreshadowing <img src="images/outputs.png" width="40%" /> ??? but first a pop quiz... --- class: pop-quiz # Pop quiz Which of the following is **NOT** a part of R Markdown basic anatomy? 1. Metadata 1. Text 1. CSS 1. Code 1. Output --- class: your-turn # Your turn ## Mad libs style — with a neighbor ```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 ``` _psst: answers on next slide..._
03
:
00
--- class: your-turn # Your turn ## Answers ```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) ``` --- class: your-turn # Your turn Find a partner and look at `02-progress.Rmd` together. Look at the source, then ๐งถ **Knit**. Answer the following questions: 1. What is the output **format**? 1. Are there any output **options**? 1. Are any `knitr` **global** chunk options set? 1. Anything in the code look foreign to you? 1. Anything in the output (or not in the output) surprise you?
05
:
00
--- 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;">1 </strong> </span> -- # Output formats --- 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 <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 --- class: your-turn # Your turn ## Use a different built-in format .pull-left[ Use the drop-down menu for the ๐งถ **Knit** button and try: 1. Knit to Word 1. Knit to PDF ] .pull-right[ <img src="images/knit-dropdown.png" width="60%" style="display: block; margin: auto;" /> ]
02
:
00
--- # Temp check ๐ก Does your current YAML look like this? ```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: your-turn # Your turn ## Add output options with your YAML .pull-left[ You just used the drop-down ๐งถ **Knit** button to knit to Word and PDF. 1. Change your YAML to the right... 1. Be __very careful__ about indentation and `:` here!! 1. ๐งถ **Knit** to Word again- is there a TOC? ] .pull-right[ ```yaml --- author: Your name here title: Your title here output: html_document: toc: true toc_float: true theme: flatly word_document: toc: true pdf_document: default --- ``` ]
01
:
00
--- # What is hard about YAML? .pull-left[ ![](https://media.giphy.com/media/Nt2cPxzpKhbCE/giphy.gif) ] .pull-right[ + Indentations matter + Rarely informative error messages + Knowing which output options are relevant for different formats (i.e., Word, PDF, HTML, etc.) + Easy to forget the right `key` + Easy to forget the possible `values` for a given `key` ] --- class: your-turn # Your turn ## Use an extension package 1. Edit your YAML again! Change the `output:` key to only `distill::distill_article` 1. Save the document; click on the dropdown ๐งถ **Knit** button- what do you notice? 1. ๐งถ **Knit** to distill_article. **If this was easy**, add a table of contents too. _psst...answers on the next slide..._
02
:
00
.footnote[(note: this requires you to install the [`distill` package](https://rstudio.github.io/distill/) on your own computer)] ??? At this point, I'm going to stick with the distill_article output format for the rest of this session. --- class: your-turn # Your turn ## Answer .pull-left[ ```yaml --- author: Your name here title: Your title here output: distill::distill_article --- ``` ] .pull-right[ ```yaml --- author: Your name here title: Your title here output: distill::distill_article: toc: true --- ``` ] --- class: middle # Pop quiz .pull-left[ How do you add a single output **format** to your YAML? ```yaml --- output: html_document: default --- output: html_document --- ``` ] -- .pull-right[ How do you add an output **option** to a format in your YAML? ```yaml --- output: html_document: toc: true --- output: html_document(toc=true) --- ``` ] --- class: middle # Pop quiz .pull-left[ How do you add another output **format** to your YAML? ```yaml --- output: html_document: default word_document: default --- output: html_document() word_document() --- ``` ] -- .pull-right[ How do you now add output **options** to your YAML? ```yaml --- output: html_document: toc: true word_document: default --- output: html_document(toc=true) word_document(default) --- ``` ] --- class: middle, center, inverse # Tables ![](https://media.giphy.com/media/i9c6lFmOSXKZW/giphy.gif) --- 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;">2 </strong> </span> -- # Custom typed 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: your-turn # Your turn ## Type up a Markdown table Find this section: ```markdown <--TODO: add markdown table of treatment arms here--> ``` .pull-left[ Using pipes `|`, make this table: | Arm | Drug | |:----|:-------| | A | IFL | | F | FOLFOX | | G | IROX | ] .pull-right[ ![](https://qph.fs.quoracdn.net/main-qimg-637b91293b153c5cd40419c0872c2186) ] _psst...answers on the next slide..._
03
:
00
--- class: your-turn # Your turn ## Answers .pull-left[ ```markdown | Arm | Drug | |:----|:-------| | A | IFL | | F | FOLFOX | | G | IROX | ``` ] .pull-right[ ```markdown |Arm|Drug| |:---|:---| |A|IFL| |F|FOLFOX| |G|IROX| ``` ] Spacing actually doesn't matter - it mainly helps your eyeballs. --- class: center, middle # Two table types .pull-left[ ### for your eyes mainly <img src="images/tables-you.jpg" width="3691" /> ] -- .pull-right[ ### for other people's eyes too <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)] --- 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;">3 </strong> </span> -- # EDA Tables --- # Let's try using data now ```r mockdata <- read_csv(here::here("static/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. ``` --- # EDA tables: `skimr` ```r library(skimr) mockdata %>% select(age, fu_fct) %>% skimr::skim() #> Skim summary statistics #> n obs: 1499 #> n variables: 2 #> #> โโ Variable type:factor โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ #> variable missing complete n n_unique top_counts #> fu_fct 0 1499 1499 2 Die: 1356, Liv: 143, NA: 0 #> ordered #> FALSE #> #> โโ Variable type:numeric โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ #> variable missing complete n mean sd p0 p25 p50 p75 p100 hist #> age 0 1499 1499 59.99 11.52 19 52 61 68 88 โโโโ โโโ โ ``` --- ```` ```{r results='asis'} mockdata %>% select(age, fu_fct) %>% skimr::skim() %>% kable() ``` ```` Skim summary statistics n obs: 1499 n variables: 2 Variable type: factor | variable | missing | complete | n | n_unique | top_counts | ordered | |----------|---------|----------|------|----------|----------------------------|---------| | fu_fct | 0 | 1499 | 1499 | 2 | Die: 1356, Liv: 143, NA: 0 | FALSE | Variable type: numeric | variable | missing | complete | n | mean | sd | p0 | p25 | p50 | p75 | p100 | hist | |----------|---------|----------|------|-------|-------|----|-----|-----|-----|------|----------| | age | 0 | 1499 | 1499 | 59.99 | 11.52 | 19 | 52 | 61 | 68 | 88 | โโโโ โโโ โ | --- # EDA tables: `janitor` Good for 1- and 2-way frequency tables with categorical variables Count number of participants by `arm`, then add by status (`fu_fct`) too .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% ``` ] --- .pull-left[ ```` ```{r results='asis'} mockdata %>% tabyl(arm, fu_fct) %>% knitr::kable() ``` ```` |arm | Lived| Died| |:---------|-----:|----:| |A: IFL | 18| 410| |F: FOLFOX | 99| 592| |G: IROX | 26| 354| ] -- .pull-right[ ```` ```{r results='asis'} mockdata %>% tabyl(arm, fu_fct) %>% adorn_totals("row") %>% adorn_percentages("row") %>% adorn_pct_formatting(digits = 2) %>% adorn_ns() %>% knitr::kable() ``` ```` |arm |Lived |Died | |:---------|:------------|:-------------| |A: IFL |4.21% (18) |95.79% (410) | |F: FOLFOX |14.33% (99) |85.67% (592) | |G: IROX |6.84% (26) |93.16% (354) | |Total |9.54% (143) |90.46% (1356) | ] --- class: your-turn # Your turn ## Fix this `tabyl` Find this section: ```markdown <--TODO: make this print better with knitr::kable + code chunks--> ``` Make this `tabyl` print nicely when knitted. ```r mockdata %>% tabyl(arm) %>% adorn_totals("row") %>% adorn_pct_formatting() ``` _psst: answers on next slide..._
03
:
00
--- class: your-turn # Answer ```` ```{r results='asis'} mockdata %>% tabyl(arm) %>% adorn_totals("row") %>% adorn_pct_formatting() %>% knitr::kable() ``` ```` |arm | n|percent | |:---------|----:|:-------| |A: IFL | 428|28.6% | |F: FOLFOX | 691|46.1% | |G: IROX | 380|25.4% | |Total | 1499|100.0% | --- 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;">4 </strong> </span> -- # Table One --- # Table One from `arsenal` ```r tableby(arm ~ sex, data = mockdata) #> tableby Object #> #> Function Call: #> tableby(formula = arm ~ sex, data = mockdata) #> #> Variable(s): #> arm ~ sex ``` -- ```r tableby(arm ~ sex, data = mockdata) %>% summary() #> #> #> | | A: IFL (N=428) | F: FOLFOX (N=691) | G: IROX (N=380) | Total (N=1499) | p value| #> |:------------------------|:--------------:|:-----------------:|:---------------:|:--------------:|-------:| #> |**sex** | | | | | 0.190| #> | Female | 151 (35.3%) | 280 (40.5%) | 152 (40.0%) | 583 (38.9%) | | #> | Male | 277 (64.7%) | 411 (59.5%) | 228 (60.0%) | 916 (61.1%) | | ``` --- # One more step! For R Markdown pretty printing... ```` ```{r results='asis'} tableby(arm ~ sex, data = mockdata) %>% summary() ``` ```` | | A: IFL (N=428) | F: FOLFOX (N=691) | G: IROX (N=380) | Total (N=1499) | p value| |:------------------------|:--------------:|:-----------------:|:---------------:|:--------------:|-------:| |**sex** | | | | | 0.190| | Female | 151 (35.3%) | 280 (40.5%) | 152 (40.0%) | 583 (38.9%) | | | Male | 277 (64.7%) | 411 (59.5%) | 228 (60.0%) | 916 (61.1%) | | ??? This is a new knitr chunk option- the results option. --- class: your-turn # Your turn ## Make a table one Find this section: ```markdown <--TODO: add age and bmi here--> ``` 1. Add the variables `age` and `bmi` to the table. (__hint:__ use the formula `arm ~ sex + var1 + var2`) 1. Look at the next code chunk. It uses a different package, `tableone::CreateTableOne`. You only need one "table one", so pick which one you using your code chunks (don't delete!). 1. Add/subtract variables from your table one as you like. (Note: leave this `layout="l-body-outset"` part alone- I'll explain!)
03
:
00
--- 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;">5 </strong> </span> -- # Table Two --- class: your-turn # Your turn ## Table two Find this section: ``` <--TODO: add adverse events table--> ``` 1. Find the code chunk started for you. 1. Code and print a table called `ae_tab` that summarizes 2+ variables that start with `ae_`. __If this was easy__, edit the `summary()` function to modify the number of [digits](https://cran.r-project.org/web/packages/arsenal/vignettes/tableby.html#modify-the-number-of-digits-used). Or add [labels](https://cran.r-project.org/web/packages/arsenal/vignettes/tableby.html#add-labels). _psst...answers on next slide..._
05
:
00
--- class: your-turn # Answer ```` ```{r results='asis', layout="l-body-outset"} ae_tab <- mockdata %>% tableby( arm ~ ae_blood_clot + ae_vomiting + ae_diarrhea + ae_neuropathy + ae_low_wbc, data = .) %>% summary(digits = 2, digits.p = 3, digits.pct = 1) ae_tab ``` ```` --- | | A: IFL (N=428) | F: FOLFOX (N=691) | G: IROX (N=380) | Total (N=1499) | p value| |:-------------------|:--------------:|:-----------------:|:---------------:|:--------------:|-------:| |**ae_blood_clot** | | | | | 0.742| | 0 | 411 (96.0%) | 658 (95.2%) | 365 (96.1%) | 1434 (95.7%) | | | 1 | 17 (4.0%) | 33 (4.8%) | 15 (3.9%) | 65 (4.3%) | | |**ae_vomiting** | | | | | 0.006| | 0 | 339 (79.2%) | 571 (82.6%) | 333 (87.6%) | 1243 (82.9%) | | | 1 | 89 (20.8%) | 120 (17.4%) | 47 (12.4%) | 256 (17.1%) | | |**ae_diarrhea** | | | | | 0.001| | 0 | 339 (79.2%) | 546 (79.0%) | 333 (87.6%) | 1218 (81.3%) | | | 1 | 89 (20.8%) | 145 (21.0%) | 47 (12.4%) | 281 (18.7%) | | |**ae_neuropathy** | | | | | < 0.001| | 0 | 354 (82.7%) | 580 (83.9%) | 347 (91.3%) | 1281 (85.5%) | | | 1 | 74 (17.3%) | 111 (16.1%) | 33 (8.7%) | 218 (14.5%) | | |**ae_low_wbc** | | | | | < 0.001| | 0 | 354 (82.7%) | 551 (79.7%) | 347 (91.3%) | 1252 (83.5%) | | | 1 | 74 (17.3%) | 140 (20.3%) | 33 (8.7%) | 247 (16.5%) | | --- class: middle, center # โฑ # Time for a break!
15
:
00