the object factory
play

The Object Factory Object-Oriented Programming in R: S3 & R6 - PowerPoint PPT Presentation

OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6 The Object Factory Object-Oriented Programming in R: S3 & R6 pour refill Object-Oriented Programming in R: S3 & R6 class generators are templates for objects a.k.a. factories


  1. OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6 The Object Factory

  2. Object-Oriented Programming in R: S3 & R6 pour refill

  3. Object-Oriented Programming in R: S3 & R6 class generators are templates for objects a.k.a. factories

  4. Object-Oriented Programming in R: S3 & R6

  5. Object-Oriented Programming in R: S3 & R6

  6. Object-Oriented Programming in R: S3 & R6 library(R6) thing_factory <- R6Class( "Thing", private = list( a_field = "a value", another_field = 123 ) )

  7. Object-Oriented Programming in R: S3 & R6 Coming soon… public active

  8. Object-Oriented Programming in R: S3 & R6 > a_thing <- thing_factory$new() > another_thing <- thing_factory$new() > yet_another_thing <- thing_factory$new()

  9. Object-Oriented Programming in R: S3 & R6 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

  10. OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6 Let’s practice!

  11. OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6 Hiding Complexity with Encapsulation

  12. Object-Oriented Programming in R: S3 & R6 Encapsulation | implementation user interface | |

  13. Object-Oriented Programming in R: S3 & R6 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 } ) )

  14. Object-Oriented Programming in R: S3 & R6 private$ accesses private elements self$ accesses public elements

  15. Object-Oriented Programming in R: S3 & R6 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

  16. OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6 Let’s practice!

  17. OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6 Ge � ing and Se � ing with Active Bindings

  18. Object-Oriented Programming in R: S3 & R6 Part# S-0896

  19. Object-Oriented Programming in R: S3 & R6 ge � ing = read the data field se � ing = write the data field

  20. Object-Oriented Programming in R: S3 & R6 Active Bindings defined like functions accessed like data variables

  21. Object-Oriented Programming in R: S3 & R6 thing_factory <- R6Class( "Thing", private = list( ..a_field = "a value", ..another_field = 123 ), active = list( a_field = function() { if(is.na(private$..a_field)) { return("a missing value") } private$..a_field }, another_field = function(value) { if(missing(value)) { private$..another_field } else { assert_is_a_number(value) private$..another_field <- value } } ) )

  22. Object-Oriented Programming in R: S3 & R6 > a_thing <- thing_factory$new() > a_thing$a_field [1] "a value" > a_thing$a_field <- "a new value" Error in (function (value) : a_field is read-only.

  23. Object-Oriented Programming in R: S3 & R6 > a_thing$another_field <- 456 > a_thing$another_field <- "456" Error in (function (value) : is_a_number : value is not of class 'numeric'; it has class 'character'.

  24. Object-Oriented Programming in R: S3 & R6 Summary ● Control private access with active bindings ● Defined like functions ● Accessed like data ● Use assertive to check binding inputs

  25. OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6 Let’s practice!

Recommend


More recommend