class: title-slide, left, middle background-image: url("images/renoir.jpg") background-position: right background-size: contain background-color: #0071B8 .pull-left[ # Authoring ## R / Medicine 2021 ### Alison Hill · RStudio #### [r3-rmedicine.netlify.app](https://r3-rmedicine.netlify.app) ] --- class: middle, center # <i class="fas fa-cloud"></i> # Go here and log in (free): https://rstd.io/r3-cloud --- layout: true <div class="my-footer"><span>https://rstd.io/r3-cloud</span></div> --- # Your turn **WARM-UP**: open `02-draft.qmd` and read the source file. It has several figures coded using the `ggplot2` package. Look at the source and answer the following questions: 1. What is the output **format**? 1. Are there any output **options**? 1. Are any `knitr` **execution** options set? 1. Anything in the code look foreign to you? Finally, render this file. 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> -- # Figures --- # Including plots .pull-left[ Will this print? ```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() ) ``` ] -- .pull-right[ <img src="02-authoring_files/figure-html/unnamed-chunk-4-1.png" width="504" /> ] --- # Including plots .pull-left[ What about this? ```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() ) ``` ] -- .pull-right[ ] --- # Including plots .pull-left[ What about this? ```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="02-authoring_files/figure-html/unnamed-chunk-8-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.width`, `fig.height`, `fig.asp`) - [fig "device"](https://r4ds.had.co.nz/graphics-for-communication.html#other-important-options) See also: https://r4ds.had.co.nz/graphics-for-communication.html#figure-sizing .footnote[https://yihui.name/knitr/options/#plots] --- # out.width .pull-left[ ```` ```{r} #| out.width="70%" age_histogram ``` ```` <img src="02-authoring_files/figure-html/unnamed-chunk-9-1.png" width="70%" /> ] -- .pull-right[ ```` ```{r} #| out.width="10%" age_histogram ``` ```` <img src="02-authoring_files/figure-html/unnamed-chunk-10-1.png" width="10%" /> ] --- # fig-cap .pull-left[ ```` ```{r fig-age} #| echo: FALSE #| fig-cap: 'Age distributions' age_histogram ``` ```` ] .pull-right[ <img src="02-authoring_files/figure-html/fig-age-1.png" width="50%" /> **Fig. 1:** Age distributions ] --- # Chunk labels .pull-left[ ```` ```{r} #| label: peek #| echo: FALSE #| results: 'hide' glimpse(mockdata) ``` ```` Place either: + On a separate line after `#| label:` + In chunk header after `r` ] .pull-right[ + 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: middle, center # Chunks as house plants or crops? .footnote[https://masalmon.eu/2017/08/08/chunkpets/] --- class: middle, center # A good chunk label .pull-left[ ### Good `myplot` `my-plot` `myPlot` `myplot1` ] .pull-right[ ### Bad `my_plot` `my plot` everything else! ] --- background-color: #fff class: middle, center # Think: kebabs, not snakes .pull-left[  ] .pull-right[  ] --- # Cross-reference a figure .pull-left[ See Figure `@fig-age`. ```` ```{r fig-age} #| echo: FALSE #| fig-cap: 'Age distributions' age_histogram ``` ```` ] .pull-right[ See Figure [1](). .center[ <div class="figure"> <img src="02-authoring_files/figure-html/unnamed-chunk-11-1.png" alt="Age distributions" width="70%" /> <p class="caption">Age distributions</p> </div> ] ] --- class: middle, center # Key takeaway Once you have: ✔️ A labeled chunk that produces a plot ✔️ AND the plot label starts with `fig-` ✔️ AND a figure caption using `fig-cap` (**not `fig.cap`**) Then you can have: ### Cross-references! In any format. --- class: inverse, middle # Live code demo We open up `02-draft.Rmd` and take the static image and together we: 1. Label that chunk like `fig-bmj` 1. Add a cross-reference like `see Figure @fig-bmj` 1. Try again with `counts-by-site` code chunk: `see Figure @fig-counts-by-site` _(watch what happens if try to cross-reference without a caption, or use a chunk label that does not start with `fig-`!)_ --- 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> -- # Layouts --- class: middle, center # The line-up <img src="02-authoring_files/figure-html/unnamed-chunk-12-1.png" width="25%" /><img src="02-authoring_files/figure-html/unnamed-chunk-12-2.png" width="25%" /><img src="02-authoring_files/figure-html/unnamed-chunk-12-3.png" width="25%" /><img src="02-authoring_files/figure-html/unnamed-chunk-12-4.png" width="25%" /> --- class: inverse, middle # Live code demo We take these plots and use: 1. Tabsets `::: {.panel-tabset}` 1. Panels with `#| layout-ncol: 2` --- 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> -- # Tables --- class: center, middle # Two table types .pull-left[ ### for your eyes mainly <!-- --> ] -- .pull-right[ ### for other people's eyes too <!-- --> ] --- .pull-left[  ] .pull-right[  ] .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;">4 </strong> </span> -- # EDA Tables --- # Let's try using data now ```r mockdata <- read_csv(here::here("static/slides/data/mockdata.csv")) %>% mutate(fu_fct = fct_recode(as.factor(fu_stat), "Lived" = "1", "Died" = "2")) %>% mutate_at(vars(starts_with("ae_")), ~as.factor(.)) #> #> ── 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() #> ) #> ℹ Use `spec()` for the full column specifications. ``` --- # EDA tables: `janitor` Good for 1- and 2-way frequency tables with categorical variables Count number of participants by `arm`... .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% ``` ] --- Add by status (`fu_fct`) too. .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 .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 --- class: middle .pull-left[ ```r mockdata %>% count(arm) %>% gt::gt() ```
arm
n
A: IFL
428
F: FOLFOX
691
G: IROX
380
] -- .pull-right[ ```r mockdata %>% count(arm, sex) %>% pivot_wider(names_from = sex, values_from = n) %>% gt::gt() ```
arm
Female
Male
A: IFL
151
277
F: FOLFOX
280
411
G: IROX
152
228
] --- class: middle .left-column[  ] .right-column[ # the gtsummary package <http://www.danieldsjoberg.com/gtsummary/index.html> ] --- class: middle # Simple frequency table ```r library(gtsummary) mockdata %>% select(arm) %>% tbl_summary() ```
Characteristic
N = 1,499
1
arm
A: IFL
428 (29%)
F: FOLFOX
691 (46%)
G: IROX
380 (25%)
1
n (%)
--- ## Cross-tables .pull-left[ ```r mockdata %>% select(fu_fct, arm) %>% tbl_summary(by = fu_fct) ```
Characteristic
Lived
, N = 143
1
Died
, N = 1,356
1
arm
A: IFL
18 (13%)
410 (30%)
F: FOLFOX
99 (69%)
592 (44%)
G: IROX
26 (18%)
354 (26%)
1
n (%)
] -- .pull-right[ ```r mockdata %>% select(fu_fct, arm) %>% tbl_cross( row = arm, col = fu_fct, percent = "cell", label = fu_fct ~ "Follow-up") ```
Characteristic
Follow-up
Total
Lived
Died
arm
A: IFL
18 (1.2%)
410 (27%)
428 (29%)
F: FOLFOX
99 (6.6%)
592 (39%)
691 (46%)
G: IROX
26 (1.7%)
354 (24%)
380 (25%)
Total
143 (9.5%)
1,356 (90%)
1,499 (100%)
] --- class: middle ## A `tbl_summary()` .left-column[ ```r mockdata %>% select(arm, sex) %>% tbl_summary(by = arm) %>% add_p() %>% add_overall() ``` ] .right-column[
Characteristic
Overall
, N = 1,499
1
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
n (%)
2
Pearson's Chi-squared test
] --- class: middle # A bigger `tbl_summary()` .left-column[ ```r 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
n (%)
] --- # Your turn: Part 2 of 2 Now go through `02-draft-revised.Rmd` and read the source, then knit. .pull-left[ ```r # demographics----------------------------- age_density age_boxplot demo_tab # treatment response @ follow-up----------- surv_pct_plot surv_days_plot fu_tab # adverse events--------------------------- ae_pct_plot ae_tab ``` ] .pull-right[ 1. Add new code chunks containing tables and plots where you want. 1. Edit the code chunk options as you need to. 1. Add headers and other Markdown formatting as you see fit. 1. Feel free to add figure captions and in text cross-references. ]
05
:
00
--- class: middle, center # ⏱ # Time for a break!
10
:
00