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

Intro to object-oriented programming

Intro to object-oriented programming

I gave this talk at our graduate student computing club. It was an informal introduction to object-oriented programming in R, focusing on S4, with a brief comparison to Python classes. Examples on github: https://github.com/alyssafrazee/cards

Alyssa Frazee

December 04, 2014
Tweet

More Decks by Alyssa Frazee

Other Decks in Programming

Transcript

  1. Object-oriented
    programming
    Alyssa Frazee
    computing club, Dec 4 2014

    View Slide

  2. Definition
    ● “object” = user-defined data structure
    ● “attribute” = piece of data associated with the
    object
    ● “method” = procedure/function associated
    with the object
    I got this from Wikipedia (and remembering
    stuff from when I took CS101 in 2008)

    View Slide

  3. What’s the point?
    It’s convenient to be able to create your own
    way of storing data.
    Famous R examples:
    lm
    SummarizedExperiment
    fmridata
    Note: some people are anti-OOP: http://c2.
    com/cgi/wiki?ArgumentsAgainstOop

    View Slide

  4. R has 3 or 4 OO systems
    ● S3
    ● S4
    ● Reference classes
    ● Base types (“not quite OO”)
    http://adv-r.had.co.nz/OO-essentials.html

    View Slide

  5. R has 3 or 4 OO systems
    ● S3
    ● S4
    ● Reference classes
    ● Base types (“not quite OO”)
    http://adv-r.had.co.nz/OO-essentials.html

    View Slide

  6. S3

    View Slide

  7. S4 example
    #' Card
    #'
    #' A playing card
    #'
    #' @slot suit Is this card a spade, club, diamond, or heart?
    #' @slot number The card’s value. Can be 2-10, ‘J’, ‘Q’, ‘K’, or ‘A’
    #’ @slot trump Is the card a trump card? Boolean.
    #'
    #' @exportClass Card
    setClass("Card",
    representation(
    suit = "character", number = "numeric", trump = "logical")
    )
    AllClass.R

    View Slide

  8. Options for defining methods:
    ● Just write a function
    ○ probably the easiest choice if the function name is
    unique.

    View Slide

  9. S4 example
    #' find_pair
    #'
    #' Find the card with the same value and color as the given card
    #'
    #' @param card Object of class “Card”
    #' @export
    find_pair = function(card){
    new_suit = switch([email protected],
    diamonds = ‘hearts’, hearts = ‘diamonds’,
    spades = ‘clubs’, clubs = ‘spades’)
    return(new(‘Card’, suit = new_suit, number = [email protected],
    trump=FALSE))
    }
    find_pair.R

    View Slide

  10. Options for defining methods:
    ● Just write a function
    ○ probably the easiest choice if the function name is
    unique.
    ● Extend or create a generic function, then
    create a way for that function to work with
    your class of object.
    ○ required if you need to make a base function like
    “plot” work with your object
    ○ optional if function name is unique -- consider
    whether the function might be useful for existing or
    future classes of objects

    View Slide

  11. S4 example
    #' @name cardplot
    #' @export
    #' @docType methods
    #' @rdname cardplot
    #' @importFrom graphics plot
    setGeneric("plot", function(x,y,...) standardGeneric("plot"))
    Card-plot-method.R

    View Slide

  12. S4 example
    #' make an awesome plot of a card!
    #' @name cardplot
    #' @exportMethod plot
    #' @docType methods
    #' @rdname cardplot
    #' @aliases cardplot,ballgown-method
    #' @param x a Card object
    setMethod("plot", "Card", function(x){
    plot(0, 0, type='n', xaxt='n', yaxt='n', xlab='', ylab='', main='My Card!')
    if([email protected] > 10){
    s = ifelse([email protected]==11, 'Jack',
    ifelse([email protected]==12, 'Queen',
    ifelse([email protected]==13, 'King', 'Ace')))
    }else{
    s = [email protected]
    }
    text(0, 0, paste(s, 'of', [email protected]), cex=2)
    })
    Card-plot-method.R
    (continued)

    View Slide

  13. *Key Insight* about R methods
    *Functions* have different types of *objects*
    associated with them. (**not** the other way
    around!)
    (“generic function OO” vs. “message-passing
    OO”)

    View Slide

  14. An example in Python
    class Card(object):
    def __init__(self, suit, number, trump=False):
    self.suit = suit
    self.number = number
    self.trump = trump
    def repr(self): ## like R’s show
    return str(self.number)+‘ of ’+self.suit
    def plot(self):
    ## plotting code, whatever whatever

    View Slide

  15. message-passing OO
    mycard.plot()

    View Slide

  16. An example in Python
    class Card(object):
    def __init__(self, suit, number, trump=False):
    self.suit = suit
    self.number = number
    self.trump = trump
    def __cmp__(self, other): ## THIS is very cool
    if self.number > other.number:
    return 1
    elif other.number > self.number:
    return -1
    else:
    return 0

    View Slide

  17. Python
    classes

    View Slide

  18. Syntax Note
    Many languages (includng Python) use the “.”
    operator to access attributes and methods of
    different objects. (e.g. mycard.suit)
    (The fact that R uses “.” like any other
    character makes it weird).

    View Slide

  19. Much more fun to be had!
    ● inheritance!
    ○ but things get complicated VERY quickly
    ● S3 and S4 classes working together;
    documentation, etc
    ● questions? discussion?
    ● R example on github: https://github.
    com/alyssafrazee/cards

    View Slide