Upgrade to Pro — share decks privately, control downloads, hide ads and more …

R7: A first look

R7: A first look

Hadley Wickham

June 21, 2022
Tweet

More Decks by Hadley Wickham

Other Decks in Education

Transcript

  1. Hadley Wickham on behalf of

    The R Consortium Working Group on OOP

    https://rconsortium.github.io/OOP-WG/
    R7:


    A
    fi
    rst look at a new OOP system for R
    June 2022

    View Slide

  2. bizarro
    < -
    function(x) {


    if (is.numeric(x)) {


    -
    x


    } else if (is.logical(x)) {


    !x


    } else if (is.character(x)) {


    str_reverse(x)


    } else if (is.factor(x)) {


    levels(x)
    < -
    rev(level(x))


    x


    } else {


    stop("Not supported")


    }


    }
    Why do we need OOP?
    This is even more
    important for functions
    in base R, like print()

    View Slide

  3. S3
    S4
    English Fairy Tales (1918) by Flora Annie Steel, illustrated by Arthur Rackham
    R7 R7 = S3 + S4

    View Slide

  4. library(R7)


    bizarro
    < -
    new_generic("bizarro", "x")


    With R7, we
    fi
    rst de
    fi
    ne a generic
    Name of the generic
    Name of the argument used for dispatch

    View Slide

  5. method(bizarro, class_numeric)
    < -
    function(x) {


    -
    x


    }


    method(bizarro, class_logical)
    < -
    function(x) {


    !x


    }


    method(bizarro, class_character)
    < -
    function(x) {


    str_reverse(x)


    }
    Then we de
    fi
    ne methods
    Name of the generic Built-in class

    View Slide

  6. bizarro


    #> function (x,
    . . .
    ) with 4 methods:


    #> 1
    :
    method(bizarro, "integer")


    #> 2
    :
    method(bizarro, "double")


    #> 3
    :
    method(bizarro, "character")


    #> 4
    :
    method(bizarro, "logical")


    View Slide

  7. bizarro(1)


    #> [1] -1


    bizarro(TRUE)


    #> [1] FALSE


    bizarro(mean)


    #> Error: generic `bizarro()` can't f
    i
    nd method


    #> for function object

    View Slide

  8. Generics de
    fi
    ne behaviour
    Classes de
    fi
    ne data

    View Slide

  9. range
    < -
    new_class("range",


    properties = list(


    start = class_numeric,


    end = class_numeric


    )


    )


    Creating a new class
    Name of the class
    Property type
    Property name
    Constructor function

    View Slide

  10. x
    < -
    range(start = 1, end = 10)


    x


    #>


    #> @ start: num 1


    #> @ end : num 10


    [email protected]


    [email protected]
    < -
    20


    [email protected]
    < -
    "x"


    #> Error: @end must be , not

    View Slide

  11. r
    < -
    range(start = 10, end = 1)

    #>


    #> @ start: num 10


    #> @ end : num 1
    But…

    View Slide

  12. range
    < -
    new_class("range",


    properties =
    . . .
    ,


    validator = function(self) {


    if (length([email protected])
    ! =
    1) {


    "@start must be length 1"


    } else if (length([email protected])
    ! =
    1) {


    "@end must be length 1"


    } else if ([email protected] < [email protected]) {


    "@end must be greater than or equal to @start"


    }


    }


    )
    Can add a validator to prevent that from happening

    View Slide

  13. range
    < -
    new_class("range",


    properties = list(


    start = class_double,


    end = class_double,


    length = new_property(


    getter = function(self) [email protected] - [email protected],


    )


    ),





    )
    Can also create dynamic properties

    View Slide

  14. S3
    S4
    English Fairy Tales (1918) by Flora Annie Steel, illustrated by Arthur Rackham
    R7

    View Slide

  15. View Slide

  16. 1. Team e
    ff
    ort.


    2. Formal de
    fi
    nition of class and
    properties (like S4).


    3. Embrace data access.


    4. Backward compatible with S3.


    5. Pit of success.
    Why do we hope this will succeed?

    View Slide

  17. Please try it out!
    https://rconsortium.github.io/OOP-WG/
    remotes
    : :
    install_github("rconsortium/OOP-WG")
    What doesn’t work?

    What don’t you understand?
    These slides at https://speakerdeck.com/hadley/r7-a-
    fi
    rst-look

    View Slide

  18. View Slide