
Object-Oriented Programming in R: S3, S4, and Reference Classes Object-Oriented Programming in R: S3, S4, and Reference Classes Fish 512: Super-Advanced R University of Washington Cole Monnahan May 21, 2014 Object-Oriented Programming in R: S3, S4, and Reference Classes About this lecture This lecture covers the basics of classes in R, with the caveats that I am not a computer scientist nor an expert in object-oriented programming. The following slides were distilled from: 1 Hadley Wickham's \OO field guide" in his Advanced R Programming book: http://adv-r.had.co.nz/OO-essentials.html. 2 Chapter 9 of The Art of R Programming. 3 A lecture by Nicholas Christian, http://www.pitt.edu/ njc23/Lecture4.pdf. 4 Wikipedia (of course!) Simula 67 language (1960s) introduced formal concepts, then extended by the Smalltalk language By the 1990s had developed into the dominant programming methodology Is one of many programming paradigms, and has pros/cons. OOP is built into R so we use it constantly, even if we're unaware of it Object-Oriented Programming in R: S3, S4, and Reference Classes Background and History Object-oriented programming (OOP) is a programming paradigm for designing modular, reusable software systems. OOP is built into R so we use it constantly, even if we're unaware of it Object-Oriented Programming in R: S3, S4, and Reference Classes Background and History Object-oriented programming (OOP) is a programming paradigm for designing modular, reusable software systems. Simula 67 language (1960s) introduced formal concepts, then extended by the Smalltalk language By the 1990s had developed into the dominant programming methodology Is one of many programming paradigms, and has pros/cons. Object-Oriented Programming in R: S3, S4, and Reference Classes Background and History Object-oriented programming (OOP) is a programming paradigm for designing modular, reusable software systems. Simula 67 language (1960s) introduced formal concepts, then extended by the Smalltalk language By the 1990s had developed into the dominant programming methodology Is one of many programming paradigms, and has pros/cons. OOP is built into R so we use it constantly, even if we're unaware of it Object-Oriented Programming in R: S3, S4, and Reference Classes Goals of this lecture 1 Get comfortable with the basic (often convoluted) terminology used in OOP. 2 Understand how R uses classes currently, and be able to recognize them. 3 Know when and how to write your own S3 class. Rapid Development Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Ease of evolution OOP makes it easy to reuse and extend components as needed. Object-Oriented Programming in R: S3, S4, and Reference Classes Philosophy These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We'll discuss how this philosophy applies to R at the end of the lecture. Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Ease of evolution OOP makes it easy to reuse and extend components as needed. Object-Oriented Programming in R: S3, S4, and Reference Classes Philosophy These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We'll discuss how this philosophy applies to R at the end of the lecture. Rapid Development Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Ease of evolution OOP makes it easy to reuse and extend components as needed. Object-Oriented Programming in R: S3, S4, and Reference Classes Philosophy These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We'll discuss how this philosophy applies to R at the end of the lecture. Rapid Development Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Ease of evolution OOP makes it easy to reuse and extend components as needed. Object-Oriented Programming in R: S3, S4, and Reference Classes Philosophy These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We'll discuss how this philosophy applies to R at the end of the lecture. Rapid Development Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Object-Oriented Programming in R: S3, S4, and Reference Classes Philosophy These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We'll discuss how this philosophy applies to R at the end of the lecture. Rapid Development Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Ease of evolution OOP makes it easy to reuse and extend components as needed. Object-Oriented Programming in R: S3, S4, and Reference Classes Key definitions Class: Defines the behavior of objects by describing their attributes and their relationship to other classes. lm is a class in R Object: An instance of a class, containing data and methods. A fitted model is an object of class lm Method: A function associated with a class. predict.lm is a method acting on lm objects. Object-Oriented Programming in R: S3, S4, and Reference Classes Key definitions Class: Defines the behavior of objects by describing their attributes and their relationship to other classes. lm is a class in R Object: An instance of a class, containing data and methods. A fitted model is an object of class lm Method: A function associated with a class. predict.lm is a method acting on lm objects. Object-Oriented Programming in R: S3, S4, and Reference Classes Key definitions Class: Defines the behavior of objects by describing their attributes and their relationship to other classes. lm is a class in R Object: An instance of a class, containing data and methods. A fitted model is an object of class lm Method: A function associated with a class. predict.lm is a method acting on lm objects. S4: Similar to S3 but with three key differences: 1 Has a formal definition, which provides more control 2 Can use multiple dispatch (not covered here) 3 A new \slot" syntax: @ instead of $. Reference Classes: Newest type (since 2.12) Quite different from S3/S4, more like traditional languages. Uses \message-passing" style. Base type: The internal C-level types, built using C code. Not covered in this lecture. Object-Oriented Programming in R: S3, S4, and Reference Classes Overview of classes in R S3: The original and most common type in R. Uses \generic-function" style. Very casual: it has no formal definition. Reference Classes: Newest type (since 2.12) Quite different from S3/S4, more like traditional languages. Uses \message-passing" style. Base type: The internal C-level types, built using C code. Not covered in this lecture. Object-Oriented Programming in R: S3, S4, and Reference Classes Overview of classes in R S3: The original and most common type in R. Uses \generic-function" style. Very casual: it has no formal definition. S4: Similar to S3 but with three key differences: 1 Has a formal definition, which provides more control 2 Can use multiple dispatch (not covered here) 3 A new \slot" syntax: @ instead of $. Base type: The internal C-level types, built using C code. Not covered in this lecture. Object-Oriented Programming in R: S3, S4, and Reference Classes Overview of classes in R S3: The original and most common type in R. Uses \generic-function" style. Very casual: it has no formal definition. S4: Similar to S3 but with three key differences: 1 Has a formal definition, which provides more control 2 Can use multiple dispatch (not covered here) 3 A new \slot" syntax: @ instead of $. Reference Classes: Newest type (since 2.12) Quite different from S3/S4, more like traditional languages. Uses \message-passing" style. Object-Oriented Programming in R: S3, S4, and Reference Classes Overview of classes in R S3: The original and most common type in R. Uses \generic-function" style. Very casual: it has no formal definition. S4: Similar to S3 but with three key differences: 1 Has a formal definition, which provides more control 2 Can use multiple dispatch (not covered here) 3 A new \slot" syntax: @ instead of $. Reference Classes: Newest type (since 2.12) Quite different from S3/S4, more like traditional languages.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages55 Page
-
File Size-