-
Notifications
You must be signed in to change notification settings - Fork 10
/
slide_r_intro.Rmd
482 lines (321 loc) · 9.8 KB
/
slide_r_intro.Rmd
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
---
title: "Introduction to R"
subtitle: "R Foundations for Data Analysis"
author: "Marcin Kierczak and Nima Rafati"
keywords: bioinformatics, course, scilifelab, nbis, R
output:
xaringan::moon_reader:
encoding: 'UTF-8'
self_contained: false
chakra: 'assets/remark-latest.min.js'
css: 'assets/slide.css'
lib_dir: libs
include: NULL
nature:
ratio: '4:3'
highlightLanguage: r
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
slideNumberFormat: "%current%/%total%"
---
exclude: true
count: false
```{r,echo=FALSE,child="assets/header-slide.Rmd"}
```
<!-- ------------ Only edit title, subtitle & author above this ------------ -->
```{r,echo=FALSE,message=FALSE,warning=FALSE}
# load the packages you need
#library(dplyr)
#library(tidyr)
#library(stringr)
#library(ggplot2)
#library(mkteachr)
```
---
name: content
class: spaced
# Contents
* [Why R](#whyR)
* [About R](#about)
* [Pros and cons of R](#pros_and_cons)
* [Ecosystem of packages](#num_packages)
* [Programming language](#programming_language)
* [Packages](#packages)
* [Package installation](#pkg_cran_inst)
---
name: whyR
# WHY R?
.pull-left-50[
![](data/slide_intro/Lab_data_no_sense.png)
]
# With the power of R, your project can ARISE:
--
**A**utomative: Streamline your analysis
--
**R**eproducible: Reproduce the results
--
**I**nterpretable: Make sense
--
**S**hareable: Share with colleagues/mentors (FAIR)
--
**E**xplainable: Easy to explain
---
name: about
# Briefly about R
.pull-left-50[
# R is ...
* a programming language
* a programming platform (= environment + interpreter)
* a software project driven by the core team and the community
* a very powerful tool for statistical computing
* a very powerful computational tool in general
* a catalyst between an idea and its realization
]
--
.pull-right-50[
# R is not ...
* a tool to replace a statistician
* the very best programming language
* the most elegant programming solution
* the most efficient programming language
]
--
.pull-left-70[
# Yet ...
* it is very elegant
* it becomes more and more feature-rich
]
.pull-right-30[
![](data/common/r.png)
]
---
name: timeline
---
name: pros_and_cons
class: spaced
# Pros and cons
<i class='fa fa-minus-square'></i> steep learning curve
--
<i class='fa fa-plus-square'></i> uniform, clear and clean system of documentation and help
--
<i class='fa fa-minus-square'></i> difficulties due to a limited object-oriented programming capabilities,
e.g. an agent-based simulation is a challenge
--
<i class='fa fa-plus-square'></i> good interconnectivity with compiled languages like Java or C
--
<i class='fa fa-minus-square'></i> cannot order a pizza for you (?)
--
<i class='fa fa-plus-square'></i> a very powerful ecosystem of packages
--
<i class='fa fa-plus-square'></i> free and open source, GNU GPL and GNU GPL 2.0
--
<i class='fa fa-plus-square'></i> easy to generate high quality graphics
---
name: programming_language
# Programing Language
--
> Programming is a process of instructing a computer to perform a specific task. We write these instructions by **programming language**. It can be as simple as calculation (like a calculator) or complex applications.
--
* flow of _data_
--
* Data is collected information which qualitatively and/or quantitatively describe an entity.
--
* Data is collected from quite diverse sources (data types).
--
* Data processing.
--
* Data cleaning.
--
```{r,out.width="75%",fig.align='center',echo=FALSE}
knitr::include_graphics("data/slide_programming/Data_Information_Knowledge.png")
```
---
# Programing Language cted.
--
* from one _function_ to another
--
* Function is a **reusable** chunk of code that performs a task. It takes **inputs** as well as **arguments** to process.
--
* each function does something to the data and return output(s)
--
* For example `mean()`, `min()`
--
---
# Three things to think about
* what *types* of data can I process?
--
* how do I *write* what I want?
--
* when does it *mean* anything?
---
# Data type
```{r,out.width="75%",fig.align='center',echo=FALSE}
knitr::include_graphics("data/slide_programming/Data_classification.png")
```
---
# Three components of a language
--
* what *types* of data can I process — *type system*
--
* int — 1 2 5 9
* double — 1.23 -5.74
* char — a b test 7 9
* logical — TRUE/FALSE (T/F)
--
* how do I *write* what I want — *syntax* defined by a language *grammar*
`2 * 1 + 1` vs. `(+ (* 2 1) 1)`
--
* when does it *mean* anything — *semantics*
--
* *Colorful yellow train sleeps on a crazy wave.* — has no generally accepted meaning
* *There is $500 on his empty bank acount.* — internal contradiction
---
name: topic2
# Where to start?
*Divide et impera* — divide and rule.
**Top-down approach:** define the big problem and split it into smaller ones. Assume you have solution to the small problems and continue — push the responsibility down.
Wishful thinking!
---
name: packages
# Packages
.pull-right-50[
```{r, out.width="250pt", fig.align='center', echo=FALSE}
knitr::include_graphics("data/slide_intro/packages.jpg")
```
]
--
* developed by the community
--
* cover several very diverse areas of science/life
--
* uniformly structured and documented
--
* organised in repositiries:
+ [CRAN](https://cran.r-project.org)
+ [R-Forge](https://r-forge.r-project.org)
+ [Bioconductor](http://www.bioconductor.org)
+ [GitHub](https://github.com)
---
name: num_packages
# Ecosystem of R packages
<br>
```{r num_pkgs, eval=FALSE, fig.align='center', fig.height=7, fig.width=9, message=FALSE, cache=TRUE, include=FALSE}
#I mute this chunk because it gives a readLines error in the 2024 course
#I add the figure instead below the chunk
#Maybe modify in the future
data <- mkteachr::update_repos_data()
gg <- mkteachr::plot_repos_data(data)
gg
```
<div style="text-align: center;">
<img src="data/slide_intro/num_pkgs.jpg" style="width:80%;">
</div>
---
name: work_with_packages
# Working with packages
Packages are organised in repositories. The three main repositories are:
* [CRAN](https://cran.r-project.org)
* [R-Forge](http://r-forge.r-project.org)
* [Bioconductor](http://www.bioconductor.org)
We also have [GitHub](https://github.com).
--
# Working with packages -- CRAN example.
```{r,out.width="80%",fig.align='center',echo=FALSE}
knitr::include_graphics("data/slide_r_environment/ggplot2_CRAN.png")
```
---
name: pkg_cran_inst
# Working with packages -- installation
Only a few packages are pre-installed:
```{r pkg.err.ex,eval=TRUE,error=TRUE}
library(modelr)
```
In order to install a package from command line, use:
```{r pkg.inst,eval=FALSE}
install.packages("ggplot2",dependencies=TRUE)
```
---
name: work_pkg_details
# Working with packages -- details
It may happen that you want to also specify the repository, e.g. because it is geographically closer to you or because your default mirror is down:
```{r pkg.inst.repo,eval=FALSE}
install.packages('ggplot2',dependencies=TRUE,repos="http://cran.se.r-project.org")
```
But, sometimes, this does not work either because the package is not available for your platform. In such case, you need to *compile* it from its *source code*.
---
name: work_pkg_details2
# Working with packages -- details cted.
```{r,out.width="150%",fig.align='center',echo=FALSE}
knitr::include_graphics("data/slide_r_environment/ggplot2_CRAN.png")
```
---
name: source_pkg_inst
# Working with packages -- installing from source.
- Download the source file, in our example *ggplot2_3.4.3.tar.gz*.
- Install it:
```{r pkg.inst.src,eval=FALSE}
install.packages("path/to/ggplot2_3.4.3.tar.gz",
repos=NULL,
type='source',
dependencies=TRUE)
```
- Load it:
```{r pkg.load,eval=FALSE}
library('ggplot2') # always forces reloading
require('ggplot2') # load only if not already loaded
```
- Enjoy!
---
name: pkg_github
# Packages -- GitHub
Nowadays, more and more developers distribute their packages via GitHub. The easiest way to install packages from the GitHub is via the *devtools* package:
- Install the *devtools* package.
- Load it.
- Install.
- Enjoy!
```{r pkg.inst.devtools.github,eval=FALSE}
install.packages('devtools',dependencies=TRUE)
library('devtools')
install_github('talgalili/installr')
```
---
name: pkg_bioconductor
# Packages -- Bioconductor
```{r,out.width="200pt",fig.align='center',echo=FALSE}
knitr::include_graphics("data/slide_r_environment/logo_bioconductor.png")
```
First install Bioconductor Manager:
```{r inst.biocond,eval=FALSE}
if (!requireNamespace("BiocManager",quietly = TRUE))
install.packages("BiocManager")
```
---
name: pkg_bioconductor2
# Packages -- Bioconductor cted.
Now, you can install particular packages from Bioconductor:
```{r biocond.inst.pkg,eval=FALSE}
BiocManager::install("GenomicRanges")
```
For more info, visit [Bioconductor website](http://www.bioconductor.org/install/).
---
# One package to rule them all -- the magic of `renv`
- first time do `renv::activate()` and `renv::init()`
- while working: `renv::hydrate()` and `renv::snapshot()`
Now, send `renv.lock` to your friend to share the environment and she can:
- restore the environment `renv::restore()`
**Pure magic!**
<!-- --------------------- Do not edit this and below --------------------- -->
---
name: end_slide
class: end-slide, middle
count: false
# Thank you! Questions?
```{r, echo=FALSE,child="assets/footer-slide.Rmd"}
```
```{r,include=FALSE,eval=FALSE}
# manually run this to render this document to HTML
#rmarkdown::render("presentation_demo.Rmd")
# manually run this to convert HTML to PDF
#pagedown::chrome_print("presentation_demo.html",output="presentation_demo.pdf")
```