-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror-handling.html
More file actions
744 lines (703 loc) · 74.6 KB
/
Copy patherror-handling.html
File metadata and controls
744 lines (703 loc) · 74.6 KB
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>7 Error Handling | Best Coding Practices in R</title>
<meta name="description" content="This book provides a comprehensive guide to best coding practices in R, focusing on essential aspects like script organisation, naming conventions, commenting, and version control. It aims to enhance the readability, maintainability, and collaborative potential of R code by offering practical advice on creating well-structured and clean scripts." />
<meta name="generator" content="bookdown 0.40 and GitBook 2.6.7" />
<meta property="og:title" content="7 Error Handling | Best Coding Practices in R" />
<meta property="og:type" content="book" />
<meta property="og:description" content="This book provides a comprehensive guide to best coding practices in R, focusing on essential aspects like script organisation, naming conventions, commenting, and version control. It aims to enhance the readability, maintainability, and collaborative potential of R code by offering practical advice on creating well-structured and clean scripts." />
<meta name="github-repo" content="rstudio/bookdown-demo" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="7 Error Handling | Best Coding Practices in R" />
<meta name="twitter:description" content="This book provides a comprehensive guide to best coding practices in R, focusing on essential aspects like script organisation, naming conventions, commenting, and version control. It aims to enhance the readability, maintainability, and collaborative potential of R code by offering practical advice on creating well-structured and clean scripts." />
<meta name="author" content="Daniel Hammocks, Senior Data Scientist at Mayor’s Office for Policing and Crime" />
<meta name="date" content="2024-08-28" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="writing-functions.html"/>
<link rel="next" href="version-control-with-git.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
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>
<style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="toc-logo"><a href="./"><img src="images/MOPAC-DS-Square.png"></a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Preface</a>
<ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#purpose-of-this-book"><i class="fa fa-check"></i>Purpose of this Book</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#about-the-author"><i class="fa fa-check"></i>About the Author</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="introduction.html"><a href="introduction.html"><i class="fa fa-check"></i><b>1</b> Introduction</a>
<ul>
<li class="chapter" data-level="1.1" data-path="introduction.html"><a href="introduction.html#why-coding-practices-matter"><i class="fa fa-check"></i><b>1.1</b> Why Coding Practices Matter</a></li>
<li class="chapter" data-level="1.2" data-path="introduction.html"><a href="introduction.html#what-this-book-covers"><i class="fa fa-check"></i><b>1.2</b> What This Book Covers</a></li>
<li class="chapter" data-level="1.3" data-path="introduction.html"><a href="introduction.html#how-to-use-this-book"><i class="fa fa-check"></i><b>1.3</b> How to Use This Book</a></li>
<li class="chapter" data-level="1.4" data-path="introduction.html"><a href="introduction.html#who-this-book-is-for"><i class="fa fa-check"></i><b>1.4</b> Who This Book Is For</a></li>
<li class="chapter" data-level="1.5" data-path="introduction.html"><a href="introduction.html#final-thoughts"><i class="fa fa-check"></i><b>1.5</b> Final Thoughts</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="naming-conventions.html"><a href="naming-conventions.html"><i class="fa fa-check"></i><b>2</b> Naming Conventions</a>
<ul>
<li class="chapter" data-level="2.1" data-path="naming-conventions.html"><a href="naming-conventions.html#naming-scripts"><i class="fa fa-check"></i><b>2.1</b> Naming Scripts</a>
<ul>
<li class="chapter" data-level="2.1.1" data-path="naming-conventions.html"><a href="naming-conventions.html#best-practices-for-script-names"><i class="fa fa-check"></i><b>2.1.1</b> Best Practices for Script Names</a></li>
<li class="chapter" data-level="2.1.2" data-path="naming-conventions.html"><a href="naming-conventions.html#example-script-names"><i class="fa fa-check"></i><b>2.1.2</b> Example Script Names</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="naming-conventions.html"><a href="naming-conventions.html#naming-variables"><i class="fa fa-check"></i><b>2.2</b> Naming Variables</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="naming-conventions.html"><a href="naming-conventions.html#guidelines-for-variable-names"><i class="fa fa-check"></i><b>2.2.1</b> Guidelines for Variable Names</a></li>
<li class="chapter" data-level="2.2.2" data-path="naming-conventions.html"><a href="naming-conventions.html#example-variable-names"><i class="fa fa-check"></i><b>2.2.2</b> Example Variable Names</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="naming-conventions.html"><a href="naming-conventions.html#naming-functions"><i class="fa fa-check"></i><b>2.3</b> Naming Functions</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="naming-conventions.html"><a href="naming-conventions.html#best-practices-for-function-names"><i class="fa fa-check"></i><b>2.3.1</b> Best Practices for Function Names</a></li>
<li class="chapter" data-level="2.3.2" data-path="naming-conventions.html"><a href="naming-conventions.html#example-function-names"><i class="fa fa-check"></i><b>2.3.2</b> Example Function Names</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="naming-conventions.html"><a href="naming-conventions.html#consistency-is-key"><i class="fa fa-check"></i><b>2.4</b> Consistency is Key</a></li>
<li class="chapter" data-level="2.5" data-path="naming-conventions.html"><a href="naming-conventions.html#summary"><i class="fa fa-check"></i><b>2.5</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="organising-scripts.html"><a href="organising-scripts.html"><i class="fa fa-check"></i><b>3</b> Organising Scripts</a>
<ul>
<li class="chapter" data-level="3.1" data-path="organising-scripts.html"><a href="organising-scripts.html#structuring-scripts"><i class="fa fa-check"></i><b>3.1</b> Structuring Scripts</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="organising-scripts.html"><a href="organising-scripts.html#logical-script-structure"><i class="fa fa-check"></i><b>3.1.1</b> Logical Script Structure</a></li>
<li class="chapter" data-level="3.1.2" data-path="organising-scripts.html"><a href="organising-scripts.html#using-functions-for-modularity"><i class="fa fa-check"></i><b>3.1.2</b> Using Functions for Modularity</a></li>
<li class="chapter" data-level="3.1.3" data-path="organising-scripts.html"><a href="organising-scripts.html#script-length"><i class="fa fa-check"></i><b>3.1.3</b> Script Length</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="organising-scripts.html"><a href="organising-scripts.html#organising-files-and-directories"><i class="fa fa-check"></i><b>3.2</b> Organising Files and Directories</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="organising-scripts.html"><a href="organising-scripts.html#directory-structure"><i class="fa fa-check"></i><b>3.2.1</b> Directory Structure</a></li>
<li class="chapter" data-level="3.2.2" data-path="organising-scripts.html"><a href="organising-scripts.html#data-files"><i class="fa fa-check"></i><b>3.2.2</b> Data Files</a></li>
<li class="chapter" data-level="3.2.3" data-path="organising-scripts.html"><a href="organising-scripts.html#scripts"><i class="fa fa-check"></i><b>3.2.3</b> Scripts</a></li>
<li class="chapter" data-level="3.2.4" data-path="organising-scripts.html"><a href="organising-scripts.html#output-files"><i class="fa fa-check"></i><b>3.2.4</b> Output Files</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="organising-scripts.html"><a href="organising-scripts.html#documentation"><i class="fa fa-check"></i><b>3.3</b> Documentation</a>
<ul>
<li class="chapter" data-level="3.3.1" data-path="organising-scripts.html"><a href="organising-scripts.html#readme-files"><i class="fa fa-check"></i><b>3.3.1</b> README Files</a></li>
<li class="chapter" data-level="3.3.2" data-path="organising-scripts.html"><a href="organising-scripts.html#inline-documentation"><i class="fa fa-check"></i><b>3.3.2</b> Inline Documentation</a></li>
</ul></li>
<li class="chapter" data-level="3.4" data-path="organising-scripts.html"><a href="organising-scripts.html#summary-1"><i class="fa fa-check"></i><b>3.4</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="commenting-code.html"><a href="commenting-code.html"><i class="fa fa-check"></i><b>4</b> Commenting Code</a>
<ul>
<li class="chapter" data-level="4.1" data-path="commenting-code.html"><a href="commenting-code.html#the-purpose-of-comments"><i class="fa fa-check"></i><b>4.1</b> The Purpose of Comments</a>
<ul>
<li class="chapter" data-level="4.1.1" data-path="commenting-code.html"><a href="commenting-code.html#why-comment-your-code"><i class="fa fa-check"></i><b>4.1.1</b> Why Comment Your Code?</a></li>
<li class="chapter" data-level="4.1.2" data-path="commenting-code.html"><a href="commenting-code.html#when-to-comment"><i class="fa fa-check"></i><b>4.1.2</b> When to Comment</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="commenting-code.html"><a href="commenting-code.html#best-practices-for-commenting"><i class="fa fa-check"></i><b>4.2</b> Best Practices for Commenting</a>
<ul>
<li class="chapter" data-level="4.2.1" data-path="commenting-code.html"><a href="commenting-code.html#writing-clear-and-concise-comments"><i class="fa fa-check"></i><b>4.2.1</b> Writing Clear and Concise Comments</a></li>
<li class="chapter" data-level="4.2.2" data-path="commenting-code.html"><a href="commenting-code.html#comment-placement"><i class="fa fa-check"></i><b>4.2.2</b> Comment Placement</a></li>
<li class="chapter" data-level="4.2.3" data-path="commenting-code.html"><a href="commenting-code.html#consistency-in-commenting"><i class="fa fa-check"></i><b>4.2.3</b> Consistency in Commenting</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="commenting-code.html"><a href="commenting-code.html#common-pitfalls-to-avoid"><i class="fa fa-check"></i><b>4.3</b> Common Pitfalls to Avoid</a>
<ul>
<li class="chapter" data-level="4.3.1" data-path="commenting-code.html"><a href="commenting-code.html#over-commenting"><i class="fa fa-check"></i><b>4.3.1</b> Over-Commenting</a></li>
<li class="chapter" data-level="4.3.2" data-path="commenting-code.html"><a href="commenting-code.html#commenting-out-code"><i class="fa fa-check"></i><b>4.3.2</b> Commenting Out Code</a></li>
<li class="chapter" data-level="4.3.3" data-path="commenting-code.html"><a href="commenting-code.html#vague-or-uninformative-comments"><i class="fa fa-check"></i><b>4.3.3</b> Vague or Uninformative Comments</a></li>
</ul></li>
<li class="chapter" data-level="4.4" data-path="commenting-code.html"><a href="commenting-code.html#advanced-commenting-techniques"><i class="fa fa-check"></i><b>4.4</b> Advanced Commenting Techniques</a>
<ul>
<li class="chapter" data-level="4.4.1" data-path="commenting-code.html"><a href="commenting-code.html#using-roxygen2-for-documentation"><i class="fa fa-check"></i><b>4.4.1</b> Using Roxygen2 for Documentation</a></li>
<li class="chapter" data-level="4.4.2" data-path="commenting-code.html"><a href="commenting-code.html#writing-to-do-comments"><i class="fa fa-check"></i><b>4.4.2</b> Writing “To-Do” Comments</a></li>
</ul></li>
<li class="chapter" data-level="4.5" data-path="commenting-code.html"><a href="commenting-code.html#summary-2"><i class="fa fa-check"></i><b>4.5</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html"><i class="fa fa-check"></i><b>5</b> Code Syntax and Spacing</a>
<ul>
<li class="chapter" data-level="5.1" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#importance-of-consistent-syntax"><i class="fa fa-check"></i><b>5.1</b> Importance of Consistent Syntax</a>
<ul>
<li class="chapter" data-level="5.1.1" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#why-syntax-matters"><i class="fa fa-check"></i><b>5.1.1</b> Why Syntax Matters</a></li>
<li class="chapter" data-level="5.1.2" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#adopting-a-style-guide"><i class="fa fa-check"></i><b>5.1.2</b> Adopting a Style Guide</a></li>
</ul></li>
<li class="chapter" data-level="5.2" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#indentation"><i class="fa fa-check"></i><b>5.2</b> Indentation</a>
<ul>
<li class="chapter" data-level="5.2.1" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#standard-indentation-practices"><i class="fa fa-check"></i><b>5.2.1</b> Standard Indentation Practices</a></li>
<li class="chapter" data-level="5.2.2" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#indentation-in-functions"><i class="fa fa-check"></i><b>5.2.2</b> Indentation in Functions</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#spacing"><i class="fa fa-check"></i><b>5.3</b> Spacing</a>
<ul>
<li class="chapter" data-level="5.3.1" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#spacing-around-operators"><i class="fa fa-check"></i><b>5.3.1</b> Spacing Around Operators</a></li>
<li class="chapter" data-level="5.3.2" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#spacing-after-commas-and-colons"><i class="fa fa-check"></i><b>5.3.2</b> Spacing After Commas and Colons</a></li>
<li class="chapter" data-level="5.3.3" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#spacing-in-function-calls"><i class="fa fa-check"></i><b>5.3.3</b> Spacing in Function Calls</a></li>
<li class="chapter" data-level="5.3.4" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#spacing-in-conditional-statements"><i class="fa fa-check"></i><b>5.3.4</b> Spacing in Conditional Statements</a></li>
</ul></li>
<li class="chapter" data-level="5.4" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#line-breaks"><i class="fa fa-check"></i><b>5.4</b> Line Breaks</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#breaking-long-lines"><i class="fa fa-check"></i><b>5.4.1</b> Breaking Long Lines</a></li>
<li class="chapter" data-level="5.4.2" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#avoiding-deep-nesting"><i class="fa fa-check"></i><b>5.4.2</b> Avoiding Deep Nesting</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#aligning-code"><i class="fa fa-check"></i><b>5.5</b> Aligning Code</a>
<ul>
<li class="chapter" data-level="5.5.1" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#aligning-assignments"><i class="fa fa-check"></i><b>5.5.1</b> Aligning Assignments</a></li>
<li class="chapter" data-level="5.5.2" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#aligning-function-arguments"><i class="fa fa-check"></i><b>5.5.2</b> Aligning Function Arguments</a></li>
</ul></li>
<li class="chapter" data-level="5.6" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#comment-placement-and-spacing"><i class="fa fa-check"></i><b>5.6</b> Comment Placement and Spacing</a>
<ul>
<li class="chapter" data-level="5.6.1" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#spacing-before-comments"><i class="fa fa-check"></i><b>5.6.1</b> Spacing Before Comments</a></li>
<li class="chapter" data-level="5.6.2" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#block-comment-formatting"><i class="fa fa-check"></i><b>5.6.2</b> Block Comment Formatting</a></li>
</ul></li>
<li class="chapter" data-level="5.7" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#avoiding-common-pitfalls"><i class="fa fa-check"></i><b>5.7</b> Avoiding Common Pitfalls</a>
<ul>
<li class="chapter" data-level="5.7.1" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#overuse-of-blank-lines"><i class="fa fa-check"></i><b>5.7.1</b> Overuse of Blank Lines</a></li>
<li class="chapter" data-level="5.7.2" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#avoiding-excessive-line-length"><i class="fa fa-check"></i><b>5.7.2</b> Avoiding Excessive Line Length</a></li>
</ul></li>
<li class="chapter" data-level="5.8" data-path="code-syntax-and-spacing.html"><a href="code-syntax-and-spacing.html#summary-3"><i class="fa fa-check"></i><b>5.8</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="writing-functions.html"><a href="writing-functions.html"><i class="fa fa-check"></i><b>6</b> Writing Functions</a>
<ul>
<li class="chapter" data-level="6.1" data-path="writing-functions.html"><a href="writing-functions.html#the-purpose-of-functions"><i class="fa fa-check"></i><b>6.1</b> The Purpose of Functions</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="writing-functions.html"><a href="writing-functions.html#why-write-functions"><i class="fa fa-check"></i><b>6.1.1</b> Why Write Functions?</a></li>
<li class="chapter" data-level="6.1.2" data-path="writing-functions.html"><a href="writing-functions.html#when-to-write-a-function"><i class="fa fa-check"></i><b>6.1.2</b> When to Write a Function</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="writing-functions.html"><a href="writing-functions.html#structure-of-a-function"><i class="fa fa-check"></i><b>6.2</b> Structure of a Function</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="writing-functions.html"><a href="writing-functions.html#basic-function-structure"><i class="fa fa-check"></i><b>6.2.1</b> Basic Function Structure</a></li>
<li class="chapter" data-level="6.2.2" data-path="writing-functions.html"><a href="writing-functions.html#naming-conventions-1"><i class="fa fa-check"></i><b>6.2.2</b> Naming Conventions</a></li>
<li class="chapter" data-level="6.2.3" data-path="writing-functions.html"><a href="writing-functions.html#function-arguments"><i class="fa fa-check"></i><b>6.2.3</b> Function Arguments</a></li>
<li class="chapter" data-level="6.2.4" data-path="writing-functions.html"><a href="writing-functions.html#function-body"><i class="fa fa-check"></i><b>6.2.4</b> Function Body</a></li>
<li class="chapter" data-level="6.2.5" data-path="writing-functions.html"><a href="writing-functions.html#return-values"><i class="fa fa-check"></i><b>6.2.5</b> Return Values</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="writing-functions.html"><a href="writing-functions.html#documentation-and-commenting"><i class="fa fa-check"></i><b>6.3</b> Documentation and Commenting</a>
<ul>
<li class="chapter" data-level="6.3.1" data-path="writing-functions.html"><a href="writing-functions.html#documenting-functions-with-roxygen2"><i class="fa fa-check"></i><b>6.3.1</b> Documenting Functions with Roxygen2</a></li>
<li class="chapter" data-level="6.3.2" data-path="writing-functions.html"><a href="writing-functions.html#commenting-inside-functions"><i class="fa fa-check"></i><b>6.3.2</b> Commenting Inside Functions</a></li>
</ul></li>
<li class="chapter" data-level="6.4" data-path="writing-functions.html"><a href="writing-functions.html#testing-functions"><i class="fa fa-check"></i><b>6.4</b> Testing Functions</a>
<ul>
<li class="chapter" data-level="6.4.1" data-path="writing-functions.html"><a href="writing-functions.html#why-test-functions"><i class="fa fa-check"></i><b>6.4.1</b> Why Test Functions?</a></li>
<li class="chapter" data-level="6.4.2" data-path="writing-functions.html"><a href="writing-functions.html#writing-simple-tests"><i class="fa fa-check"></i><b>6.4.2</b> Writing Simple Tests</a></li>
<li class="chapter" data-level="6.4.3" data-path="writing-functions.html"><a href="writing-functions.html#automated-testing-with-testthat"><i class="fa fa-check"></i><b>6.4.3</b> Automated Testing with <code>testthat</code></a></li>
</ul></li>
<li class="chapter" data-level="6.5" data-path="writing-functions.html"><a href="writing-functions.html#advanced-function-techniques"><i class="fa fa-check"></i><b>6.5</b> Advanced Function Techniques</a>
<ul>
<li class="chapter" data-level="6.5.1" data-path="writing-functions.html"><a href="writing-functions.html#function-factories"><i class="fa fa-check"></i><b>6.5.1</b> Function Factories</a></li>
<li class="chapter" data-level="6.5.2" data-path="writing-functions.html"><a href="writing-functions.html#closures"><i class="fa fa-check"></i><b>6.5.2</b> Closures</a></li>
<li class="chapter" data-level="6.5.3" data-path="writing-functions.html"><a href="writing-functions.html#vectorised-functions"><i class="fa fa-check"></i><b>6.5.3</b> Vectorised Functions</a></li>
<li class="chapter" data-level="6.5.4" data-path="writing-functions.html"><a href="writing-functions.html#anonymous-functions"><i class="fa fa-check"></i><b>6.5.4</b> Anonymous Functions</a></li>
</ul></li>
<li class="chapter" data-level="6.6" data-path="writing-functions.html"><a href="writing-functions.html#avoiding-common-pitfalls-in-function-writing"><i class="fa fa-check"></i><b>6.6</b> Avoiding Common Pitfalls in Function Writing</a>
<ul>
<li class="chapter" data-level="6.6.1" data-path="writing-functions.html"><a href="writing-functions.html#overloading-functions"><i class="fa fa-check"></i><b>6.6.1</b> Overloading Functions</a></li>
<li class="chapter" data-level="6.6.2" data-path="writing-functions.html"><a href="writing-functions.html#handling-errors-gracefully"><i class="fa fa-check"></i><b>6.6.2</b> Handling Errors Gracefully</a></li>
<li class="chapter" data-level="6.6.3" data-path="writing-functions.html"><a href="writing-functions.html#avoiding-side-effects"><i class="fa fa-check"></i><b>6.6.3</b> Avoiding Side Effects</a></li>
</ul></li>
<li class="chapter" data-level="6.7" data-path="writing-functions.html"><a href="writing-functions.html#summary-4"><i class="fa fa-check"></i><b>6.7</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="error-handling.html"><a href="error-handling.html"><i class="fa fa-check"></i><b>7</b> Error Handling</a>
<ul>
<li class="chapter" data-level="7.1" data-path="error-handling.html"><a href="error-handling.html#introduction-to-error-handling"><i class="fa fa-check"></i><b>7.1</b> Introduction to Error Handling</a>
<ul>
<li class="chapter" data-level="7.1.1" data-path="error-handling.html"><a href="error-handling.html#what-is-error-handling"><i class="fa fa-check"></i><b>7.1.1</b> What is Error Handling?</a></li>
<li class="chapter" data-level="7.1.2" data-path="error-handling.html"><a href="error-handling.html#types-of-errors-in-r"><i class="fa fa-check"></i><b>7.1.2</b> Types of Errors in R</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="error-handling.html"><a href="error-handling.html#basic-error-handling-with-try-and-trycatch"><i class="fa fa-check"></i><b>7.2</b> Basic Error Handling with <code>try</code> and <code>tryCatch</code></a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="error-handling.html"><a href="error-handling.html#using-try"><i class="fa fa-check"></i><b>7.2.1</b> Using <code>try</code></a></li>
<li class="chapter" data-level="7.2.2" data-path="error-handling.html"><a href="error-handling.html#using-trycatch"><i class="fa fa-check"></i><b>7.2.2</b> Using <code>tryCatch</code></a></li>
<li class="chapter" data-level="7.2.3" data-path="error-handling.html"><a href="error-handling.html#returning-values-from-trycatch"><i class="fa fa-check"></i><b>7.2.3</b> Returning Values from <code>tryCatch</code></a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="error-handling.html"><a href="error-handling.html#custom-error-messages-with-stop-warning-and-message"><i class="fa fa-check"></i><b>7.3</b> Custom Error Messages with <code>stop</code>, <code>warning</code>, and <code>message</code></a>
<ul>
<li class="chapter" data-level="7.3.1" data-path="error-handling.html"><a href="error-handling.html#using-stop-for-critical-errors"><i class="fa fa-check"></i><b>7.3.1</b> Using <code>stop</code> for Critical Errors</a></li>
<li class="chapter" data-level="7.3.2" data-path="error-handling.html"><a href="error-handling.html#using-warning-for-non-critical-issues"><i class="fa fa-check"></i><b>7.3.2</b> Using <code>warning</code> for Non-Critical Issues</a></li>
<li class="chapter" data-level="7.3.3" data-path="error-handling.html"><a href="error-handling.html#using-message-for-informational-messages"><i class="fa fa-check"></i><b>7.3.3</b> Using <code>message</code> for Informational Messages</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="error-handling.html"><a href="error-handling.html#defensive-programming"><i class="fa fa-check"></i><b>7.4</b> Defensive Programming</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="error-handling.html"><a href="error-handling.html#input-validation"><i class="fa fa-check"></i><b>7.4.1</b> Input Validation</a></li>
<li class="chapter" data-level="7.4.2" data-path="error-handling.html"><a href="error-handling.html#asserting-conditions"><i class="fa fa-check"></i><b>7.4.2</b> Asserting Conditions</a></li>
<li class="chapter" data-level="7.4.3" data-path="error-handling.html"><a href="error-handling.html#handling-edge-cases"><i class="fa fa-check"></i><b>7.4.3</b> Handling Edge Cases</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="error-handling.html"><a href="error-handling.html#best-practices-for-error-handling"><i class="fa fa-check"></i><b>7.5</b> Best Practices for Error Handling</a>
<ul>
<li class="chapter" data-level="7.5.1" data-path="error-handling.html"><a href="error-handling.html#provide-clear-and-informative-messages"><i class="fa fa-check"></i><b>7.5.1</b> Provide Clear and Informative Messages</a></li>
<li class="chapter" data-level="7.5.2" data-path="error-handling.html"><a href="error-handling.html#use-trycatch-for-anticipated-errors"><i class="fa fa-check"></i><b>7.5.2</b> Use <code>tryCatch</code> for Anticipated Errors</a></li>
<li class="chapter" data-level="7.5.3" data-path="error-handling.html"><a href="error-handling.html#log-errors-in-production-code"><i class="fa fa-check"></i><b>7.5.3</b> Log Errors in Production Code</a></li>
<li class="chapter" data-level="7.5.4" data-path="error-handling.html"><a href="error-handling.html#test-error-handling"><i class="fa fa-check"></i><b>7.5.4</b> Test Error Handling</a></li>
</ul></li>
<li class="chapter" data-level="7.6" data-path="error-handling.html"><a href="error-handling.html#summary-5"><i class="fa fa-check"></i><b>7.6</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="version-control-with-git.html"><a href="version-control-with-git.html"><i class="fa fa-check"></i><b>8</b> Version Control with Git</a>
<ul>
<li class="chapter" data-level="8.1" data-path="version-control-with-git.html"><a href="version-control-with-git.html#introduction-to-git"><i class="fa fa-check"></i><b>8.1</b> Introduction to Git</a>
<ul>
<li class="chapter" data-level="8.1.1" data-path="version-control-with-git.html"><a href="version-control-with-git.html#benefits-of-using-git"><i class="fa fa-check"></i><b>8.1.1</b> Benefits of Using Git</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="version-control-with-git.html"><a href="version-control-with-git.html#setting-up-git"><i class="fa fa-check"></i><b>8.2</b> Setting Up Git</a>
<ul>
<li class="chapter" data-level="8.2.1" data-path="version-control-with-git.html"><a href="version-control-with-git.html#installing-git"><i class="fa fa-check"></i><b>8.2.1</b> Installing Git</a></li>
<li class="chapter" data-level="8.2.2" data-path="version-control-with-git.html"><a href="version-control-with-git.html#configuring-git"><i class="fa fa-check"></i><b>8.2.2</b> Configuring Git</a></li>
</ul></li>
<li class="chapter" data-level="8.3" data-path="version-control-with-git.html"><a href="version-control-with-git.html#basic-git-workflow"><i class="fa fa-check"></i><b>8.3</b> Basic Git Workflow</a>
<ul>
<li class="chapter" data-level="8.3.1" data-path="version-control-with-git.html"><a href="version-control-with-git.html#initialising-a-repository"><i class="fa fa-check"></i><b>8.3.1</b> Initialising a Repository</a></li>
<li class="chapter" data-level="8.3.2" data-path="version-control-with-git.html"><a href="version-control-with-git.html#staging-and-committing-changes"><i class="fa fa-check"></i><b>8.3.2</b> Staging and Committing Changes</a></li>
<li class="chapter" data-level="8.3.3" data-path="version-control-with-git.html"><a href="version-control-with-git.html#viewing-repository-status"><i class="fa fa-check"></i><b>8.3.3</b> Viewing Repository Status</a></li>
<li class="chapter" data-level="8.3.4" data-path="version-control-with-git.html"><a href="version-control-with-git.html#viewing-commit-history"><i class="fa fa-check"></i><b>8.3.4</b> Viewing Commit History</a></li>
</ul></li>
<li class="chapter" data-level="8.4" data-path="version-control-with-git.html"><a href="version-control-with-git.html#branching-and-merging"><i class="fa fa-check"></i><b>8.4</b> Branching and Merging</a>
<ul>
<li class="chapter" data-level="8.4.1" data-path="version-control-with-git.html"><a href="version-control-with-git.html#creating-and-switching-branches"><i class="fa fa-check"></i><b>8.4.1</b> Creating and Switching Branches</a></li>
<li class="chapter" data-level="8.4.2" data-path="version-control-with-git.html"><a href="version-control-with-git.html#merging-branches"><i class="fa fa-check"></i><b>8.4.2</b> Merging Branches</a></li>
<li class="chapter" data-level="8.4.3" data-path="version-control-with-git.html"><a href="version-control-with-git.html#resolving-merge-conflicts"><i class="fa fa-check"></i><b>8.4.3</b> Resolving Merge Conflicts</a></li>
</ul></li>
<li class="chapter" data-level="8.5" data-path="version-control-with-git.html"><a href="version-control-with-git.html#working-with-remote-repositories"><i class="fa fa-check"></i><b>8.5</b> Working with Remote Repositories</a>
<ul>
<li class="chapter" data-level="8.5.1" data-path="version-control-with-git.html"><a href="version-control-with-git.html#cloning-a-repository"><i class="fa fa-check"></i><b>8.5.1</b> Cloning a Repository</a></li>
<li class="chapter" data-level="8.5.2" data-path="version-control-with-git.html"><a href="version-control-with-git.html#before-starting-work-ensure-your-local-repository-is-up-to-date-by-pulling-the-latest-changes"><i class="fa fa-check"></i><b>8.5.2</b> Before starting work, ensure your local repository is up to date by pulling the latest changes:</a></li>
<li class="chapter" data-level="8.5.3" data-path="version-control-with-git.html"><a href="version-control-with-git.html#pushing-changes"><i class="fa fa-check"></i><b>8.5.3</b> Pushing Changes</a></li>
</ul></li>
<li class="chapter" data-level="8.6" data-path="version-control-with-git.html"><a href="version-control-with-git.html#advanced-git-features"><i class="fa fa-check"></i><b>8.6</b> Advanced Git Features</a>
<ul>
<li class="chapter" data-level="8.6.1" data-path="version-control-with-git.html"><a href="version-control-with-git.html#stashing-changes"><i class="fa fa-check"></i><b>8.6.1</b> Stashing Changes</a></li>
<li class="chapter" data-level="8.6.2" data-path="version-control-with-git.html"><a href="version-control-with-git.html#rebasing"><i class="fa fa-check"></i><b>8.6.2</b> Rebasing</a></li>
<li class="chapter" data-level="8.6.3" data-path="version-control-with-git.html"><a href="version-control-with-git.html#tags"><i class="fa fa-check"></i><b>8.6.3</b> Tags</a></li>
</ul></li>
<li class="chapter" data-level="8.7" data-path="version-control-with-git.html"><a href="version-control-with-git.html#git-in-rstudio"><i class="fa fa-check"></i><b>8.7</b> Git in RStudio</a>
<ul>
<li class="chapter" data-level="8.7.1" data-path="version-control-with-git.html"><a href="version-control-with-git.html#setting-up-git-in-rstudio"><i class="fa fa-check"></i><b>8.7.1</b> Setting Up Git in RStudio</a></li>
<li class="chapter" data-level="8.7.2" data-path="version-control-with-git.html"><a href="version-control-with-git.html#using-git-in-rstudio"><i class="fa fa-check"></i><b>8.7.2</b> Using Git in RStudio</a></li>
</ul></li>
<li class="chapter" data-level="8.8" data-path="version-control-with-git.html"><a href="version-control-with-git.html#best-practices-for-using-git"><i class="fa fa-check"></i><b>8.8</b> Best Practices for Using Git</a></li>
<li class="chapter" data-level="8.9" data-path="version-control-with-git.html"><a href="version-control-with-git.html#summary-6"><i class="fa fa-check"></i><b>8.9</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html"><i class="fa fa-check"></i><b>9</b> Reproducibility and Documentation</a>
<ul>
<li class="chapter" data-level="9.1" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#introduction-to-reproducibility"><i class="fa fa-check"></i><b>9.1</b> Introduction to Reproducibility</a>
<ul>
<li class="chapter" data-level="9.1.1" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#what-is-reproducibility"><i class="fa fa-check"></i><b>9.1.1</b> What is Reproducibility?</a></li>
<li class="chapter" data-level="9.1.2" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#challenges-to-reproducibility"><i class="fa fa-check"></i><b>9.1.2</b> Challenges to Reproducibility</a></li>
</ul></li>
<li class="chapter" data-level="9.2" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#setting-up-a-reproducible-environment"><i class="fa fa-check"></i><b>9.2</b> Setting Up a Reproducible Environment</a>
<ul>
<li class="chapter" data-level="9.2.1" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#managing-r-and-package-versions"><i class="fa fa-check"></i><b>9.2.1</b> Managing R and Package Versions</a></li>
<li class="chapter" data-level="9.2.2" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#using-rmarkdown-for-reproducible-reports"><i class="fa fa-check"></i><b>9.2.2</b> Using RMarkdown for Reproducible Reports</a></li>
<li class="chapter" data-level="9.2.3" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#version-control-with-git-1"><i class="fa fa-check"></i><b>9.2.3</b> Version Control with Git</a></li>
</ul></li>
<li class="chapter" data-level="9.3" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#sharing-your-work"><i class="fa fa-check"></i><b>9.3</b> Sharing Your Work</a>
<ul>
<li class="chapter" data-level="9.3.1" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#sharing-code-and-data"><i class="fa fa-check"></i><b>9.3.1</b> Sharing Code and Data</a></li>
<li class="chapter" data-level="9.3.2" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#creating-reproducible-examples"><i class="fa fa-check"></i><b>9.3.2</b> Creating Reproducible Examples</a></li>
</ul></li>
<li class="chapter" data-level="9.4" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#documentation-for-reproducibility"><i class="fa fa-check"></i><b>9.4</b> Documentation for Reproducibility</a>
<ul>
<li class="chapter" data-level="9.4.1" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#documenting-data"><i class="fa fa-check"></i><b>9.4.1</b> Documenting Data</a></li>
<li class="chapter" data-level="9.4.2" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#writing-a-readme-file"><i class="fa fa-check"></i><b>9.4.2</b> Writing a README File</a></li>
<li class="chapter" data-level="9.4.3" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#creating-a-reproducibility-checklist"><i class="fa fa-check"></i><b>9.4.3</b> Creating a Reproducibility Checklist</a></li>
</ul></li>
<li class="chapter" data-level="9.5" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#case-study-reproducible-analysis-in-r"><i class="fa fa-check"></i><b>9.5</b> Case Study: Reproducible Analysis in R</a>
<ul>
<li class="chapter" data-level="9.5.1" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#overview"><i class="fa fa-check"></i><b>9.5.1</b> Overview</a></li>
<li class="chapter" data-level="9.5.2" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#setting-up-the-project"><i class="fa fa-check"></i><b>9.5.2</b> Setting Up the Project</a></li>
<li class="chapter" data-level="9.5.3" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#documenting-the-analysis"><i class="fa fa-check"></i><b>9.5.3</b> Documenting the Analysis</a></li>
<li class="chapter" data-level="9.5.4" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#sharing-the-project"><i class="fa fa-check"></i><b>9.5.4</b> Sharing the Project</a></li>
<li class="chapter" data-level="9.5.5" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#running-the-analysis"><i class="fa fa-check"></i><b>9.5.5</b> Running the Analysis</a></li>
</ul></li>
<li class="chapter" data-level="9.6" data-path="reproducibility-and-documentation.html"><a href="reproducibility-and-documentation.html#summary-7"><i class="fa fa-check"></i><b>9.6</b> Summary</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="conclusion.html"><a href="conclusion.html"><i class="fa fa-check"></i><b>10</b> Conclusion</a>
<ul>
<li class="chapter" data-level="10.1" data-path="conclusion.html"><a href="conclusion.html#recap-of-key-concepts"><i class="fa fa-check"></i><b>10.1</b> Recap of Key Concepts</a>
<ul>
<li class="chapter" data-level="10.1.1" data-path="conclusion.html"><a href="conclusion.html#introduction-1"><i class="fa fa-check"></i><b>10.1.1</b> Introduction</a></li>
<li class="chapter" data-level="10.1.2" data-path="conclusion.html"><a href="conclusion.html#naming-conventions-2"><i class="fa fa-check"></i><b>10.1.2</b> Naming Conventions</a></li>
<li class="chapter" data-level="10.1.3" data-path="conclusion.html"><a href="conclusion.html#organising-scripts-1"><i class="fa fa-check"></i><b>10.1.3</b> Organising Scripts</a></li>
<li class="chapter" data-level="10.1.4" data-path="conclusion.html"><a href="conclusion.html#commenting-code-1"><i class="fa fa-check"></i><b>10.1.4</b> Commenting Code</a></li>
<li class="chapter" data-level="10.1.5" data-path="conclusion.html"><a href="conclusion.html#syntax-and-spacing"><i class="fa fa-check"></i><b>10.1.5</b> Syntax and Spacing</a></li>
<li class="chapter" data-level="10.1.6" data-path="conclusion.html"><a href="conclusion.html#writing-functions-1"><i class="fa fa-check"></i><b>10.1.6</b> Writing Functions</a></li>
<li class="chapter" data-level="10.1.7" data-path="conclusion.html"><a href="conclusion.html#error-handling-1"><i class="fa fa-check"></i><b>10.1.7</b> Error Handling</a></li>
<li class="chapter" data-level="10.1.8" data-path="conclusion.html"><a href="conclusion.html#version-control-with-git-2"><i class="fa fa-check"></i><b>10.1.8</b> Version Control with Git</a></li>
<li class="chapter" data-level="10.1.9" data-path="conclusion.html"><a href="conclusion.html#reproducibility-and-documentation-1"><i class="fa fa-check"></i><b>10.1.9</b> Reproducibility and Documentation</a></li>
</ul></li>
<li class="chapter" data-level="10.2" data-path="conclusion.html"><a href="conclusion.html#the-importance-of-adopting-best-practices"><i class="fa fa-check"></i><b>10.2</b> The Importance of Adopting Best Practices</a></li>
<li class="chapter" data-level="10.3" data-path="conclusion.html"><a href="conclusion.html#moving-forward"><i class="fa fa-check"></i><b>10.3</b> Moving Forward</a></li>
<li class="chapter" data-level="10.4" data-path="conclusion.html"><a href="conclusion.html#final-thoughts-1"><i class="fa fa-check"></i><b>10.4</b> Final Thoughts</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Best Coding Practices in R</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="error-handling" class="section level1 hasAnchor" number="7">
<h1><span class="header-section-number">7</span> Error Handling<a href="error-handling.html#error-handling" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>Error handling is a crucial aspect of writing robust and reliable R code. Properly handling errors allows your programs to fail gracefully, provides informative feedback to users, and helps you debug and maintain your code more effectively. In this chapter, we will explore techniques for managing errors in R, including the use of <code>tryCatch</code>, custom error messages, and best practices for defensive programming.</p>
<div id="introduction-to-error-handling" class="section level2 hasAnchor" number="7.1">
<h2><span class="header-section-number">7.1</span> Introduction to Error Handling<a href="error-handling.html#introduction-to-error-handling" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="what-is-error-handling" class="section level3 hasAnchor" number="7.1.1">
<h3><span class="header-section-number">7.1.1</span> What is Error Handling?<a href="error-handling.html#what-is-error-handling" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Error handling refers to the process of anticipating, detecting, and responding to errors or unexpected conditions that may occur during the execution of a program. Effective error handling ensures that:</p>
<ul>
<li><strong>Programs Fail Gracefully:</strong> Instead of crashing or producing incorrect results, the program handles errors in a controlled way.</li>
<li><strong>Users Receive Clear Feedback:</strong> Informative error messages help users understand what went wrong and how to fix it.</li>
<li><strong>Code is Easier to Debug:</strong> Error handling makes it easier to trace and fix issues when they arise.</li>
</ul>
</div>
<div id="types-of-errors-in-r" class="section level3 hasAnchor" number="7.1.2">
<h3><span class="header-section-number">7.1.2</span> Types of Errors in R<a href="error-handling.html#types-of-errors-in-r" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>In R, errors can broadly be categorised into:</p>
<ul>
<li><strong>Syntax Errors:</strong> Occur when the code is not written correctly according to the language’s grammar (e.g., missing commas or parentheses).</li>
<li><strong>Runtime Errors:</strong> Occur during the execution of the code (e.g., trying to divide by zero, or accessing a non-existent element in a vector).</li>
<li><strong>Logical Errors:</strong> Occur when the code runs without producing errors, but the results are incorrect due to a flaw in the logic.</li>
</ul>
</div>
</div>
<div id="basic-error-handling-with-try-and-trycatch" class="section level2 hasAnchor" number="7.2">
<h2><span class="header-section-number">7.2</span> Basic Error Handling with <code>try</code> and <code>tryCatch</code><a href="error-handling.html#basic-error-handling-with-try-and-trycatch" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="using-try" class="section level3 hasAnchor" number="7.2.1">
<h3><span class="header-section-number">7.2.1</span> Using <code>try</code><a href="error-handling.html#using-try" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The <code>try</code> function allows you to execute an expression and handle any errors that occur without stopping the execution of the script. It’s useful for continuing execution after encountering an error.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb59-1"><a href="error-handling.html#cb59-1" tabindex="-1"></a>result <span class="ot"><-</span> <span class="fu">try</span>(<span class="fu">log</span>(<span class="sc">-</span><span class="dv">1</span>), <span class="at">silent =</span> <span class="cn">TRUE</span>)</span>
<span id="cb59-2"><a href="error-handling.html#cb59-2" tabindex="-1"></a></span>
<span id="cb59-3"><a href="error-handling.html#cb59-3" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">inherits</span>(result, <span class="st">"try-error"</span>)) {</span>
<span id="cb59-4"><a href="error-handling.html#cb59-4" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">"An error occurred: "</span>, result)</span>
<span id="cb59-5"><a href="error-handling.html#cb59-5" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb59-6"><a href="error-handling.html#cb59-6" tabindex="-1"></a> <span class="fu">print</span>(result)</span>
<span id="cb59-7"><a href="error-handling.html#cb59-7" tabindex="-1"></a>}</span></code></pre></div></li>
</ul>
<p>In this example, the <code>log(-1)</code> operation would normally produce an error, but with <code>try</code>, the script continues running, and the error is handled gracefully.</p>
</div>
<div id="using-trycatch" class="section level3 hasAnchor" number="7.2.2">
<h3><span class="header-section-number">7.2.2</span> Using <code>tryCatch</code><a href="error-handling.html#using-trycatch" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The <code>tryCatch</code> function provides more flexibility by allowing you to define custom actions for different types of conditions: errors, warnings, and messages.</p>
<ul>
<li><p><strong>Basic Structure:</strong></p>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="error-handling.html#cb60-1" tabindex="-1"></a><span class="fu">tryCatch</span>(</span>
<span id="cb60-2"><a href="error-handling.html#cb60-2" tabindex="-1"></a> <span class="at">expr =</span> {</span>
<span id="cb60-3"><a href="error-handling.html#cb60-3" tabindex="-1"></a> <span class="co"># Code that may produce an error</span></span>
<span id="cb60-4"><a href="error-handling.html#cb60-4" tabindex="-1"></a> },</span>
<span id="cb60-5"><a href="error-handling.html#cb60-5" tabindex="-1"></a> <span class="at">error =</span> <span class="cf">function</span>(e) {</span>
<span id="cb60-6"><a href="error-handling.html#cb60-6" tabindex="-1"></a> <span class="co"># Code to handle errors</span></span>
<span id="cb60-7"><a href="error-handling.html#cb60-7" tabindex="-1"></a> },</span>
<span id="cb60-8"><a href="error-handling.html#cb60-8" tabindex="-1"></a> <span class="at">warning =</span> <span class="cf">function</span>(w) {</span>
<span id="cb60-9"><a href="error-handling.html#cb60-9" tabindex="-1"></a> <span class="co"># Code to handle warnings</span></span>
<span id="cb60-10"><a href="error-handling.html#cb60-10" tabindex="-1"></a> },</span>
<span id="cb60-11"><a href="error-handling.html#cb60-11" tabindex="-1"></a> <span class="at">finally =</span> {</span>
<span id="cb60-12"><a href="error-handling.html#cb60-12" tabindex="-1"></a> <span class="co"># Code that will always execute</span></span>
<span id="cb60-13"><a href="error-handling.html#cb60-13" tabindex="-1"></a> }</span>
<span id="cb60-14"><a href="error-handling.html#cb60-14" tabindex="-1"></a>)</span></code></pre></div></li>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb61-1"><a href="error-handling.html#cb61-1" tabindex="-1"></a><span class="fu">tryCatch</span>(</span>
<span id="cb61-2"><a href="error-handling.html#cb61-2" tabindex="-1"></a> <span class="at">expr =</span> {</span>
<span id="cb61-3"><a href="error-handling.html#cb61-3" tabindex="-1"></a> result <span class="ot"><-</span> <span class="fu">log</span>(<span class="sc">-</span><span class="dv">1</span>)</span>
<span id="cb61-4"><a href="error-handling.html#cb61-4" tabindex="-1"></a> },</span>
<span id="cb61-5"><a href="error-handling.html#cb61-5" tabindex="-1"></a> <span class="at">error =</span> <span class="cf">function</span>(e) {</span>
<span id="cb61-6"><a href="error-handling.html#cb61-6" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">"Caught an error: "</span>, e<span class="sc">$</span>message)</span>
<span id="cb61-7"><a href="error-handling.html#cb61-7" tabindex="-1"></a> },</span>
<span id="cb61-8"><a href="error-handling.html#cb61-8" tabindex="-1"></a> <span class="at">warning =</span> <span class="cf">function</span>(w) {</span>
<span id="cb61-9"><a href="error-handling.html#cb61-9" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">"Caught a warning: "</span>, w<span class="sc">$</span>message)</span>
<span id="cb61-10"><a href="error-handling.html#cb61-10" tabindex="-1"></a> },</span>
<span id="cb61-11"><a href="error-handling.html#cb61-11" tabindex="-1"></a> <span class="at">finally =</span> {</span>
<span id="cb61-12"><a href="error-handling.html#cb61-12" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">"Execution completed."</span>)</span>
<span id="cb61-13"><a href="error-handling.html#cb61-13" tabindex="-1"></a> }</span>
<span id="cb61-14"><a href="error-handling.html#cb61-14" tabindex="-1"></a>)</span></code></pre></div></li>
</ul>
<p>In this example, if an error or warning occurs during the execution of <code>log(-1)</code>, the respective handler function will be triggered, and a custom message will be displayed.</p>
</div>
<div id="returning-values-from-trycatch" class="section level3 hasAnchor" number="7.2.3">
<h3><span class="header-section-number">7.2.3</span> Returning Values from <code>tryCatch</code><a href="error-handling.html#returning-values-from-trycatch" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>You can use <code>tryCatch</code> not just for handling errors, but also for returning alternative values when an error occurs.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="error-handling.html#cb62-1" tabindex="-1"></a>safe_log <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb62-2"><a href="error-handling.html#cb62-2" tabindex="-1"></a> <span class="fu">tryCatch</span>(</span>
<span id="cb62-3"><a href="error-handling.html#cb62-3" tabindex="-1"></a> <span class="at">expr =</span> <span class="fu">log</span>(x),</span>
<span id="cb62-4"><a href="error-handling.html#cb62-4" tabindex="-1"></a> <span class="at">error =</span> <span class="cf">function</span>(e) <span class="cn">NA</span> <span class="co"># Return NA if an error occurs</span></span>
<span id="cb62-5"><a href="error-handling.html#cb62-5" tabindex="-1"></a> )</span>
<span id="cb62-6"><a href="error-handling.html#cb62-6" tabindex="-1"></a>}</span>
<span id="cb62-7"><a href="error-handling.html#cb62-7" tabindex="-1"></a><span class="fu">safe_log</span>(<span class="sc">-</span><span class="dv">1</span>) <span class="co"># Returns NA</span></span></code></pre></div></li>
</ul>
<p>This approach is useful when you want to continue processing other elements in a loop or apply a function over a dataset, even if some inputs produce errors.</p>
</div>
</div>
<div id="custom-error-messages-with-stop-warning-and-message" class="section level2 hasAnchor" number="7.3">
<h2><span class="header-section-number">7.3</span> Custom Error Messages with <code>stop</code>, <code>warning</code>, and <code>message</code><a href="error-handling.html#custom-error-messages-with-stop-warning-and-message" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="using-stop-for-critical-errors" class="section level3 hasAnchor" number="7.3.1">
<h3><span class="header-section-number">7.3.1</span> Using <code>stop</code> for Critical Errors<a href="error-handling.html#using-stop-for-critical-errors" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The <code>stop</code> function allows you to throw an error manually. This is useful when you want to enforce certain conditions in your code.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb63-1"><a href="error-handling.html#cb63-1" tabindex="-1"></a>divide <span class="ot"><-</span> <span class="cf">function</span>(a, b) {</span>
<span id="cb63-2"><a href="error-handling.html#cb63-2" tabindex="-1"></a> <span class="cf">if</span> (b <span class="sc">==</span> <span class="dv">0</span>) {</span>
<span id="cb63-3"><a href="error-handling.html#cb63-3" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Division by zero is not allowed."</span>)</span>
<span id="cb63-4"><a href="error-handling.html#cb63-4" tabindex="-1"></a> }</span>
<span id="cb63-5"><a href="error-handling.html#cb63-5" tabindex="-1"></a> <span class="fu">return</span>(a <span class="sc">/</span> b)</span>
<span id="cb63-6"><a href="error-handling.html#cb63-6" tabindex="-1"></a>}</span>
<span id="cb63-7"><a href="error-handling.html#cb63-7" tabindex="-1"></a><span class="fu">divide</span>(<span class="dv">10</span>, <span class="dv">0</span>) <span class="co"># Triggers an error</span></span></code></pre></div></li>
</ul>
<p>In this case, calling <code>divide(10, 0)</code> will produce a custom error message, preventing the operation from continuing.</p>
</div>
<div id="using-warning-for-non-critical-issues" class="section level3 hasAnchor" number="7.3.2">
<h3><span class="header-section-number">7.3.2</span> Using <code>warning</code> for Non-Critical Issues<a href="error-handling.html#using-warning-for-non-critical-issues" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The <code>warning</code> function issues a warning without stopping the execution of the code. This is useful for situations where the code can proceed, but the user should be alerted to a potential issue.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb64-1"><a href="error-handling.html#cb64-1" tabindex="-1"></a>check_positive <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb64-2"><a href="error-handling.html#cb64-2" tabindex="-1"></a> <span class="cf">if</span> (x <span class="sc"><</span> <span class="dv">0</span>) {</span>
<span id="cb64-3"><a href="error-handling.html#cb64-3" tabindex="-1"></a> <span class="fu">warning</span>(<span class="st">"Negative value detected. Proceeding with absolute value."</span>)</span>
<span id="cb64-4"><a href="error-handling.html#cb64-4" tabindex="-1"></a> x <span class="ot"><-</span> <span class="fu">abs</span>(x)</span>
<span id="cb64-5"><a href="error-handling.html#cb64-5" tabindex="-1"></a> }</span>
<span id="cb64-6"><a href="error-handling.html#cb64-6" tabindex="-1"></a> <span class="fu">return</span>(x)</span>
<span id="cb64-7"><a href="error-handling.html#cb64-7" tabindex="-1"></a>}</span>
<span id="cb64-8"><a href="error-handling.html#cb64-8" tabindex="-1"></a><span class="fu">check_positive</span>(<span class="sc">-</span><span class="dv">5</span>) <span class="co"># Issues a warning but returns 5</span></span></code></pre></div></li>
</ul>
<p>Here, a warning is issued, but the function continues and returns the absolute value.</p>
</div>
<div id="using-message-for-informational-messages" class="section level3 hasAnchor" number="7.3.3">
<h3><span class="header-section-number">7.3.3</span> Using <code>message</code> for Informational Messages<a href="error-handling.html#using-message-for-informational-messages" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>The <code>message</code> function is used to print informational messages to the console without affecting the flow of execution.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb65"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb65-1"><a href="error-handling.html#cb65-1" tabindex="-1"></a>calculate_area <span class="ot"><-</span> <span class="cf">function</span>(radius) {</span>
<span id="cb65-2"><a href="error-handling.html#cb65-2" tabindex="-1"></a> <span class="cf">if</span> (radius <span class="sc"><</span> <span class="dv">0</span>) {</span>
<span id="cb65-3"><a href="error-handling.html#cb65-3" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Radius cannot be negative."</span>)</span>
<span id="cb65-4"><a href="error-handling.html#cb65-4" tabindex="-1"></a> }</span>
<span id="cb65-5"><a href="error-handling.html#cb65-5" tabindex="-1"></a> area <span class="ot"><-</span> pi <span class="sc">*</span> radius<span class="sc">^</span><span class="dv">2</span></span>
<span id="cb65-6"><a href="error-handling.html#cb65-6" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">"Area calculated successfully."</span>)</span>
<span id="cb65-7"><a href="error-handling.html#cb65-7" tabindex="-1"></a> <span class="fu">return</span>(area)</span>
<span id="cb65-8"><a href="error-handling.html#cb65-8" tabindex="-1"></a>}</span>
<span id="cb65-9"><a href="error-handling.html#cb65-9" tabindex="-1"></a><span class="fu">calculate_area</span>(<span class="dv">5</span>) <span class="co"># Prints a message and returns the area</span></span></code></pre></div></li>
</ul>
<p>In this example, after successfully calculating the area, an informational message is printed to the console.</p>
</div>
</div>
<div id="defensive-programming" class="section level2 hasAnchor" number="7.4">
<h2><span class="header-section-number">7.4</span> Defensive Programming<a href="error-handling.html#defensive-programming" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="input-validation" class="section level3 hasAnchor" number="7.4.1">
<h3><span class="header-section-number">7.4.1</span> Input Validation<a href="error-handling.html#input-validation" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Defensive programming involves writing code that anticipates and handles potential errors before they occur. One common approach is input validation, where you check that inputs to functions are as expected.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb66-1"><a href="error-handling.html#cb66-1" tabindex="-1"></a>safe_sqrt <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb66-2"><a href="error-handling.html#cb66-2" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">is.numeric</span>(x)) {</span>
<span id="cb66-3"><a href="error-handling.html#cb66-3" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Input must be numeric."</span>)</span>
<span id="cb66-4"><a href="error-handling.html#cb66-4" tabindex="-1"></a> }</span>
<span id="cb66-5"><a href="error-handling.html#cb66-5" tabindex="-1"></a> <span class="cf">if</span> (x <span class="sc"><</span> <span class="dv">0</span>) {</span>
<span id="cb66-6"><a href="error-handling.html#cb66-6" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Cannot calculate the square root of a negative number."</span>)</span>
<span id="cb66-7"><a href="error-handling.html#cb66-7" tabindex="-1"></a> }</span>
<span id="cb66-8"><a href="error-handling.html#cb66-8" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">sqrt</span>(x))</span>
<span id="cb66-9"><a href="error-handling.html#cb66-9" tabindex="-1"></a>}</span>
<span id="cb66-10"><a href="error-handling.html#cb66-10" tabindex="-1"></a><span class="fu">safe_sqrt</span>(<span class="sc">-</span><span class="dv">4</span>) <span class="co"># Triggers an error</span></span></code></pre></div></li>
</ul>
</div>
<div id="asserting-conditions" class="section level3 hasAnchor" number="7.4.2">
<h3><span class="header-section-number">7.4.2</span> Asserting Conditions<a href="error-handling.html#asserting-conditions" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Using the <code>assertthat</code> package, you can assert conditions that must be true for the code to proceed.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb67"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb67-1"><a href="error-handling.html#cb67-1" tabindex="-1"></a><span class="fu">library</span>(assertthat)</span>
<span id="cb67-2"><a href="error-handling.html#cb67-2" tabindex="-1"></a></span>
<span id="cb67-3"><a href="error-handling.html#cb67-3" tabindex="-1"></a>process_data <span class="ot"><-</span> <span class="cf">function</span>(data) {</span>
<span id="cb67-4"><a href="error-handling.html#cb67-4" tabindex="-1"></a> <span class="fu">assert_that</span>(<span class="fu">is.data.frame</span>(data), <span class="at">msg =</span> <span class="st">"Input must be a data frame."</span>)</span>
<span id="cb67-5"><a href="error-handling.html#cb67-5" tabindex="-1"></a> <span class="fu">assert_that</span>(<span class="fu">ncol</span>(data) <span class="sc">></span> <span class="dv">0</span>, <span class="at">msg =</span> <span class="st">"Data frame must have at least one column."</span>)</span>
<span id="cb67-6"><a href="error-handling.html#cb67-6" tabindex="-1"></a> <span class="co"># Further processing</span></span>
<span id="cb67-7"><a href="error-handling.html#cb67-7" tabindex="-1"></a>}</span></code></pre></div></li>
</ul>
<p>Assertions help catch errors early in the execution process, making it easier to identify issues.</p>
</div>
<div id="handling-edge-cases" class="section level3 hasAnchor" number="7.4.3">
<h3><span class="header-section-number">7.4.3</span> Handling Edge Cases<a href="error-handling.html#handling-edge-cases" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Consider potential edge cases and handle them explicitly in your code to prevent unexpected errors.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb68"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb68-1"><a href="error-handling.html#cb68-1" tabindex="-1"></a>calculate_mean <span class="ot"><-</span> <span class="cf">function</span>(x) {</span>
<span id="cb68-2"><a href="error-handling.html#cb68-2" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(x) <span class="sc">==</span> <span class="dv">0</span>) {</span>
<span id="cb68-3"><a href="error-handling.html#cb68-3" tabindex="-1"></a> <span class="fu">warning</span>(<span class="st">"Input vector is empty. Returning NA."</span>)</span>
<span id="cb68-4"><a href="error-handling.html#cb68-4" tabindex="-1"></a> <span class="fu">return</span>(<span class="cn">NA</span>)</span>
<span id="cb68-5"><a href="error-handling.html#cb68-5" tabindex="-1"></a> }</span>
<span id="cb68-6"><a href="error-handling.html#cb68-6" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">mean</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>))</span>
<span id="cb68-7"><a href="error-handling.html#cb68-7" tabindex="-1"></a>}</span>
<span id="cb68-8"><a href="error-handling.html#cb68-8" tabindex="-1"></a><span class="fu">calculate_mean</span>(<span class="fu">numeric</span>(<span class="dv">0</span>)) <span class="co"># Returns NA with a warning</span></span></code></pre></div></li>
</ul>
</div>
</div>
<div id="best-practices-for-error-handling" class="section level2 hasAnchor" number="7.5">
<h2><span class="header-section-number">7.5</span> Best Practices for Error Handling<a href="error-handling.html#best-practices-for-error-handling" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="provide-clear-and-informative-messages" class="section level3 hasAnchor" number="7.5.1">
<h3><span class="header-section-number">7.5.1</span> Provide Clear and Informative Messages<a href="error-handling.html#provide-clear-and-informative-messages" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Error and warning messages should be descriptive and guide the user on how to resolve the issue. Avoid using cryptic messages that do not provide context.</p>
</div>
<div id="use-trycatch-for-anticipated-errors" class="section level3 hasAnchor" number="7.5.2">
<h3><span class="header-section-number">7.5.2</span> Use <code>tryCatch</code> for Anticipated Errors<a href="error-handling.html#use-trycatch-for-anticipated-errors" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Use <code>tryCatch</code> in situations where errors are expected, such as when reading files, connecting to databases, or performing operations on user inputs.</p>
</div>
<div id="log-errors-in-production-code" class="section level3 hasAnchor" number="7.5.3">
<h3><span class="header-section-number">7.5.3</span> Log Errors in Production Code<a href="error-handling.html#log-errors-in-production-code" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>In production environments, consider logging errors to a file or monitoring system to track issues over time. This can help identify recurring problems and improve the robustness of your code.</p>
</div>
<div id="test-error-handling" class="section level3 hasAnchor" number="7.5.4">
<h3><span class="header-section-number">7.5.4</span> Test Error Handling<a href="error-handling.html#test-error-handling" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Write tests that specifically check how your code handles errors. Ensure that your error handling does not inadvertently suppress important errors or produce incorrect results.</p>
<ul>
<li><p><strong>Example:</strong></p>
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb69-1"><a href="error-handling.html#cb69-1" tabindex="-1"></a><span class="fu">test_that</span>(<span class="st">"safe_log handles negative inputs"</span>, {</span>
<span id="cb69-2"><a href="error-handling.html#cb69-2" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">safe_log</span>(<span class="sc">-</span><span class="dv">1</span>), <span class="cn">NA</span>)</span>
<span id="cb69-3"><a href="error-handling.html#cb69-3" tabindex="-1"></a>})</span></code></pre></div></li>
</ul>
</div>
</div>
<div id="summary-5" class="section level2 hasAnchor" number="7.6">
<h2><span class="header-section-number">7.6</span> Summary<a href="error-handling.html#summary-5" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Effective error handling is essential for writing robust R code. By using tools like <code>tryCatch</code>, <code>stop</code>, <code>warning</code>, and <code>message</code>, you can manage errors and warnings gracefully, providing clear feedback to users and maintaining the reliability of your code. Defensive programming techniques, such as input validation and assertions, further help prevent errors before they occur. By following the best practices outlined in this chapter, you’ll be well-equipped to handle errors in your R projects and create more resilient code.</p>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="writing-functions.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="version-control-with-git.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/USERNAME/REPO/edit/BRANCH/07-error.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["_main.pdf", "_main.epub"],
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "subsection"
}
});
});
</script>
</body>
</html>