-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
technical-writing.html
736 lines (704 loc) · 36.6 KB
/
technical-writing.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
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.2.313">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Michael McCarthy">
<meta name="dcterms.date" content="2023-01-24">
<meta name="description" content="Technical writing and literate programming in Quarto documents.">
<title>Tidy Tales - Technical Writing</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="../../../site_libs/quarto-nav/quarto-nav.js"></script>
<script src="../../../site_libs/quarto-nav/headroom.min.js"></script>
<script src="../../../site_libs/clipboard/clipboard.min.js"></script>
<script src="../../../site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="../../../site_libs/quarto-search/fuse.min.js"></script>
<script src="../../../site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="../../../">
<link href="../../../series/2023-01-24_reproducible-data-science/posts/productivity-tips.html" rel="next">
<link href="../../../series/2023-01-24_reproducible-data-science/posts/r-getting-help.html" rel="prev">
<link href="../../../assets/images/favicon.png" rel="icon" type="image/png">
<script src="../../../site_libs/quarto-html/quarto.js"></script>
<script src="../../../site_libs/quarto-html/popper.min.js"></script>
<script src="../../../site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="../../../site_libs/quarto-html/anchor.min.js"></script>
<link href="../../../site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="../../../site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="../../../site_libs/bootstrap/bootstrap.min.js"></script>
<link href="../../../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="../../../site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "navbar",
"copy-button": true,
"collapse-after": 3,
"panel-placement": "end",
"type": "textbox",
"limit": 20,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit"
}
}</script>
<link href="../../../site_libs/pagedtable-1.1/css/pagedtable.css" rel="stylesheet">
<script src="../../../site_libs/pagedtable-1.1/js/pagedtable.js"></script>
<script>window.backupDefine = window.define; window.define = undefined;</script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js"></script>
<script>document.addEventListener("DOMContentLoaded", function () {
var mathElements = document.getElementsByClassName("math");
var macros = [];
for (var i = 0; i < mathElements.length; i++) {
var texText = mathElements[i].firstChild;
if (mathElements[i].tagName == "SPAN") {
katex.render(texText.data, mathElements[i], {
displayMode: mathElements[i].classList.contains('display'),
throwOnError: false,
macros: macros,
fleqn: false
});
}}});
</script>
<script>window.define = window.backupDefine; window.backupDefine = undefined;</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
<link rel="stylesheet" href="../../../assets/styles.css">
<meta property="og:title" content="Tidy Tales">
<meta property="og:description" content="Wrangling, Visualizing, Modelling, Communicating data">
<meta property="og:image" content="https://tidytales.ca/assets/images/twittercard.png">
<meta property="og:site-name" content="Tidy Tales">
<meta property="og:image:height" content="600">
<meta property="og:image:width" content="600">
<meta name="twitter:title" content="Tidy Tales">
<meta name="twitter:description" content="Wrangling, Visualizing, Modelling, Communicating data">
<meta name="twitter:image" content="https://tidytales.ca/assets/images/twittercard.png">
<meta name="twitter:creator" content="@mccarthymg">
<meta name="twitter:site" content="@propertidytales">
<meta name="twitter:card" content="summary">
<meta name="twitter:image-height" content="600">
<meta name="twitter:image-width" content="600">
</head>
<body class="nav-sidebar floating nav-fixed">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="navbar navbar-expand-lg navbar-dark ">
<div class="navbar-container container-fluid">
<div class="navbar-brand-container">
<a href="../../../index.html" class="navbar-brand navbar-brand-logo">
<img src="../../../assets/images/logo.png" alt="" class="navbar-logo">
</a>
<a class="navbar-brand" href="../../../index.html">
<span class="navbar-title">Tidy Tales</span>
</a>
</div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav navbar-nav-scroll ms-auto">
<li class="nav-item">
<a class="nav-link" href="../../../#posts">
<span class="menu-text">Posts</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="../../../#series">
<span class="menu-text">Series</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="../../../#code-snippets">
<span class="menu-text">Snippets</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="../../../about/index.html">
<span class="menu-text">About</span></a>
</li>
<li class="nav-item compact">
<a class="nav-link" href="https://fosstodon.org/@mccarthymg"><i class="bi bi-mastodon" role="img">
</i>
<span class="menu-text"></span></a>
</li>
<li class="nav-item compact">
<a class="nav-link" href="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/mccarthy-m-g/tidytales"><i class="bi bi-github" role="img">
</i>
<span class="menu-text"></span></a>
</li>
<li class="nav-item compact">
<a class="nav-link" href="https://michaelmccarthy.tidytales.ca"><i class="bi bi-person-circle" role="img">
</i>
<span class="menu-text"></span></a>
</li>
<li class="nav-item compact">
<a class="nav-link" href="../../../index.xml"><i class="bi bi-rss-fill" role="img">
</i>
<span class="menu-text"></span></a>
</li>
</ul>
<div id="quarto-search" class="" title="Search"></div>
</div> <!-- /navcollapse -->
</div> <!-- /container-fluid -->
</nav>
<nav class="quarto-secondary-nav" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<div class="container-fluid d-flex justify-content-between">
<h1 class="quarto-secondary-nav-title">Technical Writing</h1>
<button type="button" class="quarto-btn-toggle btn" aria-label="Show secondary navigation">
<i class="bi bi-chevron-right"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<header id="title-block-header" class="quarto-title-block default toc-left page-columns page-full">
<div class="quarto-title-banner page-columns page-full">
<div class="quarto-title column-body">
<h1 class="title d-none d-lg-block">Technical Writing</h1>
<div>
<div class="description">
Technical writing and literate programming in Quarto documents.
</div>
</div>
</div>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Michael McCarthy </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">January 24, 2023</p>
</div>
</div>
</div>
</header><div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse sidebar-navigation floating overflow-auto">
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/index.html" class="sidebar-item-text sidebar-link">Reproducible Data Science</a>
</div>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="true">In this series</a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="true">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/start-here.html" class="sidebar-item-text sidebar-link">Start Here</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/system-setup.html" class="sidebar-item-text sidebar-link">System Setup</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/version-control-setup.html" class="sidebar-item-text sidebar-link">Version Control Setup</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/r-setup.html" class="sidebar-item-text sidebar-link">R Setup</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/python-setup.html" class="sidebar-item-text sidebar-link">Python Setup</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/zotero-setup.html" class="sidebar-item-text sidebar-link">Reference Manager Setup</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/learning-r.html" class="sidebar-item-text sidebar-link">Learning R</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/r-packages.html" class="sidebar-item-text sidebar-link">R Packages</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/r-getting-help.html" class="sidebar-item-text sidebar-link">Getting Help with R</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/technical-writing.html" class="sidebar-item-text sidebar-link active">Technical Writing</a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/productivity-tips.html" class="sidebar-item-text sidebar-link">Productivity Tips</a>
</div>
</li>
</ul>
</li>
</ul>
</div>
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">In this post</h2>
<ul>
<li><a href="#literate-programming" id="toc-literate-programming" class="nav-link active" data-scroll-target="#literate-programming">Literate programming</a></li>
<li><a href="#plain-text-outputs" id="toc-plain-text-outputs" class="nav-link" data-scroll-target="#plain-text-outputs">Plain text outputs</a></li>
<li><a href="#figure-and-table-outputs" id="toc-figure-and-table-outputs" class="nav-link" data-scroll-target="#figure-and-table-outputs">Figure and table outputs</a></li>
<li><a href="#equations" id="toc-equations" class="nav-link" data-scroll-target="#equations">Equations</a>
<ul class="collapse">
<li><a href="#using-inline-r-code-in-math-equations" id="toc-using-inline-r-code-in-math-equations" class="nav-link" data-scroll-target="#using-inline-r-code-in-math-equations">Using inline R code in math equations</a></li>
<li><a href="#using-math-equations-in-plots" id="toc-using-math-equations-in-plots" class="nav-link" data-scroll-target="#using-math-equations-in-plots">Using math equations in plots</a></li>
</ul></li>
</ul>
</nav>
</nav>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
</div>
<!-- main -->
<main class="content quarto-banner-title-block" id="quarto-document-content">
<section id="literate-programming" class="level2">
<h2 class="anchored" data-anchor-id="literate-programming">Literate programming</h2>
<p><strong>Literate programming</strong> is the practice of mixing text and executable code in the same document, making it possible to write reproducible documents. There are four engines for executing code in Quarto documents:</p>
<ul>
<li><a href="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/yihui/knitr">knitr</a> (supports R, Python, Julia, and more)</li>
<li><a href="https://jupyter.org">jupyter</a> (Python only)</li>
<li><a href="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/JuliaLang/IJulia.jl">IJulia</a> (Julia only)</li>
<li><a href="https://observablehq.com/">Observable JS</a> (Observable JS only)</li>
</ul>
<p>All engines support executing code in <strong>code blocks</strong> within a document, which can be used to execute code and include its output in a document. Code blocks can produce a wide variety of outputs, including plots, tabular output from data frames, and plain text. The behaviour of code execution and output can be set with Quarto’s <a href="https://quarto.org/docs/computations/execution-options.html">Execution Options</a>.</p>
<p>knitr, jupyter, and Observable JS also support executing <strong>inline code</strong> within markdown text, which can be used to allow narrative to automatically use the most up to date computations. The syntax for inline code varies across the engines.</p>
<p>The remainder of this post covers some useful literate programming practices when writing reproducible documents with the knitr engine.</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Project management
</div>
</div>
<div class="callout-body-container callout-body">
<p>Literate programming is a powerful tool for writing reproducible documents, but it can also become unwieldy if your computations require a lot of code. Rather than writing all the code within a document, it is often better to source the required code within a document, then include output using the objects created by the sourced code. The simplest way to adopt this approach is by using the <code>source()</code> function to source R script files containing the computations for a document; a better way is to use the <a href="https://books.ropensci.org/targets/literate-programming.html">literate programming</a> approach described in the <strong>targets</strong> R package.</p>
</div>
</div>
</section>
<section id="plain-text-outputs" class="level2">
<h2 class="anchored" data-anchor-id="plain-text-outputs">Plain text outputs</h2>
<p>Most code you write will be output as plain text. Depending on the purpose of a document, it might be fine to leave that output as is, or you might want to format the text, include it inline, and so forth.</p>
<p>For example, say you wanted to report the number of participants in a study you ran. Rather than writing a sentence like this:</p>
<blockquote class="blockquote">
<p>There were twelve participants in the study.</p>
</blockquote>
<p>You could get the number of participants computationally, format that number into the word “twelve”, and include it in the sentence using inline code:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>n_participants <span class="ot"><-</span> xfun<span class="sc">::</span><span class="fu">numbers_to_words</span>(<span class="dv">12</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<blockquote class="blockquote">
<p>There were <code>`r n_participants`</code> participants in the study.</p>
</blockquote>
<p>This sentence would become “There were twelve participants in the study.” when you render the document. Likewise, if the number of participants changed, the number reported in the sentence would change the next time the document rendered (as long as the R object storing the number of participants knew about the change).</p>
<p>If there are many values you need to report inline, storing them in a list is a good practice. For an overview of this approach, see:</p>
<ul>
<li><a href="https://www.tjmahr.com/lists-knitr-secret-weapon/">Lists are my secret weapon for reporting stats with knitr</a> by Tristan Mahr</li>
</ul>
</section>
<section id="figure-and-table-outputs" class="level2">
<h2 class="anchored" data-anchor-id="figure-and-table-outputs">Figure and table outputs</h2>
<p>Figures and tables produced by code blocks have unique parameters you can use to adjust their layout, add captions, or make them cross-referenceable. See the Quarto documentation for details:</p>
<ul>
<li><a href="https://quarto.org/docs/authoring/figures.html">Figures documentation</a></li>
<li><a href="https://quarto.org/docs/authoring/tables.html">Tables documentation</a></li>
</ul>
<div class="callout-tip callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Adjusting figure appearance
</div>
</div>
<div class="callout-body-container callout-body">
<p>When making figures with <strong>ggplot2</strong> it’s best to know the final size of the figure before attempting to adjust the size of plot text or geometry.</p>
</div>
</div>
</section>
<section id="equations" class="level2">
<h2 class="anchored" data-anchor-id="equations">Equations</h2>
<p>Quarto features <a href="https://quarto.org/docs/authoring/markdown-basics.html#equations">extensive support</a> for writing beautiful math equations with <a href="https://en.wikibooks.org/wiki/LaTeX/Mathematics">LaTeX math</a> expressions authored using <a href="https://pandoc.org/MANUAL.html#math">standard Pandoc markdown syntax</a>: Use <code>$</code> delimiters for inline math and <code>$$</code> delimiters for display math. Provide an <code>#eq-</code> label immediately after a display math equation to <a href="https://quarto.org/docs/authoring/cross-references.html#equations">make it referenceable</a>.</p>
<table class="table">
<colgroup>
<col style="width: 48%">
<col style="width: 45%">
</colgroup>
<thead>
<tr class="header">
<th>Markdown Syntax</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>inline math: <code>$E = mc^{2}$</code></td>
<td>inline math: <span class="math inline">E=mc^{2}</span></td>
</tr>
<tr class="even">
<td><p>display math:</p>
<p><code>$$E = mc^{2}$$</code></p></td>
<td>display math:<br>
<span class="math display">E = mc^{2}</span></td>
</tr>
<tr class="odd">
<td><p>display math with label:</p>
<p><code>$$E = mc^{2}$${#eq-mc-squared}</code></p></td>
<td>display math with label:<br>
<span id="eq-mc-squared"><span class="math display">E = mc^{2} \tag{1}</span></span></td>
</tr>
</tbody>
</table>
<p>Math expressions can be rendered in any of Quarto’s output formats, but different rendering methods are used depending on the format:</p>
<ul>
<li><code>html</code> offers <a href="https://quarto.org/docs/output-formats/html-basics.html#latex-equations">several math rendering methods</a></li>
<li><code>pdf</code> uses LaTeX (including <a href="https://quarto.org/docs/output-formats/pdf-basics.html#raw-latex">raw LaTeX</a>)</li>
<li><code>docx</code> uses Microsoft Word’s equation environment</li>
</ul>
<p>To learn more about writing LaTeX math expressions, see:</p>
<ul>
<li><a href="https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference">MathJax basic tutorial and quick reference</a><a href="#fn1" class="footnote-ref" id="fnref1" role="doc-noteref"><sup>1</sup></a></li>
<li><a href="https://tilburgsciencehub.com/building-blocks/collaborate-and-share-your-work/write-your-paper/amsmath-latex-cheatsheet/">Cheatsheet for LaTeX Math Commands</a></li>
</ul>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Inspecting equations rendered by MathJax
</div>
</div>
<div class="callout-body-container callout-body">
<p>Equations rendered by MathJax can be inspected by right clicking them. This is useful if you want to view or copy the underlying TeX code from an equation <strong>Show Math As > TeX Commands</strong> or <strong>Copy to Clipboard > TeX Commands</strong>. Note that Tidy Tales uses the KaTeX renderer, so it won’t work on this site.</p>
</div>
</div>
<section id="using-inline-r-code-in-math-equations" class="level3">
<h3 class="anchored" data-anchor-id="using-inline-r-code-in-math-equations">Using inline R code in math equations</h3>
<p>Inline R code <code>`r `</code> can be used within inline or display math to include code output in math equations.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>mtcars_fit <span class="ot"><-</span> <span class="fu">lm</span>(mpg <span class="sc">~</span> am, <span class="at">data =</span> mtcars)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>coef_int <span class="ot"><-</span> <span class="fu">coef</span>(mtcars_fit)[<span class="st">"(Intercept)"</span>]</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>coef_am <span class="ot"><-</span> <span class="fu">coef</span>(mtcars_fit)[<span class="st">"am"</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Inline math: <code>$\mathrm{\widehat{mpg}} = `r coef_int` + `r coef_am`(\mathrm{am})$</code></p>
<p>Inline math: <span class="math inline">\mathrm{\widehat{mpg}} = 17.1473684 + 7.2449393(\mathrm{am})</span></p>
<p>The same approach also works for display math.</p>
<div class="callout-tip callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Writing model equations
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <strong>equatiomatic</strong> package can be used to write equations from a fitted model. Learn more on the <a href="https://datalorax.github.io/equatiomatic/">package website</a>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(equatiomatic)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>By default the model equation uses math symbols.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">extract_eq</span>(mtcars_fit)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>#> $$
#> \operatorname{mpg} = \alpha + \beta_{1}(\operatorname{am}) + \epsilon
#> $$</code></pre>
</div>
</div>
<p>But model estimates can be used too.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">extract_eq</span>(mtcars_fit, <span class="at">use_coefs =</span> <span class="cn">TRUE</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>#> $$
#> \operatorname{\widehat{mpg}} = 17.15 + 7.24(\operatorname{am})
#> $$</code></pre>
</div>
</div>
</div>
</div>
</section>
<section id="using-math-equations-in-plots" class="level3">
<h3 class="anchored" data-anchor-id="using-math-equations-in-plots">Using math equations in plots</h3>
<p>Both base R and <strong>ggplot2</strong> plots feature support for writing beautiful math equations with <a href="https://stat.ethz.ch/R-manual/R-patched/library/grDevices/html/plotmath.html">plotmath expressions</a>. Equations can be used in plot labels, legends, and text.</p>
<p>If you would rather write equations using LaTeX math expressions, the <strong>latex2exp</strong> package can be used to parse and convert LaTeX to plotmath expressions.<a href="#fn2" class="footnote-ref" id="fnref2" role="doc-noteref"><sup>2</sup></a> Learn more on the <a href="https://www.stefanom.io/latex2exp/">package website</a>.</p>
<p>ggplot2 also includes some convenience functions for using plotmath in plot labels:</p>
<ul>
<li><code>label_parsed()</code> interprets labels as plotmath expressions</li>
<li><code>label_bquote()</code> offers a flexible way of labelling facet rows or columns with plotmath expressions</li>
</ul>
</section>
</section>
<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document" role="doc-endnotes"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p><a href="https://www.mathjax.org">MathJax</a> is an open-source JavaScript rendering engine for LaTeX math expressions. It is the default rendering method in Quarto HTML documents.<a href="#fnref1" class="footnote-back" role="doc-backlink">↩︎</a></p></li>
<li id="fn2"><p>There is also an <a href="syntax https://github.com/wilkelab/ggtext/issues/4">open issue</a> to support LaTeX math expressions using markdown in the <strong>ggtext</strong> package.<a href="#fnref2" class="footnote-back" role="doc-backlink">↩︎</a></p></li>
</ol>
</section></div></main> <!-- /main -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
var filterRegex = new RegExp(/https:\/\/tidytales\.ca/);
var isInternal = (href) => {
return filterRegex.test(href) || localhostRegex.test(href);
}
// Inspect non-navigation links and adorn them if external
var links = window.document.querySelectorAll('a:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item)');
for (var i=0; i<links.length; i++) {
const link = links[i];
if (!isInternal(link.href)) {
// target, if specified
link.setAttribute("target", "_blank");
}
}
});
</script>
<nav class="page-navigation">
<div class="nav-page nav-page-previous">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/r-getting-help.html" class="pagination-link">
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text">Getting Help with R</span>
</a>
</div>
<div class="nav-page nav-page-next">
<a href="../../../series/2023-01-24_reproducible-data-science/posts/productivity-tips.html" class="pagination-link">
<span class="nav-page-text">Productivity Tips</span> <i class="bi bi-arrow-right-short"></i>
</a>
</div>
</nav>
</div> <!-- /content -->
<footer class="footer">
<div class="nav-footer">
<div class="nav-footer-center">© Copyright 2023 <a href="https://michaelmccarthy.tidytales.ca">Michael McCarthy</a></div>
</div>
</footer>
</body></html>