-
Notifications
You must be signed in to change notification settings - Fork 0
/
reproducible-results-markdown-quarto-notebooks.html
1340 lines (1325 loc) · 94.8 KB
/
reproducible-results-markdown-quarto-notebooks.html
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>9 Reproducible Results (Markdown, Quarto & Notebooks) | A Comprehensive Guide to Use R</title>
<meta name="author" content="Africano BYAMUGISHA">
<meta name="description" content="Welcome to this chapter on reproducible results in R! In this chapter, we will explore the different tools and techniques that you can use to ensure that your data analysis workflows are not only...">
<meta name="generator" content="bookdown 0.33 with bs4_book()">
<meta property="og:title" content="9 Reproducible Results (Markdown, Quarto & Notebooks) | A Comprehensive Guide to Use R">
<meta property="og:type" content="book">
<meta property="og:url" content="https://github.io/africanobyamugisha/reproducible-results-markdown-quarto-notebooks.html">
<meta property="og:image" content="https://github.io/africanobyamugisha/images/flyer.png">
<meta property="og:description" content="Welcome to this chapter on reproducible results in R! In this chapter, we will explore the different tools and techniques that you can use to ensure that your data analysis workflows are not only...">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="9 Reproducible Results (Markdown, Quarto & Notebooks) | A Comprehensive Guide to Use R">
<meta name="twitter:description" content="Welcome to this chapter on reproducible results in R! In this chapter, we will explore the different tools and techniques that you can use to ensure that your data analysis workflows are not only...">
<meta name="twitter:image" content="https://github.io/africanobyamugisha/images/flyer.png">
<!-- JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet">
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script><script src="libs/bs3compat-0.4.2/transition.js"></script><script src="libs/bs3compat-0.4.2/tabs.js"></script><script src="libs/bs3compat-0.4.2/bs3compat.js"></script><link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet">
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script><script src="libs/htmlwidgets-1.6.1/htmlwidgets.js"></script><script src="libs/plotly-binding-4.10.1/plotly.js"></script><script src="libs/typedarray-0.1/typedarray.min.js"></script><link href="libs/crosstalk-1.2.0/css/crosstalk.min.css" rel="stylesheet">
<script src="libs/crosstalk-1.2.0/js/crosstalk.min.js"></script><link href="libs/plotly-htmlwidgets-css-2.11.1/plotly-htmlwidgets.css" rel="stylesheet">
<script src="libs/plotly-main-2.11.1/plotly-latest.min.js"></script><script src="libs/kePrint-0.0.1/kePrint.js"></script><link href="libs/lightable-0.0.1/lightable.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- CSS --><style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<link rel="stylesheet" href="bs4_style.css">
</head>
<body data-spy="scroll" data-target="#toc">
<div class="container-fluid">
<div class="row">
<header class="col-sm-12 col-lg-3 sidebar sidebar-book"><a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>
<div class="d-flex align-items-start justify-content-between">
<h1>
<a href="index.html" title="">A Comprehensive Guide to Use R</a>
</h1>
<button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
</div>
<div id="main-nav" class="collapse-lg">
<form role="search">
<input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>
<nav aria-label="Table of contents"><h2>Table of contents</h2>
<ul class="book-toc list-unstyled">
<li><a class="" href="index.html">*</a></li>
<li><a class="" href="about-the-author.html">About the Author</a></li>
<li><a class="" href="about-this-guide.html">About this guide</a></li>
<li><a class="" href="introduction.html"><span class="header-section-number">1</span> Introduction</a></li>
<li><a class="" href="getting-started-setting-up.html"><span class="header-section-number">2</span> Getting Started & Setting Up</a></li>
<li><a class="" href="operators-data-objects.html"><span class="header-section-number">3</span> Operators & Data Objects</a></li>
<li><a class="" href="working-with-data.html"><span class="header-section-number">4</span> Working With Data</a></li>
<li><a class="" href="data-wrangling.html"><span class="header-section-number">5</span> Data Wrangling</a></li>
<li><a class="" href="data-visualisation.html"><span class="header-section-number">6</span> Data Visualisation</a></li>
<li><a class="" href="statistics-in-r.html"><span class="header-section-number">7</span> Statistics in R</a></li>
<li><a class="" href="functions.html"><span class="header-section-number">8</span> Functions</a></li>
<li><a class="active" href="reproducible-results-markdown-quarto-notebooks.html"><span class="header-section-number">9</span> Reproducible Results (Markdown, Quarto & Notebooks)</a></li>
<li><a class="" href="r-shiny.html"><span class="header-section-number">10</span> R Shiny</a></li>
<li><a class="" href="github_r.html"><span class="header-section-number">11</span> Version control with Git and GitHub</a></li>
<li><a class="" href="additional-resources.html">Additional Resources</a></li>
</ul>
<div class="book-extra">
</div>
</nav>
</div>
</header><main class="col-sm-12 col-md-9 col-lg-7" id="content"><div id="reproducible-results-markdown-quarto-notebooks" class="section level1" number="9">
<h1>
<span class="header-section-number">9</span> Reproducible Results (Markdown, Quarto & Notebooks)<a class="anchor" aria-label="anchor" href="#reproducible-results-markdown-quarto-notebooks"><i class="fas fa-link"></i></a>
</h1>
<p></p>
<div class="inline-figure"><img src="images/rmd.png" width="85%" style="display: block; margin: auto;"></div>
<p></p>
<p>Welcome to this chapter on reproducible results in R! In this chapter, we will explore the different tools and techniques that you can use to ensure that your data analysis workflows are <strong>not only accurate but also reproducible</strong>. We’ll be covering Markdown, Quarto, and Notebooks , so you can get a comprehensive understanding of each one.</p>
<div class="inline-figure"><img src="images/note.png"></div>
<blockquote>
<ul>
<li><p><em>Although we will cover all three tools - R Markdown, R Notebooks, and Quarto - we will put a particular emphasis on R Markdown. This is because R Markdown is one of the most widely used for creating reproducible documents and presentations in R.</em></p></li>
<li><p><em>Most Importantly! Knowing how to use R Markdown can be a great foundation for learning and using R Notebooks and Quarto. This is because R Markdown is often considered the simplest and most user-friendly of the three tools, making it an excellent starting point for those new to reproducible data analysis. Once you have a solid understanding of R Markdown, you’ll likely find it easier to pick up the more advanced features of R Notebooks and Quarto. Plus, since R Markdown documents can easily be converted to R Notebooks or Quarto files, having a strong understanding of R Markdown can make it easier to transition to the other tools. So, even if you ultimately decide to use a different tool for your particular use case, learning R Markdown is definitely a great starting point.</em></p></li>
</ul>
</blockquote>
<div id="why-reproducibility" class="section level2" number="9.1">
<h2>
<span class="header-section-number">9.1</span> Why Reproducibility<a class="anchor" aria-label="anchor" href="#why-reproducibility"><i class="fas fa-link"></i></a>
</h2>
<p>Before we dive into the specifics, let’s take a step back and ask why reproducibility is so important in the first place. In today’s data-driven world, many important decisions are made based on data analyses. But how can we be sure that these analyses are accurate and reliable? One way to increase confidence in our results is to make sure that our analyses are reproducible. This means that anyone can take our code and data, run it themselves, and get the same results that we did. Not only does this increase confidence in our results, but it also makes it easier to share our work with others and build on it collaboratively.</p>
<p>So, let’s start by exploring R Markdown, one of the most popular tools for creating reproducible documents and presentations in R. With R Markdown, you can combine code, text, and figures in a single document, making it easy to create a narrative around your analysis. We’ll show you how to create simple documents, as well as more complex reports with multiple outputs.</p>
<p>Next up, we’ll dive into R Notebooks, another powerful tool for creating reproducible analyses. With R Notebooks, you can create interactive documents that allow readers to explore your data and code in real-time.</p>
<p>Finally, we’ll introduce you to Quarto, a newer tool for creating reproducible reports and documents. Quarto builds on the strengths of both R Markdown and Notebooks, allowing you to create complex, interactive reports with a variety of outputs.</p>
<p>Throughout the chapter, we’ll provide examples and best practices for using each tool, as well as tips and tricks for making your analyses more reproducible. By the end of this chapter, you’ll have a solid understanding of the different tools available for creating reproducible results in R, and be able to choose the best tool for your specific needs.</p>
<p>So let’s get started and dive into the world of reproducible data analysis in R!</p>
</div>
<div id="the-visual-editor" class="section level2" number="9.2">
<h2>
<span class="header-section-number">9.2</span> <img src="images/tip.png"> The Visual Editor<a class="anchor" aria-label="anchor" href="#the-visual-editor"><i class="fas fa-link"></i></a>
</h2>
<p>If you’re not comfortable with writing <code>Markdown</code> or <code>LaTeX</code> code, don’t worry - there is a visual editor available for R Markdown, Quarto, and R Notebooks. This editor provide a point-and-click interface that makes it easier to create and edit your documents. <u><strong>Isn’t that nice ?!…</strong></u></p>
<div class="inline-figure"><img src="images/did-you-know-smiley.png" style="width:50.0%"></div>
<p>Starting with <strong>R Studio 1.4</strong> (i.e. release of January 2021 and later ), the IDE includes a visual markdown editor that works on any markdown-based document, such as <code>.md</code> or <code>.Rmd</code> or <code>.Qmd</code> files.</p>
<p>Highlights of visual mode include:</p>
<ul>
<li><p>Visual editing for all of Pandoc markdown, including tables, divs/spans, definition lists, attributes, etc.</p></li>
<li><p>Extensive support for citations, including integration with Zotero and the ability to insert citations from DOIs or searches of PubMed, Crossref, and DataCite.</p></li>
<li><p>Scientific and technical writing features, including cross-references, footnotes, equations, code execution, and embedded LaTeX.</p></li>
<li><p>Writing productivity features, including real time spell-checking and outline navigation.</p></li>
<li><p>Tight integration with source editing (editing location and undo/redo state are preserved when switching between modes).</p></li>
<li><p>Rich keyboard support. In addition to traditional shortcuts, you can use markdown expressions (e.g. <code>##</code>, <code>**bold**</code>, etc.) for formatting. If you don’t remember all of the keyboard shortcuts, you can also use the catch-all <code>⌃ /</code> shortcut to insert anything.</p></li>
</ul>
<p><u>Let me walk you through several features of visual editing mode.</u></p>
<ol style="list-style-type: decimal">
<li>To switch into the visual mode for a markdown document:</li>
</ol>
<ul>
<li><p>In <code>RStudio v2022.02</code>, click the “Visual” button located on the left side of the editor toolbar.</p></li>
<li><p>In earlier versions, click the compass icon located on the right side of the editor toolbar.</p></li>
</ul>
<p>Alternatively, use the <code>Ctrl + Shift + F4</code> keyboard shortcut.</p>
<ol start="2" style="list-style-type: decimal">
<li>You can customize various editor settings. Go to <code>Tools</code> -> <code>Global Options</code> -> <code>R Markdown</code> -> <code>Visual</code> to choose your options, such as:</li>
</ol>
<ul>
<li><p>Whether your new documents use the visual markdown editor by default.</p></li>
<li><p>Whether the document outline shows by default.</p></li>
<li><p>How to wrap text in the document.</p></li>
</ul>
<p></p>
<pre><code><img src="images/visual_editor_tools.png" width="90%" style="display: block; margin: auto;" /></code></pre>
<p></p>
<div class="inline-figure"><img src="images/visual_editor_tools.png" style="width:70.0%"></div>
<p>To explore more about the Visual Editor, <a href="https://posit.co/blog/exploring-rstudio-visual-markdown-editor/" title="Ckick to open the contents"><strong>CLICK HERE</strong></a></p>
</div>
<div id="r-markdown" class="section level2" number="9.3">
<h2>
<span class="header-section-number">9.3</span> R Markdown<a class="anchor" aria-label="anchor" href="#r-markdown"><i class="fas fa-link"></i></a>
</h2>
<p>Welcome to the exciting world of reproducible data analysis using R markdown! In this section, we’ll explore the powerful tool of R Markdown and how it can help you create top-notch, reproducible reports to facilitate open science.</p>
<div id="what-is-r-markdown" class="section level3" number="9.3.1">
<h3>
<span class="header-section-number">9.3.1</span> What is R markdown?<a class="anchor" aria-label="anchor" href="#what-is-r-markdown"><i class="fas fa-link"></i></a>
</h3>
<p>R Markdown is a versatile tool that allows you to seamlessly combine code, text, graphics, and tables into a single document. Not only does this make your workflow more streamlined, but it also ensures that your analysis can be easily reproduced by others. So what are you waiting for? Let’s dive in and discover the exciting possibilities of R Markdown!</p>
<p>R markdown is a simple and easy to use plain text language used to combine your R code, results from your data analysis (including plots and tables) and written commentary into a single nicely formatted and reproducible document (like a report, publication, thesis chapter or a web page like this one).</p>
<p>In technical terms, R Markdown is a type of markup language, which is essentially a way to create a plain text file that can include formatted text, images, headers, and links to other documents. Markdown is the underlying language that R Markdown is based on, and it’s widely used for creating easily readable documents on the web. You’ve likely encountered another markup language, HTML, without even realizing it, as it’s the backbone of most web content today.</p>
<p>But fear not, learning R Markdown is a breeze, and it’s far simpler than HTML. Plus, when used with RStudio, it seamlessly integrates into your workflow, allowing you to create content that’s loaded with features and visually appealing. So why not take advantage of this powerful tool? Don’t worry about the details just yet.</p>
<p>At this point, it’s likely that you’re already using R to delve into and scrutinize your intriguing data. This means that you’re already on the path towards making your analysis more transparent, reproducible, and easily shared with others.</p>
<p><strong>If i guess right, you might be currently doing this (below)?</strong></p>
<p>You importing data from your preferred source into RStudio, write R code to explore and analyze the data, and then manually combining plots, analysis output tables, and written prose into a single MS Word document, for your report.</p>
<div class="inline-figure"><img src="images/why_rmd.png" style="width:50.0%"></div>
<div class="inline-figure">
<img src="images/caution.png"> While this approach isn’t necessarily wrong, it has its limitations……..</div>
<ul>
<li><p>Firstly, it’s not very reproducible. By separating your R code from the final document, you’re creating multiple opportunities for undocumented decisions to be made, such as which plots to use or which analyses to include/exclude.</p></li>
<li><p>Secondly, it’s inefficient. If you need to make changes later on (such as creating a new plot or updating your analysis), you’ll have to create or modify multiple documents, increasing the chances of mistakes creeping into your workflow.</p></li>
<li><p>Thirdly, it’s challenging to maintain. If your analysis changes, you’ll need to update multiple files and documents.</p></li>
<li><p>Lastly, it can be difficult to decide what to share with others. Should you share all of your code (including data exploration and model validation), or just the code specific to your final document? Many researchers maintain two R scripts, one for analysis and one for sharing with their final report. However, this practice is time-consuming and confusing, and should be avoided.</p></li>
</ul>
<p>To address these limitations, we’ll explore a more efficient and reproducible workflow using R Markdown, R Notebooks, and Quarto in the following chapters of this book. With these tools, you can seamlessly combine your R code, written prose, plots, and analysis output in a single document that’s easy to share and maintain, making your research more robust and efficient.</p>
<p><img src="images/workflow_reproduciable.jpg" style="width:90.0%"><br>
Your data is imported into RStudio (or R) as before but this time all of the R code you used to analyse your data, produce your plots and your written text (Introduction, Materials and Methods, Discussion etc) is contained within a single R markdown document which is then used (along with your data) to automatically create your final document. This is exactly what R markdown allows you to do.</p>
<p>R Markdown is a powerful tool that offers many advantages to data analysts and researchers.</p>
<ul>
<li><p>One of the primary benefits of using R Markdown is that it explicitly links your data with your R code and output, creating a fully reproducible workflow. This means that all of the R code used to explore, summarize, and analyze your data can be included in a single, easy-to-read document.</p></li>
<li><p>Another advantage of R Markdown is its versatility. With just a single R markdown document, you can create a wide variety of output formats, including PDFs, HTML web pages, and Microsoft Word documents, among many others. This makes collaboration and communication with others a breeze.</p></li>
<li><p>R Markdown also enhances the transparency of your research. By including your data and R markdown file with your publication or thesis chapter as supplementary material or hosting it on a GitHub repository, you can provide others with a clear and complete picture of your research process.</p></li>
<li><p>Finally, R Markdown can significantly increase the efficiency of your workflow. If you need to modify or extend your current analysis, you can simply update your R markdown document, and these changes will automatically be included in your final document. This not only saves time but also ensures that your analyses are always up-to-date and accurate.</p></li>
</ul>
</div>
<div id="get-started-with-r-markdown" class="section level3" number="9.3.2">
<h3>
<span class="header-section-number">9.3.2</span> Get started with R markdown<a class="anchor" aria-label="anchor" href="#get-started-with-r-markdown"><i class="fas fa-link"></i></a>
</h3>
<p>To use R markdown you will first need to install the <code>rmarkdown</code> package and any package dependencies. If you would like to create pdf documents (or MS Word documents) from your R markdown file you will also need to install a version of <code>LaTeX</code> on your computer. If you’ve not installed <code>LaTeX</code> before, we recommend that you install <code>TinyTeX</code> Again.</p>
</div>
<div id="new_rm" class="section level3" number="9.3.3">
<h3>
<span class="header-section-number">9.3.3</span> Create an R markdown document<a class="anchor" aria-label="anchor" href="#new_rm"><i class="fas fa-link"></i></a>
</h3>
<p>Right, time to create your first R markdown document. Within RStudio, click on the menu <code>File</code> -> <code>New File</code> -> <code>R Markdown...</code>. In the pop up window, give the document a ‘Title’ and enter the ‘Author’ information (your name) and select HTML as the default output. We can change all of this later so don’t worry about it for the moment.</p>
<p></p>
<div class="inline-figure"><img src="images/rmd_new1.jpg" width="85%" style="display: block; margin: auto;"></div>
<p></p>
<p>As you begin creating a new R Markdown document, you may notice that it comes with some pre-existing code examples. Typically, you would remove all of this code except for the important information at the top, which is known as the YAML header (we’ll delve more into that shortly), and start adding your own code.</p>
<p>For now, though, let’s use this document as a practice exercise for converting R Markdown to both HTML and PDF formats. By doing so, we can ensure that everything is functioning as it should be before diving into more advanced coding techniques.</p>
<p>After you’ve finished creating your R Markdown document, it’s important to save it in a location that’s easy to find. You can easily save your document by clicking on “<code>File</code>” in the RStudio menu and selecting “<code>Save</code>” (or by using the keyboard shortcut “<code>Ctrl + S"</code> on Windows or”<code>Cmd + S</code>” on a Mac). Don’t forget to give your file an appropriate name. Keep in mind that your new R Markdown file will have a <code>.Rmd</code> file extension.</p>
<p>To transform your <code>.Rmd</code> file into a polished HTML document, simply click on the drop down next to the “<code>Knit</code>” icon at the top of your source window. From there, select “<code>Knit to HTML</code>” to create a formatted, HTML version of your document. Notice that there is a new <code>R Markdown</code> tab in your console window which provides you with information on the rendering process and will also display any errors if something goes wrong.</p>
<p>If everything went smoothly a new HTML file will have been created and saved in the same directory as your <code>.Rmd</code> file (ours will be called <code>file_name.html</code>). To view this document simply double click on the file to open in a browser (like Chrome or edge) to display the rendered content. RStudio will also display a preview of the rendered file in a new window for you to check out (your window might look slightly different if you’re using a Windows computer).</p>
<div class="inline-figure"><img src="images/great_emoji.png" style="width:40.0%"></div>
<p>Great, you’ve just rendered your first R markdown document. If you want to knit your <code>.Rmd</code> file to a pdf document then all you need to do is choose <code>knit to PDF</code> instead of <code>knit to HTML</code> when you click on the <code>knit</code> icon. This will create a file called <code>file_name.pdf</code> which you can double click to open. Give it a go!</p>
</div>
<div id="the-r-markdown-anatomy" class="section level3" number="9.3.4">
<h3>
<span class="header-section-number">9.3.4</span> The R markdown anatomy<a class="anchor" aria-label="anchor" href="#the-r-markdown-anatomy"><i class="fas fa-link"></i></a>
</h3>
<p>Great, now that you can easily convert your R markdown file into both HTML and PDF formats in RStudio, let’s delve deeper into the key components that make up a typical R markdown document. Typically, an R markdown document comprises of three main elements:</p>
<ol style="list-style-type: decimal">
<li><p>YAML header,</p></li>
<li><p>Formatted text, and</p></li>
<li><p>one or more code chunks.</p></li>
</ol>
<p>By mastering these components, you’ll be able to create powerful and effective data analyses that are both informative and visually appealing.</p>
<div id="yaml-header" class="section level4" number="9.3.4.1">
<h4>
<span class="header-section-number">9.3.4.1</span> YAML header<a class="anchor" aria-label="anchor" href="#yaml-header"><i class="fas fa-link"></i></a>
</h4>
<p>YAML stands for “YAML Ain’t Markup Language”, and it’s a human-readable data serialization language.</p>
<ul>
<li><p>The YAML header is an optional component that appears at the top of an R Markdown document and is enclosed by three hyphens (<code>---</code>) on a line of its own before and after.</p></li>
<li><p>The YAML header contains metadata and options for the entire document, such as the title, author, date, output format, and more.</p></li>
<li><p>A minimal YAML header is automatically generated by RStudio when you create a new R Markdown document, but you can modify it to suit your needs.</p></li>
<li><p>The most commonly used YAML options in R Markdown include “title”, “author”, “date”, “output format”, “bibliography”, “csl”, “link-citations”, “toc”, “toc_depth”, and “number_sections”.</p></li>
<li><p>YAML options are specified using a key-value pair syntax, where the key is separated from the value by a colon and a space. For example: “<code>title: My R Markdown Document</code>”.</p></li>
<li><p>You can also use variables in your YAML header to make your R Markdown document more dynamic. For example, you can use the <strong>2023-04-11</strong> function to automatically insert the current date in your document.</p></li>
<li><p>Make sure to follow the correct YAML syntax rules, such as using proper indentation and quotes when necessary, to avoid any errors when rendering your document.</p></li>
</ul>
<p></p>
<div class="sourceCode" id="cb185"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb185-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-1" aria-hidden="true" tabindex="-1"></a><span class="sc">---</span></span>
<span id="cb185-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-2" aria-hidden="true" tabindex="-1"></a>title<span class="sc">:</span> <span class="st">"My R Markdown Document"</span></span>
<span id="cb185-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-3" aria-hidden="true" tabindex="-1"></a>author<span class="sc">:</span> <span class="st">"Africano"</span></span>
<span id="cb185-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-4" aria-hidden="true" tabindex="-1"></a>date<span class="sc">:</span> <span class="st">"April 10, 2023"</span></span>
<span id="cb185-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-5" aria-hidden="true" tabindex="-1"></a>output<span class="sc">:</span></span>
<span id="cb185-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-6" aria-hidden="true" tabindex="-1"></a> html_document<span class="sc">:</span></span>
<span id="cb185-7"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-7" aria-hidden="true" tabindex="-1"></a> theme<span class="sc">:</span> cosmo</span>
<span id="cb185-8"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-8" aria-hidden="true" tabindex="-1"></a> toc<span class="sc">:</span> true</span>
<span id="cb185-9"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-9" aria-hidden="true" tabindex="-1"></a> toc_depth<span class="sc">:</span> <span class="dv">2</span></span>
<span id="cb185-10"><a href="reproducible-results-markdown-quarto-notebooks.html#cb185-10" aria-hidden="true" tabindex="-1"></a><span class="sc">---</span></span></code></pre></div>
<p></p>
</div>
<div id="formatted-text" class="section level4" number="9.3.4.2">
<h4>
<span class="header-section-number">9.3.4.2</span> Formatted text<a class="anchor" aria-label="anchor" href="#formatted-text"><i class="fas fa-link"></i></a>
</h4>
<p>As we discussed earlier, R markdown offers a fantastic feature that allows you to effortlessly combine your R code, analysis, and writing. The beauty of R markdown is that you don’t have to rely on a word processor to bring your work together. You can render almost all the text formatting that you might require, including italics, bold, strikethrough, superscript, subscript, bulleted and numbered lists, headers and footers, images, links, and equations. Here is an example of marking up text formatting in an R markdown document</p>
<div class="sourceCode" id="cb186"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb186-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb186-1" aria-hidden="true" tabindex="-1"></a><span class="fu">### Air Quality Analysis</span></span>
<span id="cb186-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb186-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb186-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb186-3" aria-hidden="true" tabindex="-1"></a>Air pollution is a major issue in urban areas, affecting both human health and the environment. Common air components include Carbon Monoxide (CO), Nitrogen Dioxide (NO~2~), Ozone (O~3~), Particulate Matter (PM), and Sulfur Dioxide (SO~2~).</span>
<span id="cb186-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb186-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb186-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb186-5" aria-hidden="true" tabindex="-1"></a>One way to measure air quality is through the Air Quality Index (AQI), which assigns a numerical value between 0 and 500 based on the concentration of several air components. A higher AQI value indicates greater health concerns.</span>
<span id="cb186-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb186-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb186-7"><a href="reproducible-results-markdown-quarto-notebooks.html#cb186-7" aria-hidden="true" tabindex="-1"></a>others include (NH~4~, NO~3~ and PO~3~)</span></code></pre></div>
<p><strong>Emphasis</strong></p>
<p>Some of the most common R markdown syntax for providing emphasis and formatting text is given below.</p>
<div class="inline-table"><table class="table table-sm">
<thead><tr class="header">
<th align="center">Objective</th>
<th align="center">R markdown code</th>
<th align="center">output</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="center">bold text</td>
<td align="center"><code>**mytext**</code></td>
<td align="center"><strong>mytext</strong></td>
</tr>
<tr class="even">
<td align="center">italic text</td>
<td align="center"><code>*mytext*</code></td>
<td align="center"><em>mytext</em></td>
</tr>
<tr class="odd">
<td align="center">strike through</td>
<td align="center"><code>~~mytext~~</code></td>
<td align="center"><del>mytext</del></td>
</tr>
<tr class="even">
<td align="center">superscript</td>
<td align="center"><code>mytext^2^</code></td>
<td align="center">mytext<sup>2</sup>
</td>
</tr>
<tr class="odd">
<td align="center">subscript</td>
<td align="center"><code>mytext~2~</code></td>
<td align="center">mytext<sub>2</sub>
</td>
</tr>
</tbody>
</table></div>
<p><img src="images/caution.png"><strong>White space and line breaks</strong></p>
<p>It is important to note that R markdown typically ignores multiple spaces within text, as well as carriage returns. Therefore, a line break in your R markdown text may not necessarily create a new paragraph, it is ignored. It is important to keep this in mind when formatting your text to ensure your intended structure is accurately rendered.</p>
<p>This is generally a good thing (no more random multiple spaces in your text). If you want your text to start on a new line then you can simply add two blank spaces at the end of the preceding line.</p>
<p>If you really want multiple spaces within your text then you can use the <strong>N</strong>on <strong>b</strong>reaking <strong>sp</strong>ace tag <code>&nbsp;</code></p>
</div>
<div id="headings" class="section level4 unnumbered">
<h4>Headings<a class="anchor" aria-label="anchor" href="#headings"><i class="fas fa-link"></i></a>
</h4>
<p>You can add headings and subheadings to your R markdown document by using the <code>#</code> symbol at the beginning of the line. You can decrease the size of the headings by simply adding more <code>#</code> symbols. For example</p>
<div class="sourceCode" id="cb187"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb187-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb187-1" aria-hidden="true" tabindex="-1"></a><span class="fu"># Heading 1</span></span>
<span id="cb187-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb187-2" aria-hidden="true" tabindex="-1"></a><span class="fu">## Heading 2</span></span>
<span id="cb187-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb187-3" aria-hidden="true" tabindex="-1"></a><span class="fu">### Heading 3</span></span>
<span id="cb187-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb187-4" aria-hidden="true" tabindex="-1"></a><span class="fu">#### Heading 4</span></span>
<span id="cb187-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb187-5" aria-hidden="true" tabindex="-1"></a><span class="fu">##### Heading 5</span></span>
<span id="cb187-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb187-6" aria-hidden="true" tabindex="-1"></a><span class="fu">###### Heading 6</span></span></code></pre></div>
</div>
<div id="comments" class="section level4 unnumbered">
<h4>Comments<a class="anchor" aria-label="anchor" href="#comments"><i class="fas fa-link"></i></a>
</h4>
<p>As you can see above the meaning of the <code>#</code> symbol is different when formatting text in an R markdown document compared to a standard R script (which is used to included a comment - remember?!). You can, however, use a <code>#</code> symbol to comment code inside a <a href="reproducible-results-markdown-quarto-notebooks.html#code_chunks">code chunk</a> as usual (more about this in a bit). If you want to include a comment in your R markdown document outside a code chunk which won’t be included in the final rendered document then enclose your comment between <code><!--</code> and <code>--></code>.</p>
<div class="sourceCode" id="cb188"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb188-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb188-1" aria-hidden="true" tabindex="-1"></a><span class="co"><!--</span></span>
<span id="cb188-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb188-2" aria-hidden="true" tabindex="-1"></a><span class="co">this is an example of how to format a comment using R markdown.</span></span>
<span id="cb188-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb188-3" aria-hidden="true" tabindex="-1"></a><span class="co">--></span></span></code></pre></div>
</div>
<div id="lists" class="section level4 unnumbered">
<h4>Lists<a class="anchor" aria-label="anchor" href="#lists"><i class="fas fa-link"></i></a>
</h4>
<p>If you want to create a bullet point list of text you can format an unordered list with sub items. Notice that the sub-items need to be indented.</p>
<div class="sourceCode" id="cb189"><pre class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb189-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb189-1" aria-hidden="true" tabindex="-1"></a><span class="ss">- </span>item 1</span>
<span id="cb189-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb189-2" aria-hidden="true" tabindex="-1"></a><span class="ss">- </span>item 2</span>
<span id="cb189-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb189-3" aria-hidden="true" tabindex="-1"></a><span class="ss"> + </span>sub-item 2</span>
<span id="cb189-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb189-4" aria-hidden="true" tabindex="-1"></a><span class="ss"> + </span>sub-item 3</span>
<span id="cb189-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb189-5" aria-hidden="true" tabindex="-1"></a><span class="ss">- </span>item 3</span>
<span id="cb189-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb189-6" aria-hidden="true" tabindex="-1"></a><span class="ss">- </span>item 4</span></code></pre></div>
<p>If you need an ordered list</p>
<div class="sourceCode" id="cb190"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb190-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb190-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb190-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb190-2" aria-hidden="true" tabindex="-1"></a><span class="ss">1. </span>item 1</span>
<span id="cb190-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb190-3" aria-hidden="true" tabindex="-1"></a><span class="ss">2. </span>item 2</span>
<span id="cb190-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb190-4" aria-hidden="true" tabindex="-1"></a><span class="ss"> + </span>sub-item 2</span>
<span id="cb190-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb190-5" aria-hidden="true" tabindex="-1"></a><span class="ss"> + </span>sub-item 3</span>
<span id="cb190-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb190-6" aria-hidden="true" tabindex="-1"></a><span class="ss">3. </span>item 3</span>
<span id="cb190-7"><a href="reproducible-results-markdown-quarto-notebooks.html#cb190-7" aria-hidden="true" tabindex="-1"></a><span class="ss">4. </span>item 4</span></code></pre></div>
<p>Images</p>
<p>Another useful feature is the ability to embed images and links to web pages (or other documents) into your R markdown document. You can include images into your R markdown document in a number of different ways. Perhaps the simplest method is to use</p>
<div class="sourceCode" id="cb191"><pre class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb191-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb191-1" aria-hidden="true" tabindex="-1"></a><span class="al">![The R Branded cup](images/r_branded_cup.png)</span></span></code></pre></div>
<p>The code above will only work if the image file (r_branded_cup.png) is in the right place relative to where you saved your <code>.Rmd</code> file. In the example above the image file is in a sub directory (folder) called <code>images</code> in the directory where we saved your file. You can embed images saved in many different file types but perhaps the most common are <code>.jpg</code> and <code>.png</code>.</p>
<p>We think a more flexible way of including images in your document is to use the <code>include_graphics()</code> function from the <code>knitr</code> package as this gives finer control over the alignment and image size (it also works more or less the same with both HTML and pdf output formats). However, to do this you will need to include this R code in a ‘<code>code chunk</code>’ which we haven’t covered yet. Despite this we’ll leave the code here for later reference. This code center aligns the image and scales it to 60% of it’s original size. See <code>?include_graphics</code> for more options.</p>
<p></p>
<div class="inline-figure"><img src="images/r_branded_cup.png" width="60%" style="display: block; margin: auto;"></div>
<p></p>
</div>
<div id="links" class="section level4 unnumbered">
<h4>Links<a class="anchor" aria-label="anchor" href="#links"><i class="fas fa-link"></i></a>
</h4>
<p>In addition to images you can also include links to webpages or other links in your document. Use the following syntax to create a clickable link to an existing webpage. The link text goes between the square brackets and the URL for the webpage between the round brackets immediately after.</p>
<div class="sourceCode" id="cb192"><pre class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb192-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb192-1" aria-hidden="true" tabindex="-1"></a>You can include a text for your clickable <span class="co">[</span><span class="ot">Author git hub</span><span class="co">](https://github.com/africanobyamugisha)</span></span></code></pre></div>
</div>
<div id="code_chunks" class="section level4" number="9.3.4.3">
<h4>
<span class="header-section-number">9.3.4.3</span> Code chunks<a class="anchor" aria-label="anchor" href="#code_chunks"><i class="fas fa-link"></i></a>
</h4>
<p>Let’s dive into the crux of the matter; The next step is to add R code to your R Markdown document. You can do this by inserting your code into a ‘<code>code chunk</code>’, which begins and ends with three backticks <code>```</code>. These are also called ‘grave accents’ or ‘back quotes’ and should not be confused with an apostrophe. On most keyboards, the backtick key is located on the same key as the tilde (~) and can be accessed by pressing shift.</p>
<div class="sourceCode" id="cb193"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb193-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb193-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb193-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb193-2" aria-hidden="true" tabindex="-1"></a><span class="in">Any valid R code goes here</span></span>
<span id="cb193-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb193-3" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p>You can insert a code chunk by either typing the chunk delimiters <code>```{r}</code> and <code>```</code> manually or use the RStudio toolbar (the Insert button) or by clicking on the menu <code>Code</code> -> <code>Insert Chunk</code>. Perhaps an even better way is to get familiar with the keyboard shortcuts <code>Ctrl + Alt + I</code> for Windows and <code>Cmd + Option + I on</code> MacOSX.</p>
<p>There are a many things you can do with code chunks: you can produce text output from your analysis, create tables and figures and insert images amongst other things. Within the code chunk you can place rules and arguments between the curly brackets <code><a href="https://rdrr.io/r/base/Paren.html">{}</a></code> that give you control over how your code is interpreted and output is rendered. These are known as chunk options. The only mandatory chunk option is the first argument which specifies which language you’re using (<code>r</code> in our case but [other][engines] languages are supported). Note, all of your chunk options must be written between the curly brackets on one line with no line breaks.</p>
<p>You can also specify an optional code chunk name (or label) which can be useful when trying to debug problems and when performing advanced document rendering. In the following block we name the code chunk <code>summary-stats</code>, create a dataframe (<code>dataf</code>) with two variables <code>x</code> and <code>y</code> and then use the <code><a href="https://rdrr.io/r/base/summary.html">summary()</a></code> function to display some summary statistics . When we run the code chunk both the R code and the resulting output are displayed in the final document.</p>
<div class="sourceCode" id="cb194"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb194-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb194-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, summary-stats}</span></span>
<span id="cb194-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb194-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb194-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb194-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb194-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb194-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb194-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb194-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb194-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb194-6" aria-hidden="true" tabindex="-1"></a><span class="in">summary(dataf)</span></span>
<span id="cb194-7"><a href="reproducible-results-markdown-quarto-notebooks.html#cb194-7" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="sourceCode" id="cb195"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span> <span class="co"># create an x variable</span></span>
<span><span class="va">y</span> <span class="op"><-</span> <span class="fl">10</span><span class="op">:</span><span class="fl">1</span> <span class="co"># create a y variable</span></span>
<span><span class="va">dataf</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html">summary</a></span><span class="op">(</span><span class="va">dataf</span><span class="op">)</span></span>
<span><span class="co">## x y </span></span>
<span><span class="co">## Min. : 1.00 Min. : 1.00 </span></span>
<span><span class="co">## 1st Qu.: 3.25 1st Qu.: 3.25 </span></span>
<span><span class="co">## Median : 5.50 Median : 5.50 </span></span>
<span><span class="co">## Mean : 5.50 Mean : 5.50 </span></span>
<span><span class="co">## 3rd Qu.: 7.75 3rd Qu.: 7.75 </span></span>
<span><span class="co">## Max. :10.00 Max. :10.00</span></span></code></pre></div>
<p></p>
<p>When using chunk names make sure that you don’t have duplicate chunk names in your R markdown document and avoid spaces and full stops as this may cause problems when you come to knit your document (We use a <code>-</code> to separate words in our chunk names).</p>
<p>If we wanted to only display the output of our R code (just the summary statistics for example) and not the code itself in our final document we can use the chunk option <code>echo=FALSE</code></p>
<div class="sourceCode" id="cb196"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb196-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb196-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, summary-stats, echo=FALSE}</span></span>
<span id="cb196-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb196-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb196-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb196-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb196-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb196-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb196-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb196-5" aria-hidden="true" tabindex="-1"></a><span class="in">summary(dataf)</span></span>
<span id="cb196-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb196-6" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<pre><code>## x y
## Min. : 1.00 Min. : 1.00
## 1st Qu.: 3.25 1st Qu.: 3.25
## Median : 5.50 Median : 5.50
## Mean : 5.50 Mean : 5.50
## 3rd Qu.: 7.75 3rd Qu.: 7.75
## Max. :10.00 Max. :10.00</code></pre>
<p></p>
<p>To display the R code but not the output use the <code>results='hide'</code> chunk option.</p>
<div class="sourceCode" id="cb198"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb198-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb198-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, summary-stats, results='hide'}</span></span>
<span id="cb198-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb198-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb198-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb198-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb198-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb198-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb198-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb198-5" aria-hidden="true" tabindex="-1"></a><span class="in">summary(dataf)</span></span>
<span id="cb198-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb198-6" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="sourceCode" id="cb199"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span> <span class="co"># create an x variable</span></span>
<span><span class="va">y</span> <span class="op"><-</span> <span class="fl">10</span><span class="op">:</span><span class="fl">1</span> <span class="co"># create a y variable</span></span>
<span><span class="va">dataf</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html">summary</a></span><span class="op">(</span><span class="va">dataf</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p>Sometimes you may want to execute a code chunk without showing any output at all. You can suppress the entire output using the chunk option <code>include=FALSE</code>.</p>
<div class="sourceCode" id="cb200"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb200-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb200-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, summary-stats, include=FALSE}</span></span>
<span id="cb200-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb200-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb200-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb200-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb200-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb200-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb200-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb200-5" aria-hidden="true" tabindex="-1"></a><span class="in">summary(dataf)</span></span>
<span id="cb200-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb200-6" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p>There are a large number of chunk options documented [here][chunk-opt] with a more condensed version [here][rmd-ref]. Perhaps the most commonly used are summarised below with the default values shown.</p>
<div class="inline-table"><table class="table table-sm">
<colgroup>
<col width="29%">
<col width="37%">
<col width="33%">
</colgroup>
<thead><tr class="header">
<th align="left">Chunk option</th>
<th align="left">default value</th>
<th align="left">Function</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="left">echo</td>
<td align="left"><code>echo=TRUE</code></td>
<td align="left">If FALSE, will not display the code in the final document</td>
</tr>
<tr class="even">
<td align="left">results</td>
<td align="left"><code>results='markup'</code></td>
<td align="left">If ‘hide’, will not display the code’s results in the final document. If ‘hold’, will delay displaying all output pieces until the end of the chunk. If ‘asis’, will pass through results without reformatting them.</td>
</tr>
<tr class="odd">
<td align="left">include</td>
<td align="left"><code>include=TRUE</code></td>
<td align="left">If FALSE, will run the chunk but not include the chunk in the final document.</td>
</tr>
<tr class="even">
<td align="left">eval</td>
<td align="left"><code>eval=TRUE</code></td>
<td align="left">If FALSE, will not run the code in the code chunk.</td>
</tr>
<tr class="odd">
<td align="left">message</td>
<td align="left"><code>message=TRUE</code></td>
<td align="left">If FALSE, will not display any messages generated by the code.</td>
</tr>
<tr class="even">
<td align="left">warning</td>
<td align="left"><code>warning=TRUE</code></td>
<td align="left">If FALSE, will not display any warning messages generated by the code.</td>
</tr>
</tbody>
</table></div>
</div>
<div id="adding-figures" class="section level4" number="9.3.4.4">
<h4>
<span class="header-section-number">9.3.4.4</span> Adding figures<a class="anchor" aria-label="anchor" href="#adding-figures"><i class="fas fa-link"></i></a>
</h4>
<p>By default, figures produced by R code will be placed immediately after the code chunk they were generated from. For example:</p>
<div class="sourceCode" id="cb201"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb201-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb201-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, simple-plot}</span></span>
<span id="cb201-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb201-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb201-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb201-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb201-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb201-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb201-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb201-5" aria-hidden="true" tabindex="-1"></a><span class="in">plot(dataf$x, dataf$y, xlab = "x axis", ylab = "y axis")</span></span>
<span id="cb201-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb201-6" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="sourceCode" id="cb202"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span> <span class="co"># create an x variable</span></span>
<span><span class="va">y</span> <span class="op"><-</span> <span class="fl">10</span><span class="op">:</span><span class="fl">1</span> <span class="co"># create a y variable</span></span>
<span><span class="va">dataf</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op">(</span><span class="va">dataf</span><span class="op">$</span><span class="va">x</span>, <span class="va">dataf</span><span class="op">$</span><span class="va">y</span>, xlab <span class="op">=</span> <span class="st">"x axis"</span>, ylab <span class="op">=</span> <span class="st">"y axis"</span><span class="op">)</span></span></code></pre></div>
<div class="inline-figure"><img src="guide_to_R_pragramming_files/figure-html/simple-plot-1.png" width="80%" style="display: block; margin: auto;"></div>
<p></p>
<p>If you want to change the plot dimensions in the final document you can use the <code>fig.width=</code> and <code>fig.height=</code> chunk options (in inches!). You can also change the alignment of the figure using the <code>fig.align=</code> chunk option.</p>
<div class="sourceCode" id="cb203"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb203-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb203-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, simple-plot, fig.width=4, fig.height=3, fig.align='center'}</span></span>
<span id="cb203-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb203-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb203-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb203-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb203-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb203-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb203-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb203-5" aria-hidden="true" tabindex="-1"></a><span class="in">plot(dataf$x, dataf$y, xlab = "x axis", ylab = "y axis")</span></span>
<span id="cb203-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb203-6" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="sourceCode" id="cb204"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span> <span class="co"># create an x variable</span></span>
<span><span class="va">y</span> <span class="op"><-</span> <span class="fl">10</span><span class="op">:</span><span class="fl">1</span> <span class="co"># create a y variable</span></span>
<span><span class="va">dataf</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op">(</span><span class="va">dataf</span><span class="op">$</span><span class="va">x</span>, <span class="va">dataf</span><span class="op">$</span><span class="va">y</span>, xlab <span class="op">=</span> <span class="st">"x axis"</span>, ylab <span class="op">=</span> <span class="st">"y axis"</span><span class="op">)</span></span></code></pre></div>
<div class="inline-figure"><img src="guide_to_R_pragramming_files/figure-html/simple-plot2-1.png" width="80%" style="display: block; margin: auto;"></div>
<p></p>
<p>You can add a figure caption using the <code>fig.cap=</code> option.</p>
<div class="sourceCode" id="cb205"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb205-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb205-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, simple-plot-cap, fig.cap="A simple plot", fig.align='center'}</span></span>
<span id="cb205-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb205-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb205-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb205-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb205-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb205-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb205-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb205-5" aria-hidden="true" tabindex="-1"></a><span class="in">plot(dataf$x, dataf$y, xlab = "x axis", ylab = "y axis")</span></span>
<span id="cb205-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb205-6" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="sourceCode" id="cb206"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span> <span class="co"># create an x variable</span></span>
<span><span class="va">y</span> <span class="op"><-</span> <span class="fl">10</span><span class="op">:</span><span class="fl">1</span> <span class="co"># create a y variable</span></span>
<span><span class="va">dataf</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op">(</span><span class="va">dataf</span><span class="op">$</span><span class="va">x</span>, <span class="va">dataf</span><span class="op">$</span><span class="va">y</span>, xlab <span class="op">=</span> <span class="st">"x axis"</span>, ylab <span class="op">=</span> <span class="st">"y axis"</span><span class="op">)</span></span></code></pre></div>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:simple-plot-cap"></span>
<img src="guide_to_R_pragramming_files/figure-html/simple-plot-cap-1.png" alt="A simple plot" width="80%"><p class="caption">
Figure 9.1: A simple plot
</p>
</div>
<p></p>
<p>If you want to suppress the figure in the final document use the <code>fig.show='hide'</code> option.</p>
<div class="sourceCode" id="cb207"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb207-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb207-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, simple-plot, fig.show='hide'}</span></span>
<span id="cb207-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb207-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb207-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb207-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb207-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb207-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb207-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb207-5" aria-hidden="true" tabindex="-1"></a><span class="in">plot(dataf$x, dataf$y, xlab = "x axis", ylab = "y axis")</span></span>
<span id="cb207-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb207-6" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="sourceCode" id="cb208"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span> <span class="co"># create an x variable</span></span>
<span><span class="va">y</span> <span class="op"><-</span> <span class="fl">10</span><span class="op">:</span><span class="fl">1</span> <span class="co"># create a y variable</span></span>
<span><span class="va">dataf</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op">(</span><span class="va">dataf</span><span class="op">$</span><span class="va">x</span>, <span class="va">dataf</span><span class="op">$</span><span class="va">y</span>, xlab <span class="op">=</span> <span class="st">"x axis"</span>, ylab <span class="op">=</span> <span class="st">"y axis"</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p>If you’re using a package like [<code>ggplot2</code>][ggplot] to create your plots then don’t forget you will need to make the package available with the <code><a href="https://rdrr.io/r/base/library.html">library()</a></code> function in the code chunk (or in a preceding code chunk).</p>
<div class="sourceCode" id="cb209"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb209-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, simple-ggplot}</span></span>
<span id="cb209-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-2" aria-hidden="true" tabindex="-1"></a><span class="in">x <- 1:10 # create an x variable</span></span>
<span id="cb209-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-3" aria-hidden="true" tabindex="-1"></a><span class="in">y <- 10:1 # create a y variable</span></span>
<span id="cb209-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-4" aria-hidden="true" tabindex="-1"></a><span class="in">dataf <- data.frame(x = x, y = y)</span></span>
<span id="cb209-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb209-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-6" aria-hidden="true" tabindex="-1"></a><span class="in">library(ggplot2)</span></span>
<span id="cb209-7"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-7" aria-hidden="true" tabindex="-1"></a><span class="in">ggplot(dataf, aes(x = x, y = y)) +</span></span>
<span id="cb209-8"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-8" aria-hidden="true" tabindex="-1"></a><span class="in"> geom_point()</span></span>
<span id="cb209-9"><a href="reproducible-results-markdown-quarto-notebooks.html#cb209-9" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="sourceCode" id="cb210"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span> <span class="co"># create an x variable</span></span>
<span><span class="va">y</span> <span class="op"><-</span> <span class="fl">10</span><span class="op">:</span><span class="fl">1</span> <span class="co"># create a y variable</span></span>
<span><span class="va">dataf</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggplot2.tidyverse.org">ggplot2</a></span><span class="op">)</span></span>
<span><span class="fu"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op">(</span><span class="va">dataf</span>, <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op">(</span>x <span class="op">=</span> <span class="va">x</span>, y <span class="op">=</span> <span class="va">y</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<div class="inline-figure"><img src="guide_to_R_pragramming_files/figure-html/simple-ggplot-1.png" width="80%" style="display: block; margin: auto;"></div>
<p></p>
<p>Again, there are a large number of chunk options specific to producing plots and figures. See [here][rmd-ref] for more details.</p>
</div>
<div id="adding-tables" class="section level4" number="9.3.4.5">
<h4>
<span class="header-section-number">9.3.4.5</span> Adding tables<a class="anchor" aria-label="anchor" href="#adding-tables"><i class="fas fa-link"></i></a>
</h4>
<p>R markdown can print the contents of a dataframe as a table (or any other tabular object such as a summary of model output) by including the name of the dataframe in a code chunk. For example, to create a table of the first 10 rows of the inbuilt dataset <code>iris</code></p>
<div class="sourceCode" id="cb211"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb211-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb211-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, ugly-table}</span></span>
<span id="cb211-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb211-2" aria-hidden="true" tabindex="-1"></a><span class="in">iris[1:10,]</span></span>
<span id="cb211-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb211-3" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 5.1 3.5 1.4 0.2
## 2 4.9 3.0 1.4 0.2
## 3 4.7 3.2 1.3 0.2
## 4 4.6 3.1 1.5 0.2
## 5 5.0 3.6 1.4 0.2
## 6 5.4 3.9 1.7 0.4
## 7 4.6 3.4 1.4 0.3
## 8 5.0 3.4 1.5 0.2
## 9 4.4 2.9 1.4 0.2
## 10 4.9 3.1 1.5 0.1
## Species
## 1 setosa
## 2 setosa
## 3 setosa
## 4 setosa
## 5 setosa
## 6 setosa
## 7 setosa
## 8 setosa
## 9 setosa
## 10 setosa</code></pre>
<p></p>
<p>But how ugly is that! You can create slightly nicer looking tables using native markdown syntax (this doesn’t need to be in a code chunk).</p>
<p> </p>
<div class="sourceCode" id="cb213"><pre class="sourceCode markdown"><code class="sourceCode markdown"><span id="cb213-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb213-1" aria-hidden="true" tabindex="-1"></a><span class="in"> | x | y |</span></span>
<span id="cb213-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb213-2" aria-hidden="true" tabindex="-1"></a><span class="in"> |:----------:|:----------:|</span></span>
<span id="cb213-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb213-3" aria-hidden="true" tabindex="-1"></a><span class="in"> | 1 | 5 | </span></span>
<span id="cb213-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb213-4" aria-hidden="true" tabindex="-1"></a><span class="in"> | 2 | 4 |</span></span>
<span id="cb213-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb213-5" aria-hidden="true" tabindex="-1"></a><span class="in"> | 3 | 3 |</span></span>
<span id="cb213-6"><a href="reproducible-results-markdown-quarto-notebooks.html#cb213-6" aria-hidden="true" tabindex="-1"></a><span class="in"> | 4 | 2 |</span></span>
<span id="cb213-7"><a href="reproducible-results-markdown-quarto-notebooks.html#cb213-7" aria-hidden="true" tabindex="-1"></a><span class="in"> | 5 | 1 |</span></span></code></pre></div>
<p> </p>
<div class="inline-table"><table class="table table-sm">
<thead><tr class="header">
<th align="center">x</th>
<th align="center">y</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="center">1</td>
<td align="center">5</td>
</tr>
<tr class="even">
<td align="center">2</td>
<td align="center">4</td>
</tr>
<tr class="odd">
<td align="center">3</td>
<td align="center">3</td>
</tr>
<tr class="even">
<td align="center">4</td>
<td align="center">2</td>
</tr>
<tr class="odd">
<td align="center">5</td>
<td align="center">1</td>
</tr>
</tbody>
</table></div>
<p> </p>
<p>The <code>:-------:</code> lets R markdown know that the line above should be treated as a header and the lines below as the body of the table. Alignment within the table is set by the position of the <code>:</code>. To center align use <code>:------:</code>, to left align <code>:------</code> and right align <code>------:</code>. Whilst it can be fun(!) to create tables with raw markup it’s only practical for very small and simple tables.</p>
<p>The easiest way we know to include tables in an R markdown document is by using the <code>kable()</code> function from the <code>knitr</code> package (this should have already been installed when you installed the <code>rmarkdown</code> package). The <code>kable()</code> function can create tables for HTML, PDF and Word outputs.</p>
<p>To create a table of the first 10 rows of the <code>iris</code> dataframe using the <code>kable()</code> function simply write your code block as</p>
<div class="sourceCode" id="cb214"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb214-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb214-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, kable-table}</span></span>
<span id="cb214-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb214-2" aria-hidden="true" tabindex="-1"></a><span class="in">library(knitr)</span></span>
<span id="cb214-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb214-3" aria-hidden="true" tabindex="-1"></a><span class="in">kable(iris[1:10,])</span></span>
<span id="cb214-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb214-4" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="inline-table"><table class="table table-sm">
<thead><tr>
<th style="text-align:right;">
Sepal.Length
</th>
<th style="text-align:right;">
Sepal.Width
</th>
<th style="text-align:right;">
Petal.Length
</th>
<th style="text-align:right;">
Petal.Width
</th>
<th style="text-align:left;">
Species
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:right;">
5.1
</td>
<td style="text-align:right;">
3.5
</td>
<td style="text-align:right;">
1.4
</td>
<td style="text-align:right;">
0.2
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
4.9
</td>
<td style="text-align:right;">
3.0
</td>
<td style="text-align:right;">
1.4
</td>
<td style="text-align:right;">
0.2
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
4.7
</td>
<td style="text-align:right;">
3.2
</td>
<td style="text-align:right;">
1.3
</td>
<td style="text-align:right;">
0.2
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
4.6
</td>
<td style="text-align:right;">
3.1
</td>
<td style="text-align:right;">
1.5
</td>
<td style="text-align:right;">
0.2
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
5.0
</td>
<td style="text-align:right;">
3.6
</td>
<td style="text-align:right;">
1.4
</td>
<td style="text-align:right;">
0.2
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
5.4
</td>
<td style="text-align:right;">
3.9
</td>
<td style="text-align:right;">
1.7
</td>
<td style="text-align:right;">
0.4
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
4.6
</td>
<td style="text-align:right;">
3.4
</td>
<td style="text-align:right;">
1.4
</td>
<td style="text-align:right;">
0.3
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
5.0
</td>
<td style="text-align:right;">
3.4
</td>
<td style="text-align:right;">
1.5
</td>
<td style="text-align:right;">
0.2
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
4.4
</td>
<td style="text-align:right;">
2.9
</td>
<td style="text-align:right;">
1.4
</td>
<td style="text-align:right;">
0.2
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
<tr>
<td style="text-align:right;">
4.9
</td>
<td style="text-align:right;">
3.1
</td>
<td style="text-align:right;">
1.5
</td>
<td style="text-align:right;">
0.1
</td>
<td style="text-align:left;">
setosa
</td>
</tr>
</tbody>
</table></div>
<p></p>
<p></p>
<p></p>
<p>The <code>kable()</code> function offers plenty of options to change the formatting of the table. For example, if we want to round numeric values to one decimal place use the <code>digits =</code> argument. To center justify the table contents use <code>align = 'c'</code> and to provide custom column headings use the <code>col.names =</code> argument. See <code><a href="https://rdrr.io/pkg/knitr/man/kable.html">?knitr::kable</a></code> for more information.</p>
<div class="sourceCode" id="cb215"><pre class="sourceCode md"><code class="sourceCode markdown"><span id="cb215-1"><a href="reproducible-results-markdown-quarto-notebooks.html#cb215-1" aria-hidden="true" tabindex="-1"></a><span class="in">```{r, kable-table2}</span></span>
<span id="cb215-2"><a href="reproducible-results-markdown-quarto-notebooks.html#cb215-2" aria-hidden="true" tabindex="-1"></a><span class="in">kable(iris[1:10,], digits = 0, align = 'c', </span></span>
<span id="cb215-3"><a href="reproducible-results-markdown-quarto-notebooks.html#cb215-3" aria-hidden="true" tabindex="-1"></a><span class="in"> col.names = c('sepal length', 'sepal width', </span></span>
<span id="cb215-4"><a href="reproducible-results-markdown-quarto-notebooks.html#cb215-4" aria-hidden="true" tabindex="-1"></a><span class="in"> 'petal length', 'petal width', 'species'))</span></span>
<span id="cb215-5"><a href="reproducible-results-markdown-quarto-notebooks.html#cb215-5" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span></code></pre></div>
<p></p>
<div class="inline-table"><table class="table table-sm">
<thead><tr>
<th style="text-align:center;">
sepal length
</th>
<th style="text-align:center;">
sepal width
</th>
<th style="text-align:center;">
petal length
</th>
<th style="text-align:center;">
petal width
</th>
<th style="text-align:center;">
species
</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center;">
5
</td>
<td style="text-align:center;">
4
</td>
<td style="text-align:center;">
1
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">
5
</td>
<td style="text-align:center;">
3
</td>
<td style="text-align:center;">
1
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">
5
</td>
<td style="text-align:center;">
3
</td>
<td style="text-align:center;">
1
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">
5
</td>
<td style="text-align:center;">
3
</td>
<td style="text-align:center;">
2
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">
5
</td>
<td style="text-align:center;">
4
</td>
<td style="text-align:center;">
1
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">
5
</td>
<td style="text-align:center;">
4
</td>
<td style="text-align:center;">
2
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">
5
</td>
<td style="text-align:center;">
3
</td>
<td style="text-align:center;">
1
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">
5
</td>
<td style="text-align:center;">
3
</td>
<td style="text-align:center;">
2
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">
4
</td>
<td style="text-align:center;">
3
</td>
<td style="text-align:center;">
1
</td>
<td style="text-align:center;">
0
</td>
<td style="text-align:center;">
setosa
</td>
</tr>
<tr>
<td style="text-align:center;">