-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·1800 lines (1695 loc) · 107 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="author" content="Julien Diot" />
<title>R Tips & Tricks</title>
<script src="index_files/header-attrs-2.25/header-attrs.js"></script>
<script src="index_files/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="index_files/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="index_files/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="index_files/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="index_files/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="index_files/jqueryui-1.13.2/jquery-ui.min.js"></script>
<link href="index_files/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="index_files/tocify-1.9.1/jquery.tocify.js"></script>
<script src="index_files/navigation-1.1/tabsets.js"></script>
<script src="index_files/navigation-1.1/codefolding.js"></script>
<link href="index_files/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="index_files/highlightjs-9.12.0/highlight.js"></script>
<link href="index_files/pagedtable-1.1/css/pagedtable.css" rel="stylesheet" />
<script src="index_files/pagedtable-1.1/js/pagedtable.js"></script>
<script src="index_files/htmlwidgets-1.6.2/htmlwidgets.js"></script>
<script src="index_files/plotly-binding-4.10.3/plotly.js"></script>
<script src="index_files/typedarray-0.1/typedarray.min.js"></script>
<link href="index_files/crosstalk-1.2.0/css/crosstalk.min.css" rel="stylesheet" />
<script src="index_files/crosstalk-1.2.0/js/crosstalk.min.js"></script>
<link href="index_files/plotly-htmlwidgets-css-2.11.1/plotly-htmlwidgets.css" rel="stylesheet" />
<script src="index_files/plotly-main-2.11.1/plotly-latest.min.js"></script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<link rel="stylesheet" href="src/myStyle.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before, .tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "\e259";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "\e258";
font-family: 'Glyphicons Halflings';
border: none;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
.code-folding-btn { margin-bottom: 4px; }
</style>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div id="header">
<div class="btn-group pull-right float-right">
<button type="button" class="btn btn-default btn-xs btn-secondary btn-sm dropdown-toggle" data-toggle="dropdown" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span>Code</span> <span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right" style="min-width: 50px;">
<li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
<li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
</ul>
</div>
<h1 class="title toc-ignore">R Tips & Tricks</h1>
<h4 class="author">Julien Diot</h4>
<h4 class="date">Last update: 2024/02/21</h4>
</div>
<style type="text/css">
/* add some CSS styling here */
</style>
<p><strong>Disclaimer</strong></p>
<p>The R Tips and Tricks presented here <strong>are just what I
use</strong>, and I don’t guarantee that this is the best way to do it.
Moreover, new features are developed every days some stuff presented
here might be outdated in the future. If you have any comments or
suggestions, feel free to contact me: <a
href="mailto:[email protected]"
class="email">[email protected]</a> or post an issue on <a
href="https://github.com/juliendiot42/RTips-Tricks/issues">GitHub</a>.</p>
<div id="r-studio-configuration" class="section level1" number="1">
<h1><span class="header-section-number">1</span> R-Studio
configuration</h1>
<p>My favourite
<svg aria-hidden="true" role="img" viewBox="0 0 581 512" style="height:20px;width:22.7px;vertical-align:-0.125em;margin-left:auto;margin-right:auto;font-size:inherit;fill:steelblue;overflow:visible;position:relative;"><path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"/></svg>
IDE is <a
href="https://rstudio.com/products/rstudio/download/#download">R-Studio
Desktop</a>. It is free and open-source and provides a lot of powerful
features.</p>
<p>This section presents interesting R-Studio settings and my current
setup.</p>
<p>To setup R-Studio go to:
<code>Tools -> Global Options...</code>.</p>
<p>Documentation for these settings is available <a
href="https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio">here</a>.
It might not be up to date, but most of the settings are presented.</p>
<div class="note">
<p>Some useful R-Studio settings are not activated by default, and I
think it’s worth taking some time to check these settings.</p>
</div>
<div id="pane-layout-and-appearance" class="section level2"
number="1.1">
<h2><span class="header-section-number">1.1</span> Pane layout and
appearance</h2>
<p><code>Tools -> Global Options... -> Pane layout</code></p>
<p>To have <strong>more space for the editor</strong>, we can
<strong>move the console on the top right corner</strong> and move the
“Environment pane” to the bottom right.</p>
<p>Indeed, the panes on the bottom left do not need to be continuously
displayed.</p>
<div class="float">
<img src="src/img/RS-layout.png"
alt="The editor takes the the whole windows" />
<div class="figcaption">The editor takes the the whole windows</div>
</div>
<p>As you can see, I prefer <strong>dark theme</strong> because it’s
more pleasant for my eyes. “Idle Fingers” is used on the image above.
However, several other themes are available, some a light other are
black.</p>
<p>It is also possible to create <strong>your own custom themes</strong>
as explain in this <a
href="https://blog.rstudio.com/2018/10/29/rstudio-ide-custom-theme-support/">R-Studio
blog’s article</a></p>
<p>I also use the font <a
href="https://github.com/tonsky/FiraCode"><strong>“Fira
code”</strong></a>, this font provide programming ligatures:</p>
<div class="row">
<div class="col-md-6">
<div class="float">
<img src="src/img/fontCourrier.png" alt="Courier 10 pitch" />
<div class="figcaption">Courier 10 pitch</div>
</div>
</div>
<div class="col-md-6">
<div class="float">
<img src="src/img/fontFira.png" alt="Fira Code" />
<div class="figcaption">Fira Code</div>
</div>
</div>
</div>
</div>
<div id="generalSettings" class="section level2" number="1.2">
<h2><span class="header-section-number">1.2</span> General settings</h2>
<div class="float">
<img src="src/img/RS-general.png" alt="General settings" />
<div class="figcaption">General settings</div>
</div>
<p>It is essential to <strong>not</strong> restore or save your
workspace at startup or end of the R session. By this way, you will
always start R-Studio in a fresh and clean environment. Because you have
saved your script, you can regenerate the situation by running it. This
is much better for the reproducibility of your work.</p>
</div>
<div id="code-settings"
class="section level2 tabset tabset-fade tabset-pills" number="1.3">
<h2 class="tabset tabset-fade tabset-pills"><span
class="header-section-number">1.3</span> Code settings</h2>
<p><code>Tools -> Global Options... -> Code</code></p>
<p>I present here R-Studio code settings. The highlighted lines are
those I find interesting.</p>
<div id="editing" class="section level3" number="1.3.1">
<h3><span class="header-section-number">1.3.1</span> Editing</h3>
<div class="float">
<img src="src/img/RS-edit.png" alt="Editing Settings" />
<div class="figcaption">Editing Settings</div>
</div>
<ul>
<li><strong>Insert matching parens/quotes</strong>: When typing a paren,
quote, or brace automatically insert a matching one and position the
cursor between them.</li>
<li><strong>Auto-indent code after paste</strong>: Automatically execute
a Reindent on blocks of R code pasted into the editor.</li>
<li><strong>Vertically align arguments in auto-indent</strong>: Preserve
the current indentation level for function arguments split across
multiple lines.</li>
<li><strong>Enable code snippets</strong>: see the corresponding <a
href="#snippets">section</a>.</li>
</ul>
</div>
<div id="display" class="section level3" number="1.3.2">
<h3><span class="header-section-number">1.3.2</span> Display</h3>
<center>
<p><img src="src/img/RS-display.png" /></p>
</center>
<ul>
<li><strong>Highlight selected word</strong>: Add a background highlight
effect to all instances of the currently selected word within the
document. (Useful to find some variables in the code)</li>
<li><strong>Show line numbers</strong>: Show or hide line numbers within
the left margin. (Useful for debugging)</li>
<li><strong>Show margin</strong>: Display a margin guide on the
right-hand side of the source editor at the specified column. (The “good
practices” require to have line smaller than 80 characters)</li>
<li><strong>Show whitespace characters</strong>: Display glyphs
indicating where whitespace characters (tab, newline, etc.) are located
in source documents. (Useful to detect “double spaces” and
tabulation)</li>
<li><strong>Highlight R function calls</strong>: Highlight R functions.
(Better distinction between functions and other R objects)</li>
<li><strong>Rainbow Parentheses</strong>: Being able to color your
parentheses (and brackets and braces) based on the level of nesting. See
<a
href="https://blog.rstudio.com/2020/11/04/rstudio-1-4-preview-rainbow-parentheses/">here</a></li>
</ul>
</div>
<div id="saving" class="section level3" number="1.3.3">
<h3><span class="header-section-number">1.3.3</span> Saving</h3>
<center>
<p><img src="src/img/RS-save.png" /></p>
</center>
<ul>
<li><strong>Ensure that source files end with a new line</strong>: I
It’s a good practice to end the coding files with a new line.</li>
<li><strong>Strip trailing horizontal whitespaces when saving</strong>:
Remove whitespaces from blank line:</li>
</ul>
<div class="float">
<img src="src/img/RS-remWhiteSpace.gif" alt="Effect of Ctrl + S" />
<div class="figcaption">Effect of <code>Ctrl</code> +
<code>S</code></div>
</div>
<ul>
<li><strong>Auto-save</strong>: New feature of <strong>R-Studio
1.3</strong></li>
</ul>
</div>
<div id="completion" class="section level3" number="1.3.4">
<h3><span class="header-section-number">1.3.4</span> Completion</h3>
<center>
<p><img src="src/img/RS-completion.png" /></p>
</center>
<p>Completion help to write functions name and their parameters just by
taping the first letters and the <code>tab</code> key:</p>
<div class="float">
<img src="src/img/RS-completion.gif"
alt="Press tab for automatic completion" />
<div class="figcaption">Press <code>tab</code> for automatic
completion</div>
</div>
</div>
<div id="diagnostics" class="section level3" number="1.3.5">
<h3><span class="header-section-number">1.3.5</span> Diagnostics</h3>
<center>
<p><img src="src/img/RS-diagnostic.png" /></p>
</center>
<ul>
<li><strong>Show diagnostics for R</strong>: Check to display alert
messages in the margin of the editor when R code appears incorrect.</li>
</ul>
<div class="float">
<img src="src/img/RS-diagnostic.gif"
alt="Different diagnostic results" />
<div class="figcaption">Different diagnostic results</div>
</div>
</div>
</div>
<div id="spellchecking" class="section level2" number="1.4">
<h2><span class="header-section-number">1.4</span> Spellchecking</h2>
<div class="float">
<img src="src/img/RS-spellCheck.png"
alt="R-Studio 1.3 provide real time spellchecking" />
<div class="figcaption">R-Studio 1.3 provide real time
spellchecking</div>
</div>
</div>
<div id="export-your-configuration" class="section level2" number="1.5">
<h2><span class="header-section-number">1.5</span> Export your
configuration</h2>
<p>From R-Studio 1.3, the configuration is saved at
<code>~/.config/rstudio</code> for Linux/MacOS users and at
<code>AppData/Roaming/RStudio</code> on Windows. It is then easy to save
and retrieve your configuration. This folder contains:</p>
<ul>
<li>The R-Studio settings: <code>rstudio-pref.json</code></li>
<li>The <a href="#snippets">snippets</a></li>
<li>The personal dictionary</li>
<li>The personal keyboard shortcuts definitions</li>
</ul>
</div>
</div>
<div id="rprofile-file" class="section level1" number="2">
<h1><span class="header-section-number">2</span> <code>.Rprofile</code>
file</h1>
<p>The <code>.Rprofile</code> file can be used to customize the
environment at the startup of R. It should be placed in the HOME
directory, or inside a folder of an R-project. (In this case, the one
inside the project take precedence over the other).</p>
<p>When R starts, it automatically sources the <code>.Rprofile</code>
file. So it can be used to set up some R options like:
<code>options(max.print = 200)</code> to reduce the number of printed
lines or <code>options("scipen" = 999)</code> to disabled the scientific
notation.</p>
<p>There are also two particular functions: <code>.First( )</code> which
will be run at the start of the R session and <code>.Last( )</code> at
the end of the session.</p>
<p>You can try to copy these lines in your <code>.Rprofile</code>
file:</p>
<pre class="r"><code>.First <- function() {
cat("Welcome", Sys.info()["user"], "!\n")
}</code></pre>
<div class="NOTE">
<p>You should not modify options that can have an effect on the R
behavior ! For example setting
<code>options(stringsAsFactors = TRUE/FALSE)</code> in your
<code>.Rprofile</code> file is a bad idea. Indeed, your codes might not
run well on another computer.</p>
</div>
</div>
<div id="working-with-r-studio" class="section level1" number="3">
<h1><span class="header-section-number">3</span> Working with
R-Studio</h1>
<div id="panes" class="section level2 tabset tabset-fade tabset-pills"
number="3.1">
<h2 class="tabset tabset-fade tabset-pills"><span
class="header-section-number">3.1</span> Panes</h2>
<div id="environment" class="section level3" number="3.1.1">
<h3><span class="header-section-number">3.1.1</span> Environment</h3>
<div class="float">
<img src="src/img/RSTools-Env.png" alt="Environment pane" />
<div class="figcaption">Environment pane</div>
</div>
<ul>
<li><p>Environment pane presents the variables defined in your
environments. By default, it shows the global environment, but you can
select others (like those of the loaded packages).</p></li>
<li><p>The <img src="src/img/RSTools-EnvButton.png" alt="broom" />
button clears the workspace.</p></li>
<li><p>An easy and graphical way to import data of different types
(text, excel, SAS …) is to use the <img
src="src/img/RSTools-EnvButton2.png" alt="import Dataset" /> button. The
corresponding R code will be written in the console, and you can then
copy/paste it in your scripts.</p></li>
<li><p>You can visualize the variables defined in your environments by
clicking on them. This is a shortcut for a call to the R function
<code>View()</code>.</p></li>
</ul>
<div class="float">
<img src="src/img/RSTools-Env.gif"
alt="Click on your R objects to view them in the main window" />
<div class="figcaption">Click on your R objects to view them in the main
window</div>
</div>
</div>
<div id="file-browser" class="section level3" number="3.1.2">
<h3><span class="header-section-number">3.1.2</span> File browser</h3>
<div class="float">
<img src="src/img/RSTools-file.png" alt="R-Studio file pane" />
<div class="figcaption">R-Studio file pane</div>
</div>
<p>By default, it is open at your current working directory. You can
navigate through your file and set up a new working directory with the
button <img src="src/img/RSTools-fileMore.png" />.</p>
<p>It’s quite convenient to manage the files directly from R-Studio. If
you are working in a <a href="#Rproj">project</a>, you can quickly
access to your root folder with the <img
src="src/img/RSTools-fileR.png" /> button.</p>
</div>
<div id="packages" class="section level3" number="3.1.3">
<h3><span class="header-section-number">3.1.3</span> Packages</h3>
<div class="float">
<img src="src/img/RSTools-pkg.png" alt="R-Studio package pane" />
<div class="figcaption">R-Studio package pane</div>
</div>
<p>In the package pane, you can <strong>install new packages</strong>.
It’s particularly convenient when you don’t know the exact package
name.</p>
<p><img src="src/img/RSTools-pkgInst.png" /></p>
<p>With the update button you can also check if some of your packages
are not up to date. With the <img
src="src/img/RSTools-pkgUpButton.png" /> button, you can access
<strong>the “news file”</strong> of these packages to see what is new.
(See also section <a href="#cranalert">CRANalert</a> for a notifier tool
about updated packages).</p>
<p><img src="src/img/RSTools-pkgUp.png" /></p>
</div>
<div id="jobs" class="section level3" number="3.1.4">
<h3><span class="header-section-number">3.1.4</span> Jobs</h3>
<div class="float">
<img src="src/img/RSTools-jobs2.png"
alt="Jobs pane with 2 finished jobs" />
<div class="figcaption">Jobs pane with 2 finished jobs</div>
</div>
<p>Click on the <img src="src/img/RSTools-jobsButton.png"
alt="start local job" /> button or source a file as a local job to run
an R script in a background R session.</p>
<p><img src="src/img/RSTools-jobs4.png" /></p>
<p>By this way, you can continue to work on R-Studio with an available
console while a script is running.</p>
<div class="float">
<img src="src/img/RSTools-jobs.png"
alt="Jobs pane with a running job showing some outputs" />
<div class="figcaption">Jobs pane with a running job showing some
outputs</div>
</div>
<div class="note">
<p>You can find more information on this <a
href="https://blog.rstudio.com/2019/03/14/rstudio-1-2-jobs/">R-Studio
blog post</a>.</p>
</div>
<!-- ### Build -->
<!-- ![Build pane](src/img/RSTools-Build.png) -->
<!-- When you develop a R package, you can manage your test / checks / build in the pane.-->
</div>
<div id="git" class="section level3" number="3.1.5">
<h3><span class="header-section-number">3.1.5</span> Git</h3>
<p>R-Studio have an integrated GIT client. <img
src="src/img/RSTools-Git.png" alt="Build pane" /></p>
<p>If you work with Git in your R-Project, you can run the main git
commands in this pane:</p>
<ul>
<li>Stage / commit</li>
<li>Pull / Push</li>
<li>Create / checkout branches</li>
</ul>
<p>You can also open a dedicated window to see your history and the
“file differences”: <code>Ctrl</code> + <code>Alt</code> +
<code>D</code>.</p>
</div>
<div id="tutorial" class="section level3" number="3.1.6">
<h3><span class="header-section-number">3.1.6</span> Tutorial</h3>
<p>Some R packages provide tutorials thanks to the <a
href="https://rstudio.github.io/learnr/"><code>learnr</code></a>
package. You can try them in this pane.</p>
<div class="float">
<img src="src/img/RSTools-tuto.png"
alt="Data basics tutorial running in the pane" />
<div class="figcaption">Data basics tutorial running in the pane</div>
</div>
</div>
</div>
<div id="addins" class="section level2" number="3.2">
<h2><span class="header-section-number">3.2</span> Addins</h2>
<p>Some R packages include some R-Studio “addins”.</p>
<p>You can launch an addin using the <img
src="src/img/RS-AddinsButton.png" alt="addin" /> button.</p>
<p>These addins provides new functionality to R studio like: <a
href="https://github.com/daattali/colourpicker">Color picker</a>, <a
href="https://github.com/MilesMcBain/datapasta">create data.frame from
clipboard</a>…</p>
<div class="float">
<img src="src/img/RS-Addins.png"
alt="Addins can be installed with “Browse RStudio addins”" />
<div class="figcaption">Addins can be installed with “Browse RStudio
addins”</div>
</div>
</div>
<div id="commands-palette" class="section level2" number="3.3">
<h2><span class="header-section-number">3.3</span> Commands palette</h2>
<p>From version 1.4, R-Studio have a commands palette you can trigger
with <code>Ctrl + Shift + P</code>. This commands palette can let you
search and execute R-Studio’s commands.</p>
<p>You can try typing to get an idea of what is possible:</p>
<ul>
<li><code>create</code></li>
<li><code>rename</code></li>
<li><code>find</code></li>
<li><code>python</code></li>
</ul>
<div class="float">
<img src="src/img/RS-CommandsPalette.png" alt="Commands Palette" />
<div class="figcaption">Commands Palette</div>
</div>
<div class="note">
<p>More info <a
href="https://www.rstudio.com/blog/rstudio-v1-4-preview-command-palette/">on
R-Studio’s website</a></p>
</div>
</div>
<div id="Rproj" class="section level2" number="3.4">
<h2><span class="header-section-number">3.4</span> R-Project</h2>
<p>When you work, <del>I hope</del> you keep all your files associated
with a project in one specific directory in your computer. R-Studio
provides a built-in support for this via “projects”.</p>
<p>You can find some information about R-Project in this <a
href="https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects">R-Studio
support article</a> or at this <a
href="https://r4ds.had.co.nz/workflow-projects.html#rstudio-projects"><em>R
for Data Science</em> section</a>.</p>
<div id="advantages" class="section level3" number="3.4.1">
<h3><span class="header-section-number">3.4.1</span> Advantages</h3>
<p>The main advantages to work with R-Projects are:</p>
<ul>
<li><p>When an R-Project is open, the current working directory is
automatically set to the project directory. An R-Project can then be
<strong>shared across multiple users</strong> on different computers
without thinking about setting the working directory.</p></li>
<li><p>When an R-Project is open, you get back to where you left off:
The <strong>previously opened files are restored</strong> into the
editor tabs such as the command history (if it had been saved). Even the
files that had <em>never</em> been saved (named <code>Untitled1</code>
in the editor) are restored.</p></li>
<li><p>You can use Git from R-Studio.</p></li>
</ul>
<div class="note">
<p>Of course, the global environment is not restored if R-Studio is set
up correctly, as explained in this <a
href="#generalSettings">section</a>.</p>
</div>
</div>
<div id="createRproj" class="section level3" number="3.4.2">
<h3><span class="header-section-number">3.4.2</span> Create
R-Project</h3>
<p>You can create a new R-Project either by clicking on
<code>File -> New Project...</code> or on the <img
src="src/img/RS-project.png" alt="project" /> button.</p>
<p>You can then choose to create it in an existing directory, or to
create a new one. In this case, R-Studio provides different templates:
“R package”, “Shiny Web Application”, “Plumber API” … After creation,
the directory will already contain some files and folder.</p>
<div class="float">
<img src="src/img/RS-project2.png" alt="Differents project templates" />
<div class="figcaption">Differents project templates</div>
</div>
<p>There is no template for data analysis projects, but a simple
structure can be:</p>
<pre><code>## projectExample/
## ├── analysis.R
## ├── data
## ├── output
## ├── projectExample.Rproj
## └── src</code></pre>
</div>
<div id="how-it-works" class="section level3" number="3.4.3">
<h3><span class="header-section-number">3.4.3</span> How it works</h3>
<p>R project is a directory containing a <code>.Rproj</code> file which
can be open by R-Studio. When you open a <code>.Rproj</code> file, the
following happens:</p>
<ul>
<li>A new R session (process) is started</li>
<li>The <code>.Rprofile</code> file in the project’s main directory (if
any) is sourced by R</li>
<li>The <code>.RData</code> file in the project’s main directory is
loaded (if project options indicate that it should be loaded).</li>
<li>The <code>.Rhistory</code> file in the project’s main directory is
loaded into the R-Studio history pane (and used for Console Up/Down
arrow command history).</li>
<li>The current working directory is set to the project directory.</li>
<li>Previously edited source documents are restored into editor
tabs</li>
<li>Other R-Studio settings (e.g. active tabs, splitter positions, etc.)
are restored to where they were the last time the project was
closed.</li>
</ul>
<p>The <code>.Rproj</code> file is a text file (you can try to open it
with a text editor) containing the project settings. These settings can
be modified in <code>Tools -> Project Options...</code>. It is
essential to know that <strong>these settings have priority</strong>
over the standard R-Studio settings.</p>
<h2>
</h2>
<div class="note">
<ul>
<li><p>If your R project is included in a Google Drive folder, you can
encounter some troubles when saving your files.</p></li>
<li><p>Even if the <code>.Rproj.user</code> folder is not so big, it can
contain a lot of small files. It can then take a long time to be
synchronized on clouds services.</p></li>
</ul>
</div>
</div>
</div>
<div id="keyboard-shortcuts-and-useful-command" class="section level2"
number="3.5">
<h2><span class="header-section-number">3.5</span> Keyboard shortcuts
and useful command</h2>
<p>R-Studio provide a lot of <strong>handy commands</strong> accessible
from the menu bar (<code>File</code>, <code>Edit</code>,
<code>Code</code>, <code>Session</code> … ).</p>
<p>These commands are linked to keyboard shortcuts. Even if it is not
easy to learn, it will significantly improve your comfort (and
productivity).</p>
<p>The default shortcuts can be modified through:
<code>Tools -> Modify keyboard shortcuts...</code></p>
<p>The list below is just a non-exhaustive list of my favourite
shortcuts / functionality.</p>
<div class="note">
<ul>
<li>Remember, if you are doing an annoying and repetitive task, there is
probably a shortcut for that!</li>
</ul>
</div>
<div id="my-top-5" class="section level3" number="3.5.1">
<h3><span class="header-section-number">3.5.1</span> My top 5</h3>
<p>First a quick summary of my most useful ones:</p>
<table>
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<thead>
<tr class="header">
<th>Shortcut</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>Ctrl</code> + <code>Shift</code> + <code>F10</code></td>
<td>Restart R the session.</td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>Shift</code> + <code>C</code></td>
<td>Toggle current/selected line(s)</td>
</tr>
<tr class="odd">
<td><code>F1</code></td>
<td>Show function help</td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>Enter</code></td>
<td>Run current/selected lines(s)</td>
</tr>
<tr class="odd">
<td><code>Ctrl</code> + <code>F7</code></td>
<td>Add a new source column (you can remove it by closing all its tabs
(<code>Ctrl</code> + <code>W</code>))</td>
</tr>
</tbody>
</table>
</div>
<div id="basic" class="section level3" number="3.5.2">
<h3><span class="header-section-number">3.5.2</span> Basic</h3>
<p>Some of these shortcuts are similar to those of other software.</p>
<table>
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<thead>
<tr class="header">
<th>Shortcut</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>Ctrl</code> + <code>Z</code></td>
<td>Undo</td>
</tr>
<tr class="even">
<td><strong><code>Ctrl</code> + <code>Shift</code> +
<code>Z</code></strong></td>
<td>Redo (this is not the usual convention!)</td>
</tr>
<tr class="odd">
<td><code>Ctrl</code> + <code>O</code></td>
<td><strong>O</strong>pen file</td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>A</code></td>
<td>Select <strong>a</strong>ll</td>
</tr>
<tr class="odd">
<td><code>Ctrl</code> + <code>S</code></td>
<td><strong>S</strong>ave file</td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>F</code></td>
<td><strong>F</strong>ind in the current file</td>
</tr>
<tr class="odd">
<td><strong><code>Ctrl</code> + <code>Shit</code> +
<code>F</code></strong></td>
<td><strong>F</strong>ind in files (to search across multiple
files)</td>
</tr>
<tr class="even">
<td><strong><code>Ctrl</code> + <code>w</code></strong></td>
<td>Close tab</td>
</tr>
<tr class="odd">
<td><strong><code>Ctrl</code> + <code>Tab</code></strong></td>
<td>Go to next tab</td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>Q</code></td>
<td>Quit R-Studio</td>
</tr>
<tr class="odd">
<td><code>Tab</code></td>
<td>Indent / Autocompletion</td>
</tr>
<tr class="even">
<td><code>Shift</code> + <code>Tab</code></td>
<td>Outdent</td>
</tr>
<tr class="odd">
<td><strong><code>Ctrl</code> + <code>Shift</code> +
<code>F10</code></strong></td>
<td>Restart R session. <strong>You should do it quite
often!</strong></td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>Shift</code> + <code>H</code></td>
<td><del>Choose working directory</del> Not needed because you are
working in an R project. 😉</td>
</tr>
<tr class="odd">
<td><strong><code>Ctrl</code> + <code>F7</code></strong></td>
<td>Add a new source column (you can remove it by closing all its tabs
(<code>Ctrl</code> + <code>W</code>))</td>
</tr>
</tbody>
</table>
<div class="float">
<img src="src/img/KS-NewSourceColumn.png"
alt="Layout with two source columns. 2 files can be opened side by side." />
<div class="figcaption">Layout with two source columns. 2 files can be
opened side by side.</div>
</div>
</div>
<div id="help" class="section level3" number="3.5.3">
<h3><span class="header-section-number">3.5.3</span> Help</h3>
<table>
<thead>
<tr class="header">
<th>Shortcut</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong><code>Alt</code> + <code>Shift</code> +
<code>K</code></strong></td>
<td>Show <strong>k</strong>eyboard shortcuts</td>
</tr>
<tr class="even">
<td><code>F1</code></td>
<td>Show function help</td>
</tr>
<tr class="odd">
<td><strong><code>Ctrl</code> + <code>Click</code></strong></td>
<td>(on a function) Go to function definition.</td>
</tr>
</tbody>
</table>
</div>
<div id="run-code" class="section level3" number="3.5.4">
<h3><span class="header-section-number">3.5.4</span> Run code</h3>
<table>
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<thead>
<tr class="header">
<th>Shortcut</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>Ctrl</code> + <code>Enter</code></td>
<td>Run selected lines</td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>Alt</code> +
<code>B</code>/<code>E</code></td>
<td>Run code from <strong>B</strong>egining to line or from line to
<strong>E</strong>nd</td>
</tr>
<tr class="odd">
<td><code>Ctrl</code> + <code>Alt</code> + <code>Shift</code> +
<code>P</code></td>
<td>Profile selected lines (see: <a href="#profiling">profiling
section</a>)</td>
</tr>
</tbody>
</table>
</div>
<div id="editor" class="section level3" number="3.5.5">
<h3><span class="header-section-number">3.5.5</span> Editor</h3>
<table>
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<thead>
<tr class="header">
<th>Shortcut</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong><code>Alt</code> + <code>-</code></strong></td>
<td>insert <code><-</code></td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>Shift</code> + <code>M</code></td>
<td>insert a pipe <code>%>%</code></td>
</tr>
<tr class="odd">
<td><code>Ctrl</code> + <code>Shift</code> + <code>C</code></td>
<td>(un)<strong>C</strong>omment code <img src="src/img/KS-comment.gif"
alt="Ctrl + Shift + C" /></td>
</tr>
<tr class="even">
<td><code>Ctrl</code> + <code>Shift</code> + <code>/</code></td>
<td>Reflow comment <img src="src/img/KS-comment2.gif"