forked from lgatto/2017-04-03-adv-r-progr-EMBL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
41 lines (29 loc) · 966 Bytes
/
README.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
---
output: md_document
---
# Object-oriented programming in R
Object-oriented programming (OOP) can help us encapsulate complex data structures into a simple interface.
## OOP concepts
- Encapsulation - related data is stored and handled together
- Polymorphism - the most appropriate function is called based on the object type (e.g various plot functions)
- Inheritance - code reuse by hierarchy of more-to-less general object types
In R, everything has a class (i.e type):
```{r}
class("Hello")
class(1:5)
class(2.1)
class(table(c(1,4,5,3,2,1)))
class(plot)
x = 10
class(x)
```
# OOP in R
Unlike other main-stream languages, R has multiple OOP systems that co-exist in parallel:
- S3 - used in base R
- S4 - _de-facto_ standard in Bioconductor
- Reference classes (RC) - used in special use cases
S3 and S4 are similar, but S4 is more formal. RC is quite different and is Java-like.
# Sections
- [S3 OOP](s3.md)
- [S4 OOP](s4.md)
- [RC OOP](rc.md)