-
Notifications
You must be signed in to change notification settings - Fork 2
/
Getting_started_with_R.Rnw
1778 lines (1500 loc) · 51.6 KB
/
Getting_started_with_R.Rnw
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
\documentclass[xcolor=dvipsnames, aspectratio=1610, 11pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} % so that dollar sign does not turn into pound when italic!!
\usepackage{amsmath,amssymb,lmodern} % nice T1 compatible fonts
\usepackage[UKenglish]{babel}
\usepackage{pdfpages} % to import pdf pages
\usepackage{ragged2e} % pour justifier le text, après il suffit de tapper \justifying avant le paragraphe
\usepackage{subcaption} % for captions below figures
\usepackage{eurosym} % for euro sign
\setbeamertemplate{navigation symbols}{}%no nav symbols
\usetheme[secheader]{Madrid}%
\usepackage{colourchange}% to change theme colours on the go
\def\r{{\bf R}}
\title{Getting started with R}
\author[IZW]{Alexandre Courtiol @datazoogang.de $\vcenter{\hbox{\includegraphics[height=1em]{figures/bluesky}}}$}
\institute[]{Leibniz Institute of Zoo and Wildlife Research}%
\titlegraphic{
\vspace{1cm}
\centering
\includegraphics[height=2cm]{figures/izw_logo}
\hspace{2cm}
\includegraphics[height=2cm]{figures/FU}
}
\date[February 2024]{\small February 2024}%
\begin{document}
\selectmanualcolour{blue!75!black}%set main colour
\setlength{\topsep}{1pt} % space between input and output
<<knitr options, echo = FALSE, message = FALSE>>=
options(width = 100,
tibble.print_min = 3,
tibble.print_max = 3)
library(knitr)
opts_chunk$set("size" = "scriptsize",
"error" = TRUE,
"fig.align" = "center",
fig.width = 6,
fig.asp = 0.64,
out.width = "7cm",
cache = FALSE)
@
\AtBeginSection[]{
\begin{frame}
\frametitle{Getting started with \r}
\setcounter{tocdepth}{1}
\tableofcontents[currentsection]
\end{frame}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
\titlepage
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t]{What is \r?}
\r \ is a \underline{programming language} and software environment for statistical computing \& graphics
\pause
\vspace{2em}
Key points (for this course):
\vspace{1em}
\begin{itemize}
\item \r \ includes {\bf long-established parametric and non-parametric tests},\\ as well as cutting edge statistical methods
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[t]{What is \r?}
\r \ is a \underline{programming language} and software environment for statistical computing \& graphics
\vspace{2em}
Key points (general):
\vspace{1em}
\begin{itemize}
\item interactive (you can explore each step of your workflow in details)
\item flexible (you can create your own functions)
\item transparent (you use scripts which allows for reproducible research)
\item free (\url{https://www.r-project.org/COPYING})%GPL2
\item open (\url{https://github.com/wch/r-source})
\item honest (\url{https://bugs.r-project.org/bugzilla})
\item scalable (laptop/supercomputer; local/remote; Windows/MacOS/Linux/Unix)
\item rich\\ (\url{https://rdrr.io}; \url{https://www.rdocumentation.org}; \url{https://r-universe.dev})
\item up to date (\url{https://dirk.eddelbuettel.com/cranberries/cran/updated})
\item friendly community (more on that later)
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{What is \r \ good for?}
\begin{columns}
\column[t]{0.4\linewidth}
\begin{block}{\r \ is good for:}
\begin{itemize}
\item data manipulation
\item statistical analyses
\item plotting
\item programming around data
\end{itemize}
\end{block}
\pause
\column[t]{0.4\linewidth}
\begin{block}{\r \ is not optimal for:}
\begin{itemize}
\item data entry
\item formal algebra
\item general-purpose programming
\item beginners (yet, getting easier)
\end{itemize}
\end{block}
\end{columns}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Getting started with \r}
\setcounter{tocdepth}{1}
\tableofcontents
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Installation}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Installation steps}
\begin{enumerate}
%\item connect to the WIFI network \texttt{izw-gast} (check password on blackboard)%
%\item set the internet proxy: 192.168.2.2:3128 (necessary at IZW, but usually not)
%\item check that you do get internet access
\item install \r: \url{https://cran.r-project.org/}
\item install RStudio: \url{https://posit.co/download/rstudio-desktop/}
\item open RStudio
\end{enumerate}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{RStudio}
\subsection{introduction}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{The RStudio Integrated Development Environment (IDE)}
\begin{center}
\includegraphics[width=0.9\linewidth]{figures/RStudio.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Why using RStudio?}
You can use \r \ without RStudio, but RStudio is:
\begin{itemize}
\item provided with more functionalities than the official \r \ IDE
\item integrated with several packages developed by RStudio (e.g. \verb`{readr}`)
\item suitable for both desktop and remote computers (web server) %so it looks the same on a cluster!
\item free and open source
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Overall structure}
\begin{itemize}
\item Top menu
\item Tool bar (small subset from the top menu)
\item 4 visible panes
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{top menu}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{The top menu}
\includegraphics[width = \linewidth]{figures/RStudio_topmenu.png}
\vfill
\large{10 most useful items:}
\begin{itemize}
\item \texttt{File $\rightarrow$ New File}
\item \texttt{File $\rightarrow$ New Project...}
\item \texttt{File $\rightarrow$ Import Dataset}
\item \texttt{File $\rightarrow$ Save}
\item \texttt{Edit $\rightarrow$ Undo}
\item \texttt{Edit $\rightarrow$ Clear console}
\item \texttt{Code $\rightarrow$ Comment/Uncomment Lines}
\item \texttt{Tools $\rightarrow$ Global Options...}
\item \texttt{Help $\rightarrow$ Check for Updates} (for updating RStudio, not \r, nor \r \ packages)
\item \texttt{Help $\rightarrow$ Cheatsheets}
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\selectmanualcolour{red!75!black}
\begin{frame}[fragile]{Practice}
\begin{enumerate}
\item create a new project
\item create a new \r \ Script file
\item save the created \r \ Script file into the project folder directory
\item have a look at the RStudio cheatsheet
\end{enumerate}
\vfill
\textbf{NB:}
\begin{itemize}
\item a project is defined by a simple (text) file. Its main benefit is to open RStudio with the correct working directory set (that is, the one where the project file is)
\item an \r \ Script file is a (text) file where we can write \r \ code
\end{itemize}
\end{frame}
\selectmanualcolour{blue!75!black}%set main colour
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{My tips about the global options}
1. I never save or restore the workspace and so should you (default settings are DANGEROUS)
\begin{center}
\includegraphics[height=0.8\textheight]{figures/RStudio_settings.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{My tips about the global options}
2. I don't like parentheses and quotes auto-completion \& I like the native pipe
\begin{center}
\includegraphics[height=0.8\textheight]{figures/RStudio_settings2.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{My tips about the global options}
3. I activate all the code diagnostics (but one) offered by RStudio!
\begin{center}
\includegraphics[height=0.8\textheight]{figures/RStudio_settings3.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{My tips about the global options}
4. I sometimes use a black theme
\begin{center}
\includegraphics[height=0.8\textheight]{figures/RStudio_settings4.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{My tips about the global options}
5. I usually put the \texttt{Source} and the \texttt{Console} panes side-by-side
\begin{center}
\includegraphics[height=0.8\textheight]{figures/RStudio_settings5.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{the panes}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{The \texttt{Source} pane}
This is where you type the code you want to keep
\begin{center}
\includegraphics[height=0.8\textheight]{figures/RStudio_pane1.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{The \texttt{Console} pane}
This is where you can run \r \ code directly
\begin{center}
\includegraphics[height=0.8\textheight]{figures/RStudio_pane2.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{The other panes}
This is where everything else is
\begin{center}
\includegraphics[height=0.8\textheight]{figures/RStudio_pane34.png}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Tips}
\begin{enumerate}
\item only use the \texttt{Console} pane to mess around
\item write proper \r \ code in the \texttt{Source} pane
\item comment thoroughly your \r \ script using \# signs
\item re-run frequently your entire script to make sure that it works\\
(after restarting the session: Session/Restart R)
\end{enumerate}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Basics of the language}
\subsection{arithmetic}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Arithmetic \& Logic}
\begin{columns}[T]
\column{0.5\linewidth}
\r \ can perform basic arithmetic:
<<arithmetic>>=
1 + 1
1 - 1
2 * pi
3 / 2
5^2
5^(2 + 1)
Inf/Inf
@
\column{0.5\linewidth}
\pause
\r \ can perform logical operations:
<<logic1>>=
1 == 1
(1 == 1) & (1 == 2)
(1 == 1) | (1 == 2)
1 != 2
!(1 == 2)
2 >= 1
2 < 1
@
\end{columns}
\pause
\vspace{1em}
As for most other programming languages, avoid equality tests for floating-point numbers!
% https://www.gao.gov/assets/imtec-92-26.pdf
% https://web.ma.utexas.edu/users/arbogast/misc/disasters.html
<<logic4>>=
0.8 - 0.3 - 0.5 == 0.8 - 0.5 - 0.3
## check later ?all.equal() and ?dplyr::near()
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\selectmanualcolour{red!75!black}
\begin{frame}[fragile]{Practice}
\vfill
\begin{center}
\begin{Huge}
Compute {\Huge $\sqrt{\frac{2^{3+1}}{\frac{4}{5\times{6}}}-20}$} % = 10
\end{Huge}
\end{center}
\vfill
\end{frame}
\selectmanualcolour{blue!75!black}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{objects}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Creating objects}
Objects are being assigned using the ``arrow'' operator:
<<one plus one>>=
one_plus_one <- 1 + 1 ## storing the result
@
\pause
\vfill
Objects are being used through their name (that is the whole point):
<<display one plus one>>=
one_plus_one ## displaying the result
one_plus_one_plus_one <- one_plus_one + 1
one_plus_one_plus_one
@
\pause
\vfill
{\bf Tips:}
<<one times two>>=
(one_times_two <- 1 * 2) ## storing and displaying the result at once
@
\vfill
\pause
\begin{itemize}[<+->]
\item \verb+->+ works too (if you switch the left hand side and the right hand side)
\item ``\texttt{\_}'' and ``\texttt{.}'' are OK but avoid spaces \& other weird characters in names
\item names are CaSe sensITIVE
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{functions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Functions}
<<citation, eval=FALSE>>=
citation() ## function showing how to cite R
@
<<citation run, eval=TRUE, echo=FALSE, size="tiny">>=
citation()
@
\vfill
\pause
<<help citation, eval=FALSE, tidy=FALSE>>=
help(citation) ## getting help for this function
@
\vfill
<<eval = FALSE>>=
?citation() ## same but shorter (syntactic sugar)
@
\vfill
\pause
\textbf{NB:} it is best to look at the help before using a function new to you!
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\selectmanualcolour{red!75!black}
\begin{frame}[fragile]{Practice}
\vfill
\begin{center}
\begin{large}
Display the path of any file you want using \texttt{file.choose()}
\end{large}
\end{center}
\vfill
\end{frame}
\selectmanualcolour{blue!75!black}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Syntax for functions that take arguments}
Basic syntax:
<<example function>>=
sign(x = -5)
sign(-5)
@
\vfill
\pause
Both are synonymous, but:
\begin{itemize}
\item the first syntax is safer
\item the second syntax removes visual clutter
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Functions}
<<eval=F, echo=T>>=
mean()
@
<<eval=F, tidy=F>>=
?mean()
@
\begin{scriptsize}
\begin{verbatim}
Usage:
mean(x, ...)
## Default S3 method:
mean(x, trim = 0, na.rm = FALSE, ...)
Arguments:
x: An R object. Currently there are methods for numeric/logical
vectors and date, date-time and time interval objects, and
for data frames all of whose columns have a method. Complex
vectors are allowed for ‘trim = 0’, only.
trim: the fraction (0 to 0.5) of observations to be trimmed from
each end of ‘x’ before the mean is computed. Values of trim
outside that range are taken as the nearest endpoint.
na.rm: a logical value indicating whether ‘NA’ values should be
stripped before the computation proceeds.
[...]
\end{verbatim}
\end{scriptsize}
\end{frame}
\begin{frame}[fragile]{Functions}
All these calls are equivalent:
<<>>=
mean(x = c(1, 5, 3, 4))
@
<<>>=
mean(c(1, 5, 3, 4))
@
<<>>=
vector.of.numbers <- c(1, 5, 3, 4)
mean(x = vector.of.numbers)
@
<<>>=
mean(vector.of.numbers)
@
<<>>=
c(1, 5, 3, 4) |> mean() ## see ?pipeOp
@
<<>>=
vector.of.numbers |> mean()
@
<<>>=
vector.of.numbers |> mean(x = _)
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\selectmanualcolour{red!75!black}
\begin{frame}[fragile]{Practice}
<<>>=
new.vector <- c(1, 2, 3, NA, 5)
@
\vfill
\begin{large}
\begin{center}
Try to compute the mean of \texttt{new.vector} using \texttt{mean()}!
\end{center}
\end{large}
\end{frame}
\selectmanualcolour{blue!75!black}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Finding functions}
To find the name of the function you are look for, you may try:
<<finding function, eval = FALSE>>=
??"linear model" ## if you want something that fits a linear model
@
or
<<finding function 2, eval = FALSE>>=
help.search(pattern = "linear model", package = "stats") ## if you know where to look for
@
or
\begin{center}
\includegraphics[height = 5cm]{figures/RStudio_search_fn.png}
\end{center}
\end{frame}
\section{Packages}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{The concept of an \r \ package}
Packages extend \r \ functionalities:
\begin{itemize}
\item for most users; e.g. \verb+{dplyr}+, \verb+{ggplot2}+
\item for specific users; e.g. \verb+{IsoriX}+, \verb+{hyenaR}+
\item for developers; e.g. \verb+{devtools}+, \verb+{Rcpp}+
\end{itemize}
\vfill
\pause
Key facts about packages:
\begin{itemize}
\item a package is just a folder (often compressed) containing functions, data \& documentation
\item a library is the installed version of the package (also a folder)
\item there are tons of packages out there:
\url{https://rdrr.io}; \url{https://www.rdocumentation.org}; \url{https://r-universe.dev}
\end{itemize}
%\vfill
%\pause
%\textbf{NB:} packages can be used to create research compendia!
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Installing a package}
In general, the installation procedure depends on:
\begin{itemize}
\item where the package is being hosted (local, CRAN, bioconductor, GitHub, other)
\item if the package contains sources in another language that have been compiled or not
\end{itemize}
\vfill
\pause
{\bf Tip:} in order to be able to install packages that require compilation (and thus have access to more or newer versions of packages), you need to install:
\begin{itemize}
\item \texttt{Rtools} if you use Windows (\url{https://cran.r-project.org/bin/windows/Rtools})
\item \texttt{clang} and \texttt{gfortran} (\url{https://cran.r-project.org/bin/macosx/tools})\\ or \texttt{Xcode} (\url{https://developer.apple.com/xcode}) if you use macOS
\item if you use Linux or Unix-based system, you should already have everything you need
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Installing a package}
Simple situation: the package is available as a binary file prepared for your system on CRAN
\vfill
You can use RStudio:
\begin{center}
\includegraphics[height = 4cm]{figures/RStudio_packages_installation.png}
\end{center}
\vfill
\pause
or you can type in the \texttt{Console} pane:
\vfill
<<install package, eval=FALSE>>=
install.packages("tidyverse") ## install {tidyverse}
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\selectmanualcolour{red!75!black}
\begin{frame}[fragile]{Practice}
\begin{center}
\begin{large}
Install the \verb+{tidyverse}+!
\end{large}
\end{center}
\end{frame}
\selectmanualcolour{blue!75!black}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile]{Installing a package}
%Try to install the \verb`{tidyverse}` as shown previously\\
%\pause
%\vspace{1em}
%If the installation procedure does not start, abort, do the %following, and then try installing the package again!\\
%\vspace{1em}
%\begin{itemize}
%\item create or edit the file \texttt{.Renviron}:
%<<eval = FALSE>>=
%file.edit("~/.Renviron")
%@
%\item add to this file the following lines:
%
%\url{http_proxy=http://192.168.2.2:3128/}
%
%\url{https_proxy=http://192.168.2.2:3128/}
%\end{itemize}
%\pause
%\vfill
%{\bf Note:} if you do this, at home, you will have to comment the %lines inside the file for \r\ to be able to access internet
%\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Loading a library}
That is always simple:
<<>>=
library(tidyverse)
@
\vfill
Contrary to the installation that is only needed once per \r \ installation, you need to load the libraries each time you open a new \r \ session!
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Manipulating data}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{iris}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{The \texttt{iris} dataset}
The \texttt{iris} dataset is readily available in \r; it is often used to illustrate statistical procedures (\url{https://en.wikipedia.org/wiki/Iris_flower_data_set})
\vfill
\begin{figure}
\centering
\begin{subfigure}[b]{0.3\textwidth}
\begin{center}
\includegraphics[height=3cm]{"figures/Iris_setosa"}
\caption*{\emph{Iris setosa}\\ {\tiny \textcopyright Miya.m}}
\end{center}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\begin{center}
\includegraphics[height=3cm]{"figures/Iris_versicolor"}
\caption*{\emph{Iris versicolor}\\ {\tiny \textcopyright D.G.E. Robertson}}
\end{center}
\end{subfigure}
\begin{subfigure}[b]{0.3\textwidth}
\begin{center}
\includegraphics[height=3cm]{"figures/Iris_virginica"}
\caption*{\emph{Iris virginica}\\ {\tiny \textcopyright F. Mayfield}}
\end{center}
\end{subfigure}
\end{figure}
\vfill
<<>>=
colnames(iris) ## shows the column names of the iris dataset
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{data frames}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Data representation in \r}
The most common class of objects used for storing data in \r \ is the \underline{data frame}!\\
\vspace{1em}
\pause
Example: the \texttt{iris} dataset
<<iris>>=
iris
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Data representation in \r}
The most common class of objects used for storing data in \r \ is the \underline{data frame}!\\
\vspace{1em}
Example: the \texttt{iris} dataset
<<iris2>>=
head(iris)
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Data representation in \r}
The most common class of objects used for storing data in \r \ is the \underline{data frame}!\\
\vspace{1em}
Example: the \texttt{iris} dataset
<<iris3>>=
str(iris)
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Data representation in \r}
The most common class of objects used for storing data in \r \ is the \underline{data frame}!\\
\vspace{1em}
\begin{itemize}
\item each column usually corresponds to a \underline{vector} (a series of elements of 1 type)
\item all columns have the same length (rectangular format)
\item contains column names (usually informative) and row names (usually not informative)
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Data frames come in 2 flavours}
\begin{columns}[t]
\column{0.35\linewidth}
\begin{center}
\begin{block}{\centering original data frames\\ (class \texttt{data.frame})}
\begin{itemize}
\item perfectly fine to work with
\item don't rely on extra packages
\end{itemize}
\end{block}
\end{center}
\column{0.55\linewidth}
\begin{center}
\begin{block}{\centering modern data frames: tibbles\\ (class \texttt{tbl\_df})}
\begin{itemize}
\item improved display (see \verb+?pillar_options+)
\item lazier \& \underline{safer} (see \verb+?`tbl_df-class`+)
\item more flexible (work well with groupings)
\end{itemize}
\end{block}
\end{center}
\end{columns}
\vspace{1em}
\pause
\textbf{Tip:} functions working with data frames usually work fine with tibbles, but in case of problems just try converting your tibble into an original data frame using \texttt{as.data.frame()}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Tibbles}
You can easily turn an original data frame into a tibble
<<>>=
library(tidyverse)
iris_tbl <- as_tibble(iris) ## as.data.frame() does the opposite: tbl -> df
iris_tbl
@
\pause
\vspace{1em}
{\bf Note:} the tidyverse is a meta-package that loads several packages we are going to use
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Working with data frames}
There are two competing paradigms for manipulating data in \r:
\pause
\vspace{1em}
\begin{columns}[T]
\column{0.45\linewidth}
\begin{block}{Vector-focussed}
\begin{itemize}
\item easier for developers
\item more error prone, but easier to debug
\item more efficient (often)
\item dominant paradigm for first generations of \r\ users
\end{itemize}
\end{block}
\pause
\column{0.45\linewidth}
\begin{block}{Data frame-focussed}
\begin{itemize}
\item easier for users
\item closer to Excel, SPSS, \dots
\item \emph{functional programming} friendly
\item dominant paradigm for new generations of \r\ users
\end{itemize}
\end{block}
\end{columns}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Vector based approach}
\textbf{Goal:} computing the mean and SD of sepal length in mm per species
\vfill
<<vector way>>=
## create a new column storing the sepal length in mm
iris$Sepal.Length.mm <- iris$Sepal.Length * 10
## create data frame to store results
results <- data.frame(Species = unique(iris$Species))
## compute the mean and SD per species
for (sp in results$Species) {
results$meanSL[results$Species == sp] <- mean(iris$Sepal.Length.mm[iris$Species == sp])
results$sdSL[results$Species == sp] <- sd(iris$Sepal.Length.mm[iris$Species == sp])
}
## display the result
results
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Data frame based approach}
\textbf{Goal:} computing the mean and SD of sepal length in mm per species
\vfill
<<echo = FALSE>>=
rm(iris)
@
<<data frame way>>=
## create a new column storing the sepal length in mm
iris <- mutate(iris, Sepal.Length.mm = Sepal.Length * 10)
## compute the mean and SD per species
results <- summarise(iris, meanSL = mean(Sepal.Length.mm),
sdSL = sd(Sepal.Length.mm),
.by = Species) # declare that you will perform operations within species
## display the result
results
@
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Data frame based approach with pipes (\texttt{|>} or \texttt{\%>\%})}
\textbf{Goal:} computing the mean and SD of sepal length in mm per species
\vspace{1em}
<<echo = FALSE>>=
rm(iris)
@
<<data frame pipe way>>=
iris |>
mutate(Sepal.Length.mm = Sepal.Length * 10) |>
summarise(meanSL = mean(Sepal.Length.mm),
sdSL = sd(Sepal.Length.mm),
.by = Species) -> results
results
@
\pause
\vspace{1em}
{\bf Note:} pipes capture what is on their left and forward this as the first argument of the function that comes on their right
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\texttt{\{tidyverse\}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{The \texttt{\{tidyverse\}}}
The tidyverse (\url{https://www.tidyverse.org}) is an ecosystem of R packages.
\vspace{1em}
There are currently 9 core `tidy' packages:
\begin{center}
\raisebox{-0.5\height}{\includegraphics[width = 1.5cm]{"figures/tidyverse/hex-tidyverse"}}
=
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-tibble"}}
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-readr"}}
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-dplyr"}}
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-stringr"}}
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-lubridate"}}
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-forcats"}}
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-tidyr"}}
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-ggplot2"}}
\raisebox{-0.5\height}{\includegraphics[width = 1cm]{"figures/tidyverse/hex-purrr"}}
\end{center}
\pause
\vfill
\begin{itemize}
\item philosophy: making \r \ more accessible and more modern\\ % modern as linked to new libraries that are very efficient
\item more functions, more focussed: 1 function = 1 action = 1 verb
\item backward compatibility is not the absolute priority
\item open-source (collaborative) development supervised by Posit (formerly known as RStudio)
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{\texttt{\{dplyr\}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{Manipulating data frames with \texttt{\{dplyr\}}}
You can do a lot with 4 functions:\\
\vspace{1em}
\pause
\begin{itemize}
\item \texttt{select()} to keep or discard columns
\item \texttt{filter()} to keep or discard rows
\item \texttt{mutate()} to create or modify columns
\item \texttt{summarise()} to compute summary statistics
\end{itemize}
\vfill
\pause
{\bf Notes:}
\begin{itemize}
\item this is much more than that in \texttt{\{dplyr\}}
\item you can use the argument \texttt{.by} to define groups of rows when using \texttt{filter()}, \texttt{mutate()} or \texttt{summarise()}
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{\texttt{select()}}
\begin{columns}[t]
\column{0.475\linewidth}
Keep columns using \texttt{select()}:\\
<<select details>>=
iris_tbl |>
select(Sepal.Length, Sepal.Width)
@
\pause
\column{0.475\linewidth}
Discard columns using \texttt{select()}:\\
<<select details2>>=
iris_tbl |>
select(-Sepal.Length, -Sepal.Width)
@
\end{columns}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile, t]{\texttt{filter()}}
Keep rows using \texttt{filter()}:\\
<<filter details>>=
iris_tbl |> filter(Sepal.Length > 7, Sepal.Width > 3)