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 see how S3 handles mutability and encapsulation. To do so, we will print the a value in the rectangle, modify it, and print it again. As you can see, we are able to modify it, and from that point on we get a different result, and we do so without any method calls. That's a very risky thing to do, and you should definitely wrap this type of behavior in method calls:

print(S3_rectangle$a)#> [1] 2S3_rectangle$a <- 1print(S3_rectangle$a)#> [1] 1
Even if you can, never modify an object's internals directly.

The proper way of modifying an object would be through some type of setter function. The set_color.S3Rectangle() method will be used to modify the rectangle's color, by receiving a S3Rectangle and a ...

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