Hiding Complexity with Encapsulation

Object-Oriented Programming with S3 and R6 in R

Richie Cotton

Data Evangelist at DataCamp

Object-Oriented Programming with S3 and R6 in R

Encapsulation

Object-Oriented Programming with S3 and R6 in R

Encapsulation

 

 

                                        |

implementation   |   user interface

                                        |

Object-Oriented Programming with S3 and R6 in R
microwave_oven_factory <- R6Class(
  "MicrowaveOven",
  private = list(
    power_rating_watts = 800,

door_is_open = FALSE
),
public = list(
open_door = function() {
private$door_is_open <- TRUE
}
)
)
Object-Oriented Programming with S3 and R6 in R

 

 

 

private$ accesses private elements

self$ accesses public elements

Object-Oriented Programming with S3 and R6 in R

Summary

  • Encapsulation = separating implementation from UI
  • Store data in private list
  • Store methods in public list
  • Use private$ to access private elements
  • ... and self$ to access public elements
Object-Oriented Programming with S3 and R6 in R

Let's practice!

Object-Oriented Programming with S3 and R6 in R

Preparing Video For Download...