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

the object factory
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

The Object Factory

slide-2
SLIDE 2

Object-Oriented Programming in R: S3 & R6

pour refill

slide-3
SLIDE 3

Object-Oriented Programming in R: S3 & R6

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

slide-4
SLIDE 4

Object-Oriented Programming in R: S3 & R6

slide-5
SLIDE 5

Object-Oriented Programming in R: S3 & R6

slide-6
SLIDE 6

Object-Oriented Programming in R: S3 & R6

library(R6) thing_factory <- R6Class( "Thing", ) private = list( a_field = "a value", another_field = 123 )

slide-7
SLIDE 7

Object-Oriented Programming in R: S3 & R6

Coming soon…

public active

slide-8
SLIDE 8

Object-Oriented Programming in R: S3 & R6

> a_thing <- thing_factory$new() > another_thing <- thing_factory$new() > yet_another_thing <- thing_factory$new()

slide-9
SLIDE 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
slide-10
SLIDE 10

OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Let’s practice!

slide-11
SLIDE 11

OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Hiding Complexity with Encapsulation

slide-12
SLIDE 12

Object-Oriented Programming in R: S3 & R6

Encapsulation

implementation user interface

| | |

slide-13
SLIDE 13

Object-Oriented Programming in R: S3 & R6

microwave_oven_factory <- R6Class( "MicrowaveOven", private = list( power_rating_watts = 800, ), ) public = list( ) private$door_is_open <- TRUE

  • pen_door = function() {

} door_is_open = FALSE

slide-14
SLIDE 14

Object-Oriented Programming in R: S3 & R6

private$ accesses private elements self$ accesses public elements

slide-15
SLIDE 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
slide-16
SLIDE 16

OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Let’s practice!

slide-17
SLIDE 17

OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Geing and Seing with Active Bindings

slide-18
SLIDE 18

Object-Oriented Programming in R: S3 & R6

Part# S-0896

slide-19
SLIDE 19

Object-Oriented Programming in R: S3 & R6

geing = read the data field seing = write the data field

slide-20
SLIDE 20

Object-Oriented Programming in R: S3 & R6

Active Bindings

defined like functions accessed like data variables

slide-21
SLIDE 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 { private$..another_field <- value } } ) ) assert_is_a_number(value)

slide-22
SLIDE 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.

slide-23
SLIDE 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'.

slide-24
SLIDE 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
slide-25
SLIDE 25

OBJECT-ORIENTED PROGRAMMING IN R: S3 & R6

Let’s practice!