-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversion-control-with-git.html
More file actions
678 lines (637 loc) · 67 KB
/
Copy pathversion-control-with-git.html
File metadata and controls
678 lines (637 loc) · 67 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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>8 Version Control with Git | 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="8 Version Control with Git | 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="8 Version Control with Git | 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="error-handling.html"/>
<link rel="next" href="reproducibility-and-documentation.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="version-control-with-git" class="section level1 hasAnchor" number="8">
<h1><span class="header-section-number">8</span> Version Control with Git<a href="version-control-with-git.html#version-control-with-git" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>Version control is a crucial aspect of managing code in any programming environment, and Git is the most widely used version control system. By incorporating Git into your R workflow, you can track changes, collaborate with others, and maintain a history of your codebase. This chapter will guide you through the essentials of using Git for version control in R projects.</p>
<div id="introduction-to-git" class="section level2 hasAnchor" number="8.1">
<h2><span class="header-section-number">8.1</span> Introduction to Git<a href="version-control-with-git.html#introduction-to-git" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Git is a distributed version control system that allows multiple developers to work on a project simultaneously without overwriting each other’s changes. It keeps a history of all changes made to files in a repository, enabling you to revert to previous versions if needed. Understanding Git is essential for any developer looking to collaborate on projects or maintain a robust and organised codebase.</p>
<div id="benefits-of-using-git" class="section level3 hasAnchor" number="8.1.1">
<h3><span class="header-section-number">8.1.1</span> Benefits of Using Git<a href="version-control-with-git.html#benefits-of-using-git" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<ul>
<li><strong>Change Tracking</strong>: Git records every change made to your files, allowing you to track progress and undo mistakes.</li>
<li><strong>Collaboration</strong>: Multiple developers can work on different parts of a project simultaneously, merging their changes seamlessly.</li>
<li><strong>Backup</strong>: With Git, each clone of the repository is a complete backup, safeguarding against data loss.</li>
<li><strong>Branching</strong>: Git allows you to create branches to work on new features or experiments without affecting the main codebase.</li>
</ul>
</div>
</div>
<div id="setting-up-git" class="section level2 hasAnchor" number="8.2">
<h2><span class="header-section-number">8.2</span> Setting Up Git<a href="version-control-with-git.html#setting-up-git" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="installing-git" class="section level3 hasAnchor" number="8.2.1">
<h3><span class="header-section-number">8.2.1</span> Installing Git<a href="version-control-with-git.html#installing-git" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Before using Git, you need to install it on your machine:</p>
<ul>
<li><strong>Windows</strong>: Download Git from <a href="https://git-scm.com/">git-scm.com</a> and follow the installation instructions.</li>
<li><strong>macOS</strong>: Install Git using Homebrew with the command <code>brew install git</code>.</li>
<li><strong>Linux</strong>: Install Git through your package manager, e.g., <code>sudo apt-get install git</code> on Ubuntu.</li>
</ul>
</div>
<div id="configuring-git" class="section level3 hasAnchor" number="8.2.2">
<h3><span class="header-section-number">8.2.2</span> Configuring Git<a href="version-control-with-git.html#configuring-git" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>After installation, configure Git with your username and email address. This information will be associated with your commits.</p>
<div class="sourceCode" id="cb70"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb70-1"><a href="version-control-with-git.html#cb70-1" tabindex="-1"></a><span class="fu">git</span> config <span class="at">--global</span> user.name <span class="st">"Your Name"</span></span>
<span id="cb70-2"><a href="version-control-with-git.html#cb70-2" tabindex="-1"></a><span class="fu">git</span> config <span class="at">--global</span> user.email <span class="st">"youremail@example.com"</span></span></code></pre></div>
<p>To verify your configuration, use:</p>
<div class="sourceCode" id="cb71"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb71-1"><a href="version-control-with-git.html#cb71-1" tabindex="-1"></a><span class="fu">git</span> config <span class="at">--list</span></span></code></pre></div>
</div>
</div>
<div id="basic-git-workflow" class="section level2 hasAnchor" number="8.3">
<h2><span class="header-section-number">8.3</span> Basic Git Workflow<a href="version-control-with-git.html#basic-git-workflow" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="initialising-a-repository" class="section level3 hasAnchor" number="8.3.1">
<h3><span class="header-section-number">8.3.1</span> Initialising a Repository<a href="version-control-with-git.html#initialising-a-repository" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>To start using Git in your R project, navigate to your project directory and initialise a repository:</p>
<div class="sourceCode" id="cb72"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb72-1"><a href="version-control-with-git.html#cb72-1" tabindex="-1"></a><span class="fu">git</span> init</span></code></pre></div>
<p>This command creates a <code>.git</code> directory in your project, which Git uses to track changes.</p>
</div>
<div id="staging-and-committing-changes" class="section level3 hasAnchor" number="8.3.2">
<h3><span class="header-section-number">8.3.2</span> Staging and Committing Changes<a href="version-control-with-git.html#staging-and-committing-changes" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Git tracks changes in your project through a process of staging and committing. First, add changes to the staging area:</p>
<div class="sourceCode" id="cb73"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb73-1"><a href="version-control-with-git.html#cb73-1" tabindex="-1"></a><span class="fu">git</span> add filename.R</span></code></pre></div>
<p>To stage all changes at once:</p>
<div class="sourceCode" id="cb74"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb74-1"><a href="version-control-with-git.html#cb74-1" tabindex="-1"></a><span class="fu">git</span> add .</span></code></pre></div>
<p>After staging, commit your changes with a descriptive message:</p>
<div class="sourceCode" id="cb75"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb75-1"><a href="version-control-with-git.html#cb75-1" tabindex="-1"></a><span class="fu">git</span> commit <span class="at">-m</span> <span class="st">"Your commit message"</span></span></code></pre></div>
</div>
<div id="viewing-repository-status" class="section level3 hasAnchor" number="8.3.3">
<h3><span class="header-section-number">8.3.3</span> Viewing Repository Status<a href="version-control-with-git.html#viewing-repository-status" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>To see which files have been modified or staged:</p>
<div class="sourceCode" id="cb76"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb76-1"><a href="version-control-with-git.html#cb76-1" tabindex="-1"></a><span class="fu">git</span> status</span></code></pre></div>
</div>
<div id="viewing-commit-history" class="section level3 hasAnchor" number="8.3.4">
<h3><span class="header-section-number">8.3.4</span> Viewing Commit History<a href="version-control-with-git.html#viewing-commit-history" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>To view the history of commits in your repository:</p>
<div class="sourceCode" id="cb77"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb77-1"><a href="version-control-with-git.html#cb77-1" tabindex="-1"></a><span class="fu">git</span> log</span></code></pre></div>
<p>This command shows a log of all commits made to the repository.</p>
</div>
</div>
<div id="branching-and-merging" class="section level2 hasAnchor" number="8.4">
<h2><span class="header-section-number">8.4</span> Branching and Merging<a href="version-control-with-git.html#branching-and-merging" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="creating-and-switching-branches" class="section level3 hasAnchor" number="8.4.1">
<h3><span class="header-section-number">8.4.1</span> Creating and Switching Branches<a href="version-control-with-git.html#creating-and-switching-branches" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Branches allow you to work on new features or fixes without affecting the main codebase. To create a new branch:</p>
<div class="sourceCode" id="cb78"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb78-1"><a href="version-control-with-git.html#cb78-1" tabindex="-1"></a><span class="fu">git</span> branch new-feature</span></code></pre></div>
<p>Switch to the new branch:</p>
<div class="sourceCode" id="cb79"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb79-1"><a href="version-control-with-git.html#cb79-1" tabindex="-1"></a><span class="fu">git</span> checkout new-feature</span></code></pre></div>
<p>Alternatively, you can create and switch to a new branch in one command:</p>
<div class="sourceCode" id="cb80"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb80-1"><a href="version-control-with-git.html#cb80-1" tabindex="-1"></a><span class="fu">git</span> checkout <span class="at">-b</span> new-feature</span></code></pre></div>
</div>
<div id="merging-branches" class="section level3 hasAnchor" number="8.4.2">
<h3><span class="header-section-number">8.4.2</span> Merging Branches<a href="version-control-with-git.html#merging-branches" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Once your work on a branch is complete, merge it back into the main branch:</p>
<div class="sourceCode" id="cb81"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb81-1"><a href="version-control-with-git.html#cb81-1" tabindex="-1"></a><span class="fu">git</span> checkout main</span>
<span id="cb81-2"><a href="version-control-with-git.html#cb81-2" tabindex="-1"></a><span class="fu">git</span> merge new-feature</span></code></pre></div>
</div>
<div id="resolving-merge-conflicts" class="section level3 hasAnchor" number="8.4.3">
<h3><span class="header-section-number">8.4.3</span> Resolving Merge Conflicts<a href="version-control-with-git.html#resolving-merge-conflicts" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Conflicts may arise if changes on different branches affect the same part of a file. Git will pause the merge and mark the conflicting files for you to resolve manually. After resolving the conflicts, stage the changes:</p>
<div class="sourceCode" id="cb82"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb82-1"><a href="version-control-with-git.html#cb82-1" tabindex="-1"></a><span class="fu">git</span> add conflicted-file.R</span></code></pre></div>
<p>Then, complete the merge:</p>
<div class="sourceCode" id="cb83"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb83-1"><a href="version-control-with-git.html#cb83-1" tabindex="-1"></a><span class="fu">git</span> commit <span class="at">-m</span> <span class="st">"Resolved merge conflicts"</span></span></code></pre></div>
</div>
</div>
<div id="working-with-remote-repositories" class="section level2 hasAnchor" number="8.5">
<h2><span class="header-section-number">8.5</span> Working with Remote Repositories<a href="version-control-with-git.html#working-with-remote-repositories" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="cloning-a-repository" class="section level3 hasAnchor" number="8.5.1">
<h3><span class="header-section-number">8.5.1</span> Cloning a Repository<a href="version-control-with-git.html#cloning-a-repository" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>To collaborate on an existing project, clone the repository to your local machine:</p>
<div class="sourceCode" id="cb84"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb84-1"><a href="version-control-with-git.html#cb84-1" tabindex="-1"></a><span class="fu">git</span> clone https://github.com/username/repository.git</span></code></pre></div>
</div>
<div id="before-starting-work-ensure-your-local-repository-is-up-to-date-by-pulling-the-latest-changes" class="section level3 hasAnchor" number="8.5.2">
<h3><span class="header-section-number">8.5.2</span> Before starting work, ensure your local repository is up to date by pulling the latest changes:<a href="version-control-with-git.html#before-starting-work-ensure-your-local-repository-is-up-to-date-by-pulling-the-latest-changes" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<div class="sourceCode" id="cb85"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb85-1"><a href="version-control-with-git.html#cb85-1" tabindex="-1"></a><span class="fu">git</span> pull origin main</span></code></pre></div>
</div>
<div id="pushing-changes" class="section level3 hasAnchor" number="8.5.3">
<h3><span class="header-section-number">8.5.3</span> Pushing Changes<a href="version-control-with-git.html#pushing-changes" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>After committing your changes locally, push them to the remote repository:</p>
<div class="sourceCode" id="cb86"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb86-1"><a href="version-control-with-git.html#cb86-1" tabindex="-1"></a><span class="fu">git</span> push origin branch-name</span></code></pre></div>
<p>If working on the main branch, replace branch-name with main.</p>
</div>
</div>
<div id="advanced-git-features" class="section level2 hasAnchor" number="8.6">
<h2><span class="header-section-number">8.6</span> Advanced Git Features<a href="version-control-with-git.html#advanced-git-features" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="stashing-changes" class="section level3 hasAnchor" number="8.6.1">
<h3><span class="header-section-number">8.6.1</span> Stashing Changes<a href="version-control-with-git.html#stashing-changes" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>If you need to switch branches or shelve your work temporarily without committing, you can stash your changes:</p>
<div class="sourceCode" id="cb87"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb87-1"><a href="version-control-with-git.html#cb87-1" tabindex="-1"></a><span class="fu">git</span> stash</span></code></pre></div>
<p>To retrieve stashed changes later:</p>
<div class="sourceCode" id="cb88"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb88-1"><a href="version-control-with-git.html#cb88-1" tabindex="-1"></a><span class="fu">git</span> stash apply</span></code></pre></div>
</div>
<div id="rebasing" class="section level3 hasAnchor" number="8.6.2">
<h3><span class="header-section-number">8.6.2</span> Rebasing<a href="version-control-with-git.html#rebasing" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Rebasing rewrites the commit history to create a linear sequence of commits. This is useful when you want to incorporate changes from one branch into another without a merge commit:</p>
<div class="sourceCode" id="cb89"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb89-1"><a href="version-control-with-git.html#cb89-1" tabindex="-1"></a><span class="fu">git</span> checkout feature-branch</span>
<span id="cb89-2"><a href="version-control-with-git.html#cb89-2" tabindex="-1"></a><span class="fu">git</span> rebase main</span></code></pre></div>
</div>
<div id="tags" class="section level3 hasAnchor" number="8.6.3">
<h3><span class="header-section-number">8.6.3</span> Tags<a href="version-control-with-git.html#tags" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Tags are useful for marking specific points in your commit history, such as release versions:</p>
<div class="sourceCode" id="cb90"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb90-1"><a href="version-control-with-git.html#cb90-1" tabindex="-1"></a><span class="fu">git</span> tag <span class="at">-a</span> v1.0 <span class="at">-m</span> <span class="st">"Version 1.0 release"</span></span>
<span id="cb90-2"><a href="version-control-with-git.html#cb90-2" tabindex="-1"></a><span class="fu">git</span> push origin v1.0</span></code></pre></div>
</div>
</div>
<div id="git-in-rstudio" class="section level2 hasAnchor" number="8.7">
<h2><span class="header-section-number">8.7</span> Git in RStudio<a href="version-control-with-git.html#git-in-rstudio" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="setting-up-git-in-rstudio" class="section level3 hasAnchor" number="8.7.1">
<h3><span class="header-section-number">8.7.1</span> Setting Up Git in RStudio<a href="version-control-with-git.html#setting-up-git-in-rstudio" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>RStudio integrates well with Git, making it easy to manage version control directly from the IDE. To set up Git in RStudio:</p>
<ul>
<li>Open RStudio and go to Tools > Global Options > Git/SVN.</li>
<li>Ensure that the path to the Git executable is correctly set.</li>
</ul>
</div>
<div id="using-git-in-rstudio" class="section level3 hasAnchor" number="8.7.2">
<h3><span class="header-section-number">8.7.2</span> Using Git in RStudio<a href="version-control-with-git.html#using-git-in-rstudio" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>In RStudio, you can commit, push, pull, and manage branches using the Git pane. This integration simplifies version control tasks by providing a graphical interface.</p>
</div>
</div>
<div id="best-practices-for-using-git" class="section level2 hasAnchor" number="8.8">
<h2><span class="header-section-number">8.8</span> Best Practices for Using Git<a href="version-control-with-git.html#best-practices-for-using-git" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<ul>
<li><em>Commit Often:</em> Make small, frequent commits with clear messages to track changes effectively.</li>
<li><em>Use Branches:</em> Always create a new branch for features or fixes to avoid conflicts and keep the main branch stable.</li>
<li><em>Write Descriptive Commit Messages:</em> Commit messages should clearly describe the changes made to the code.</li>
<li><em>Pull Before Pushing:</em> Always pull the latest changes from the remote repository before pushing your commits to avoid conflicts.</li>
</ul>
</div>
<div id="summary-6" class="section level2 hasAnchor" number="8.9">
<h2><span class="header-section-number">8.9</span> Summary<a href="version-control-with-git.html#summary-6" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>In this chapter, we covered the essentials of using Git for version control in R projects. You learned how to set up Git, manage repositories, use branches, and collaborate with others. We also discussed advanced Git features like rebasing and stashing, as well as best practices to ensure efficient version control. By integrating Git into your R workflow, you’ll enhance your ability to manage and collaborate on projects effectively.</p>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="error-handling.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="reproducibility-and-documentation.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/08-version.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>