-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.rmd
2047 lines (1333 loc) · 43.6 KB
/
index.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "RMarkdown"
subtitle: "reproducible reporting"
author: "Tom Mock, PhD • RStudio"
date: "`r Sys.Date()`"
output:
xaringan::moon_reader:
css: ["default", "css/my-theme.css", "css/my-fonts.css"]
seal: false
lib_dir: libs
nature:
ratio: '16:9'
highlightStyle: dracula
highlightLines: false
countIncrementalSlides: false
beforeInit: "https://platform.twitter.com/widgets.js"
---
```{r, include=FALSE}
options(htmltools.dir.version = FALSE,
eval = TRUE, echo = TRUE)
library(tidyverse)
library(gt)
library(glue)
```
```{r metathis, echo=FALSE}
library(metathis)
meta() %>%
meta_name("github-repo" = "jthomasmock/rmd-nhs") %>%
meta_social(
title = "RMarkdown for reproducible reporting",
description =
"An introduction to the RMarkdown package for reproducible reporting in RStudio",
url = "https://jthomasmock.github.io/rmd-nhs/",
image = "https://images.unsplash.com/photo-1603204254626-d0de8eb24cf1?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1471&q=80",
og_type = "website",
og_author = "Tom Mock",
twitter_card_type = "summary_large_image",
twitter_creator = "@thomas_mock",
twitter_site = "@thomas_mock"
)
```
class: title-slide, left, top
# `r rmarkdown::metadata$title`:
## `r rmarkdown::metadata$subtitle`
### `r rmarkdown::metadata$author`
### `r Sys.Date()`
`r fontawesome::fa("github", "white")` [github.com/jthomasmock/rmd-nhs/](https://github.com/jthomasmock/rmd-nhs/tree/master/workshop)
`r fontawesome::fa("link", "white")` [jthomasmock.github.io/rmd-nhs/index.html](https://jthomasmock.github.io/rmd-nhs/index.html)
<span style='color:white;'>Slides released under</span> [CC-BY 2.0](https://creativecommons.org/licenses/by/2.0/) `r fontawesome::fa("creative-commons", "white")``r fontawesome::fa("creative-commons-by", "white")` ]
<div style = "position: absolute;top: -50px;right: -200px;"><img src="images/letters.png" alt="A collection of wooden letters loosely arranged. The letters are used for stamps." width="800"></img></div>
<div style = "position: absolute;bottom: 5px;right:25px;font-size:8px;color:white;">Image Credit: Bruno Martins, unsplash.com/photos/OhJmwB4XWLE</div>
---
layout: true
<div class="my-footer"><span>jthomasmock.github.io/rmd-nhs/</span></div>
---
# Resources
### Workshop materials: [on Github](https://github.com/jthomasmock/rmd-nhs/tree/master/workshop)
### Slides: [jthomasmock.github.io/rmd-nhs/](https://jthomasmock.github.io/rmd-nhs/)
Some content adapted from superstar Dr. Alison Hill [CDC](https://www.apreshill.com/talk/2020-rmd4cdc/) & [R4Pharma](https://rmd4pharma.netlify.app/): CC-BY
---
### 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. Magic (🧙️)
---
background-image: url("https://raw.githubusercontent.com/allisonhorst/stats-illustrations/master/rstats-artwork/rmarkdown_wizards.png")
background-position: center
background-size: contain
---
### Why RMarkdown?
1. Reproducibility
--
2. Re-usability
--
3. Extensibility
--
4. "Lazy" ability
---
### Change your mental model
.pull-left[
#### Source <-> Output
```{r, echo=FALSE, out.width="50%"}
knitr::include_graphics("images/word.png")
```
]
--
.pull-right[
#### Source -> Output
```{r, echo=FALSE, out.width="50%"}
knitr::include_graphics("images/rmd-file.png")
```
]
---
### R Markdown
> R Markdown documents are fully reproducible. Use a productive notebook interface to weave together narrative text and code to produce elegantly formatted output. Use multiple languages including R, Python, and SQL.
--
No more copy-paste, no more manually rebuilding analyses from disparate components, no more dread when the data is updated and you need to run an analysis.
---
background-image: url("https://images.unsplash.com/4/madebyvadim.jpg?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1469&q=80")
background-position: center
background-size: fill
class: center, middle
--
<h1 style='color:black;background-color: rgba(255,255,255,0.75);padding:5px;line-height:2em;'>Ease of Use VS Full Details</h1>
---
### Pouring milk
A straight-forward task - pour the boxed milk into the glass.
--
.pull-left[
> Easy to use
![Person pouring milk with quite a bit of spilling](https://j.gifs.com/WPwJ1W.gif)
]
--
.pull-right[
> Understanding the Full details
![Person pouring box of milk with the cap facing up, resulting in no spilling.](https://j.gifs.com/z6BO57.gif)
]
---
```{r, echo = FALSE}
table_data <- tibble::tribble(
~section, ~name, ~desc, ~url,
"Documents", "Notebook", "Interactive R Notebooks", "https://bookdown.org/yihui/rmarkdown/notebook.html",
"Documents", "HTML", "HTML document w/ Bootstrap CSS", "https://bookdown.org/yihui/rmarkdown/html-document.html",
"Documents", "PDF", "PDF document (via LaTeX template)", "https://bookdown.org/yihui/rmarkdown/pdf-document.html",
"Documents", "Word", "Microsoft Word document (docx)", "https://bookdown.org/yihui/rmarkdown/word-document.html",
"Documents", "ODT", "OpenDocument Text document", "https://bookdown.org/yihui/rmarkdown/opendocument-text-document.html",
"Documents", "RTF", "Rich Text Format document", "https://bookdown.org/yihui/rmarkdown/rich-text-format-document.html",
"Documents", "Markdown", "Markdown document (various flavors)", "https://bookdown.org/yihui/rmarkdown/markdown-document.html",
"Presentations", "ioslides", "HTML presentation with ioslides", "https://bookdown.org/yihui/rmarkdown/ioslides-presentation.html",
"Presentations", "reveal.js", "HTML presentation with reveal.js", "https://bookdown.org/yihui/rmarkdown/revealjs.html",
"Presentations", "Slidy", "HTML presentation with W3C Slidy", "https://bookdown.org/yihui/rmarkdown/slidy-presentation.html",
"Presentations", "Beamer", "PDF presentation with LaTeX Beamer", "https://bookdown.org/yihui/rmarkdown/beamer-presentation.html",
"Presentations", "PowerPoint", "PowerPoint presentation", "https://bookdown.org/yihui/rmarkdown/powerpoint-presentation.html",
"Journals", "distill", "Scientific and technical writing for the web", "https://rstudio.github.io/distill/",
"Journals", "jss_article", "Journal of Statistical Software (JSS)", "https://github.com/rstudio/rticles",
"Journals", "acm_article", "Association for Computing Machinery (ACM)", "https://github.com/rstudio/rticles",
"Journals", "acs_article", "American Chemical Society (ACS) Journal", "https://github.com/rstudio/rticles",
"Journals", "ctex", "Documents based on the LaTeX package ctex", "https://github.com/rstudio/rticles",
"Journals", "elsevier_article", "Submissions to Elsevier journals", "https://github.com/rstudio/rticles",
"More", "flexdashboard", "Interactive dashboards", "flexdashboard/index.html",
"More", "bookdown", "HTML, PDF, ePub, and Kindle books", "https://bookdown.org",
"More", "Websites", "Multi-page websites", "https://bookdown.org/yihui/rmarkdown/rmarkdown-site.html",
"More", "blogdown", "Customizable websites and blogs", "https://bookdown.org/yihui/blogdown/",
"More", "pkgdown", "Package documentation websites", "https://pkgdown.r-lib.org/",
"More", "Tufte Handout", "Handouts in the style of Edward Tufte", "https://bookdown.org/yihui/rmarkdown/tufte-handouts.html",
"More", "Package Vignette", "R package vignette (HTML)", "https://bookdown.org/yihui/rmarkdown/r-package-vignette.html",
"More", "Github Document", "GitHub Flavored Markdown document.", "github_document_format.html"
)
paste_table <- function(sections = c("Documents", "Presentations")){
table_data %>%
filter(section %in% sections) %>%
gt(groupname_col = "section", rowname_col = "name") %>%
gtExtras::gt_theme_nytimes() %>%
text_transform(
locations = cells_stub(),
fn = function(x){
raw_url <- gtExtras::gt_index(., url)
map2(x,raw_url, function(xy, url){
gt::html(
glue::glue(
"<a href ='{url}' style='text-decoration:none;' target='_blank'>
{xy}</a>"
)
)
})
}
) %>%
cols_hide(url) %>%
cols_label(desc = "") %>%
opt_table_lines("none") %>%
tab_style(
locations = cells_row_groups(),
style = list(
cell_borders("bottom", "black", weight = px(3)),
cell_text(weight = "bold", color = "black"),
cell_fill(color = "lightgrey")
)
) %>%
tab_style(
locations = cells_stub(),
style = cell_text(font = google_font("Fira Mono"))
) %>%
opt_table_font(font = google_font("Open Sans"))
}
```
.pull-left[
### Many [RMarkdown Formats](https://rmarkdown.rstudio.com/formats.html)
```{r, echo = FALSE}
paste_table()
```
]
--
.pull-right[
```{r, echo = FALSE}
paste_table(c("Journals", "More"))
```
]
---
### Anatomy of RMarkdown
- Metadata (YAML)
```
---
output: html_document
---
```
--
- Code
````md
```{r basicExample, echo = TRUE}`r ''`
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarize(n = n(), mean = mean(mpg))
```
````
--
- Text
```
# Heading 1
This is a sentence with some **bold text**, some *italic text* and an [image](image.png).
```
---
### Metadata: YAML
The [YAML](https://yaml.org/) metadata or header is:
> processed in many stages of the rendering process and can influence the final document in many different ways. It is placed at the very beginning of the document and is read by each of Pandoc, rmarkdown, and knitr. Along the way, the information that it contains can affect the code, content, and the rendering process. - [RMarkdown Cookbook](https://bookdown.org/yihui/rmarkdown-cookbook/rmarkdown-anatomy.html)
--
A generic YAML follows a pattern like the below:
```
---
key: value
---
```
Valid YAML requires 3 dashes `---` on either end, creating the YAML block, with `key` + `value` pairs assigned throughout.
---
### Metadata: YAML
Specific metadata about the document and rendering options are included in this YAML header.
Note that if you want to find out what type of options are available, you can run `?rmarkdown::html_document()` or `?rmarkdown::pdf_document()`, etc, to show the specific rendering options for _that_ specific output format. There is also a [`rmarkdown` `pkgdown` site](https://rmarkdown.rstudio.com/docs/reference/html_document.html), so you can explore specific options there as well.
---
### Output options
```
---
output: html_document
---
```
```{r, echo=FALSE, fig.alt="Image of the letter R slightly tilted, along with the various RMarkdown outputs scattered around it. It includes HTML logo, PDF, a report, a dashboard, Word, Tufte, ePUB, kindle and LATEX", out.width = "30%"}
knitr::include_graphics("https://d33wubrfki0l68.cloudfront.net/aee91187a9c6811a802ddc524c3271302893a149/a7003/images/bandthree2.png")
```
---
### Specify output options in YAML
.pull-left[
```
---
title: My First RMarkdown Report
author: Tom Mock
output: html_document
---
```
]
---
### Specify output options in YAML
.pull-left[
```
---
title: My First RMarkdown Report
author: Tom Mock
output: html_document
---
```
```
---
title: My First RMarkdown Report
author: Tom Mock
output:
html_document:
toc: true
toc_float: true
theme: united
code_download: true
---
```
]
--
.pull-right[
```
---
title: My PDF RMarkdown Report
author: Tom Mock
output: pdf_document
---
```
]
---
### Specify output options in YAML
.pull-left[
```
---
title: My First RMarkdown Report
author: Tom Mock
output: html_document
---
```
```
---
title: My First RMarkdown Report
author: Tom Mock
output:
html_document:
toc: true
toc_float: true
theme: united
code_download: true
---
```
]
.pull-right[
```
---
title: My PDF RMarkdown Report
author: Tom Mock
output: pdf_document
---
```
```
---
title: "My Presentation"
subtitle: "made with xaringan"
author: "Yihui Xie"
date: "2021-10-30"
output:
xaringan
---
```
]
---
class: inverse, center, middle
# Text
---
### Text and Markdown text
* _Text_ is **human language**
* _Markdown_ is **markup language**
--
```
* _Text_ is **human language**
* _Markdown_ is **markup language**
```
--
You can read all about Markdown via the [Markdown guide](https://www.markdownguide.org/) or use the [`rmarkdown` cheat sheet](https://github.com/rstudio/cheatsheets/blob/master/rmarkdown-2.0.pdf)!
---
### Headings for splitting up your document
.pull-left[
```
# Header 1
## Header 2
### Header 3
#### Header 4
```
]
--
.pull-right[
# Header 1
## Header 2
### Header 3
#### Header 4
]
---
### Headings for splitting up your document
```{r, echo = FALSE, out.width = "65%"}
knitr::include_graphics("images/rstudio-headers.png")
```
---
### More than _just_ text
You can inline R code, so that you can programatically generate specific components of the document.
--
IE I can report that there are `r knitr::inline_expr("nrow(mtcars)")`
--
Which returns `r nrow(mtcars)`.
--
These inline texts can be as complex or simple as you like, and can also return R objects.
```{r}
car_rows <- glue::glue("There are {nrow(mtcars)} rows in the `mtcars` dataset.")
```
--
`r knitr::inline_expr("car_rows")`
--
`r car_rows`
---
.pull-left[
### More than _just_ text
```
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
]
---
### Images
.pull-left[
```
![RMarkdown hex logo](https://raw.githubusercontent.com/rstudio/rmarkdown/main/man/figures/logo.png)
![Alt text](path-to-local-file.png)
```
![RMarkdown hex logo](https://raw.githubusercontent.com/rstudio/rmarkdown/main/man/figures/logo.png)
]
--
.pull-right[
```{r, out.width="30%"}
knitr::include_graphics("https://user-images.githubusercontent.com/163582/45438104-ea200600-b67b-11e8-80fa-d9f2a99a03b0.png")
```
]
---
.pull-left[
### RStudio 1.4 and beyond
RStudio v1.4 (and future versions) include a visual markdown editing mode. Write code, text, and markdown. See the results in real time!
]
.pull-right[
```{r, echo = FALSE, out.width="100%", fig.alt="A screenshot showing the conversion of markdown into it's formatted output inside the RStudio IDE", fig.align="center"}
knitr::include_graphics("https://rstudio.github.io/visual-markdown-editing/images/visual-editing.png")
```
]
---
### Visual Editor
* The visual editor is more than _just_ the ability to see the output in real time.
* It also provides "word processor" like formatting of text, math, and other capabilities!
```{r, echo = FALSE, out.width="60%", fig.alt="A screenshot of a RMarkdown document inside the RStudio visual editor. It highlights the text processing abilities and shortcuts.", fig.align="center"}
knitr::include_graphics("https://rstudio.github.io/visual-markdown-editing/images/visual-editing-math.png")
```
---
class: inverse, middle, center
# Code chunks
---
### Code chunks
Specified sections of the document that are used to evaluate code and optionally return outputs.
.pull-left[
````md
```{r, echo = FALSE}`r ''`
mtcars %>%
distinct(cyl)
```
````
]
--
.pull-right[
```{r, echo = TRUE}
mtcars %>%
distinct(cyl)
```
]
---
### Anatomy of a code chunk
.pull-left[
````md
```{r car-stuff, echo = FALSE}`r ''`
mtcars %>%
distinct(cyl)
```
````
]
--
.pull-right[
- Has 3x backticks on each end ```` ``` ````
- Place engine (`r` and chunk label `label`) between curly braces `{r label}`
- Place options behind the label, after a comma: `{r label, option1=value}`
]
---
### Code chunks
Code in a chunk auto print, unless saved to an object, which needs to be explicitly returned.
```{r}
n_manufacturers <- n_distinct(gt::gtcars$mfr)
```
--
```{r}
n_manufacturers
```
--
```{r}
n_manufacturers <- n_distinct(gt::gtcars$mfr)
n_manufacturers
```
---
### Code chunks return more than just data
.pull-left[
```{r}
car_data <- gt::gtcars %>%
filter(!is.na(mpg_h)) %>%
group_by(mfr) %>%
summarise(
mean_mpg = mean(mpg_h),
n_models = n_distinct(model)
) %>%
filter(n_models >= 2) %>%
arrange(desc(n_models))
```
```{r plotEx, fig.dim = c(4,6), dpi = 150, eval = FALSE}
ggplot(car_data, aes(x = mean_mpg,
y = fct_reorder(mfr, mean_mpg))) +
geom_col()
```
]
--
.pull-right[
```{r plotEx, fig.dim = c(4,6), dpi = 150, eval = TRUE}
```
]
---
### Code chunks return more than just data
```{r}
car_data %>%
gt::gt()
```
---
### Code chunks are controllable
There are many chunk options you can control via `knitr` (the package that provides core features of RMarkdown).
Link to all the [`knitr` options](https://yihui.org/knitr/options/#code-chunk)
--
.pull-left[
````md
```{r, eval = FALSE}`r ''`
mtcars %>%
distinct(cyl)
```
````
]
--
.pull-right[
```{r, eval = FALSE}
# This won't return, because the
# chunk is not evaluated
# via eval = FALSE
mtcars %>%
distinct(cyl)
```
]
---
### Code chunks are controllable
Some examples
| Option | Description |
| ----------- | ----------- |
| `fig.dim = c(4,6)` | Plots generated from this chunk will have a width of 4 and height of 6. |
| `dpi = 150` | Plots generated will have a dots per inch (pixel density) of 150 |
| `echo = FALSE` | Code will not be echoed (ie not shown) |
| `eval = FALSE` | Nothing will be evaluated, but could still be printed |
| `cache = TRUE` | Results will be cached, and chunk will not be run in subsequent renders, unless code is changed. |
| `message = FALSE` | No messages will be printed |
| `warning = FALSE` | No warnings will be printed |
| `include = FALSE` | No ouputs/echo/messages/etc will be returned |
---
### Code chunks are controllable
````md
```{r, fig.dim = c(6,4), dpi=150}`r ''`
mtcars %>%
count(cyl) %>%
ggplot(aes(x = n, y = factor(cyl))) + geom_col()
```
````
--
New to `knitr` v1.35, there's now support for in-chunk option setting via special chunk option comments (ie `#|`). Very useful for longer options (like `fig.cap` or `fig.alt`), for setting many chunks at once, or for logical statements.
````md
```{r}`r ''`
#| fig.dim = c(6,4), dpi = 150,
#| fig.alt = "A simple barplot with the count of observations on the x-axis by the number of cylinders on the y-axis. There are 14 eight-cylinder cars, 7 six-cylinder cars, and 11 four-cylinder cars.\n
```
````
---
### Code chunks are controllable
.pull-left[
Default plot, too big and "fuzzy"
```{r}
mtcars %>%
count(cyl) %>%
ggplot(aes(x = n, y = factor(cyl))) + geom_col()
```
]
--
.pull-right[
opts set: `fig.dim = c(6,4)` and `dpi=150`
```{r, fig.dim = c(6,4), dpi=150}
mtcars %>%
count(cyl) %>%
ggplot(aes(x = n, y = factor(cyl))) + geom_col()
```
]
---
### Chunk options can be globally set
````md
```{r setup, include = FALSE}`r ''`
knitr::opts_chunk$set(
echo = FALSE, fig.width = 6, dpi = 150
)
```
````
--
This is typically most useful in avoiding repeating yourself throughout a document and making the different components consistent between chunks without _having_ to manually set these for **EACH** chunk.
---
### Chunk option defaults
.pull-left[
```{r}
str(knitr::opts_chunk$get()[1:27])
```
]
--
.pull-right[
```{r}
str(knitr::opts_chunk$get()[28:53])
```
]
---
### Chunks can be named
.pull-left[
* Useful for managing your longer documents (RStudio outline)
```{r, echo = FALSE, out.width="80%"}
#| fig.alt="A screenshot of a RMarkdown document inside RStudio. The screenshot highlights the automatically generated document outline for each chunk name."
knitr::include_graphics("images/chunk-outline.png")
```
]
--
.pull-right[
* Useful for troubleshooting (ie _where_ is the document failing on render)
```
label: unnamed-chunk-23
|..............................| 83%
ordinary text without R code
|..............................| 85%
label: unnamed-chunk-24 (with options)
List of 2
$ fig.dim: num [1:2] 6 4
$ dpi : num 150
|..............................| 86%
ordinary text without R code
```
]
---
### Named chunks can be re-used!
.pull-left[