Skip to Content
R Programming By Example
book

R Programming By Example

by Omar Trejo Navarro
December 2017
Beginner to intermediate
470 pages
12h 29m
English
Packt Publishing
Content preview from R Programming By Example

Encapsulation and mutability

Now, we will take a look at the encapsulation and mutability concepts in S4. First, note that we are using the print() and not the S4print() method, because we are printing specific slots from S4_rectangle. As you can see, if we're not careful, we can still assign values directly to the internals of the object. Again, you should not do this.

Also note that if we use the method S4color() that we created before to encapsulate the access to the color attribute, we get an error telling us that the S4color<- function could not be found. That hints to us that we can create such a function, and we can:

print(S4_rectangle@a)#> [1] 2S4_rectangle@a <- 1print(S4_rectangle@a)#> [1] 1print(S4color(S4_rectangle))#> [1] "blue" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Efficient R Programming

Efficient R Programming

Colin Gillespie, Robin Lovelace
R Programming

R Programming

Jared P. Lander

Publisher Resources

ISBN: 9781788292542Supplemental Content