The Object Factory

Object-Oriented Programming with S3 and R6 in R

Richie Cotton

Data Evangelist at DataCamp

 

ch3_1-the-object-factory.003.png

Object-Oriented Programming with S3 and R6 in R

 

 

class generators are templates for objects

Object-Oriented Programming with S3 and R6 in R

 

 

class generators are templates for objects a.k.a. factories

Object-Oriented Programming with S3 and R6 in R

ch3_1-the-object-factory.007.png

Object-Oriented Programming with S3 and R6 in R

 

ch3_1-the-object-factory.009.png

Object-Oriented Programming with S3 and R6 in R

 

ch3_1-the-object-factory.010.png

Object-Oriented Programming with S3 and R6 in R

 

library(R6)
thing_factory <- R6Class(
  "Thing",

private = list( a_field = "a value", another_field = 123 )
)
Object-Oriented Programming with S3 and R6 in R

Coming soon ...

 

 

     public

     active

Object-Oriented Programming with S3 and R6 in R

 

a_thing <- thing_factory$new()
another_thing <- thing_factory$new() 
yet_another_thing <- thing_factory$new()
Object-Oriented Programming with S3 and R6 in R

Summary

  • Load the R6 package to work with R6!
  • Define class generators with R6Class()
  • Class names should be UpperCamelCase
  • Data fields stored in private list
  • Create objects with factory's new() method
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...