Skip to content

Commit

Permalink
Merge pull request #9 from gbdias/master
Browse files Browse the repository at this point in the history
fix render failure
  • Loading branch information
gbdias authored Oct 15, 2024
2 parents 493c3a4 + 0837193 commit 3bc5b11
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions slide_r_elements_3.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -525,21 +525,6 @@ ikea_sweden$ikea_uppsala$park
```


---
name: lists_2
exclude: true

# Lists — collections of various data types

Elements of a list can also be different data structures:

```{r lists2, echo=T, eval=F}
weight <- matrix(sample(1:9, size = 9), nrow=3)
data <- list(name, weight)
data
data[[2]][3]
```

---
name: objects_type_class
exclude: true
Expand All @@ -548,7 +533,7 @@ exclude: true

An object of class **factor** is internally represented by numbers:

```{r obj.type.class, echo=T}
```{r obj.type.class, echo=T, eval=F}
size <- factor('small')
class(size) # Class 'factor'
mode(size) # Is represented by 'numeric'
Expand Down Expand Up @@ -577,7 +562,7 @@ exclude: true

We can easily modify values of object's **attributes**:

```{r obj.fix, echo=T}
```{r obj.fix, echo=T, eval=F}
attributes(his)
attr(his, "names")
#fix(his) # Opens an object editor
Expand All @@ -590,7 +575,7 @@ exclude: true
# Lists as S3 classes

A list that has been named, becomes an S3 class:
```{r obj.S3.list, echo=T}
```{r obj.S3.list, echo=T, eval=F}
my.list <- list(numbers = c(1:5),
letters = letters[1:5])
class(my.list)
Expand All @@ -607,7 +592,7 @@ exclude: true
# S3 classes

For an S3 class we can define a *generic function* applicable to all objects of this class.
```{r obj.S3, echo=T}
```{r obj.S3, echo=T, eval=F}
print.my.list.class <- function(x) {
cat('Numbers:', x$numbers, '\n')
cat('Letters:', x$letters)
Expand All @@ -617,7 +602,7 @@ print(my.list)

But here, we have no error-proofing. If the object will lack *numbers*, the function will still be called:

```{r obj.S3.error, echo=T}
```{r obj.S3.error, echo=T, eval=F}
class(his) <- 'my.list.class' # alter class
print(his) # Gibberish but no error...
```
Expand All @@ -637,7 +622,7 @@ exclude: true
# S4 class mechanism

S4 classes are more advanced as you actually define the structure of the data within the object of your particular class:
```{r S4, echo=T}
```{r S4, echo=T, eval=F}
setClass('gene',
representation(name='character',
coords='numeric')
Expand All @@ -654,7 +639,7 @@ exclude: true

The variables within an S4 class are stored in the so-called **slots**. In the above example, we have 2 such slots: *name* and *coords*. Here is how to access them:

```{r S4.slots, echo=T}
```{r S4.slots, echo=T, eval=F}
my.gene@name # access using @ operator
my.gene@coords[2] # access the 2nd element in slot coords
```
Expand All @@ -667,7 +652,7 @@ exclude: true

The power of classes lies in the fact that they define both the data types in particular slots and operations (functions) we can perform on them. Let us define a *generic print function* for an S4 class:

```{r S4.methods, echo=T}
```{r S4.methods, echo=T, eval=F}
setMethod('print', 'gene',
function(x) {
cat('GENE: ', x@name, ' --> ')
Expand Down

0 comments on commit 3bc5b11

Please sign in to comment.