R package unit tests and intro to OOP – Part 02
Introduction to unit testing and object-oriented programming with R packages
Pre-lecture activities
Tip
For unit testing, please install
testthat- this provides utilities for unit testing package for R
For reading more about object-oriented programming, please have a brief look through these four sections of the book:
- https://adv-r.hadley.nz/oo (Chapters 12-16)
Lecture
Acknowledgements
Material for this lecture was borrowed and adopted from
- https://testthat.r-lib.org
- https://adv-r.hadley.nz/oo (Chapters 12-16)
Learning objectives
NoteLearning objectives
At the end of this lesson you will:
- Create unit tests for the function in your R package using
testthat - Recognize the primary object-oriented systems in R: S3, S4, and Reference Classes (RC).
- Understand the terminology of a class, object, method, and generic.
- Be able to create a new S3 with generics and methods
Slides
Class activity
For the rest of the time in class, you and your team will work on the final project. Stephanie will walk around to answer questions and happy to help in any way!
Post-lecture
Summary
- R has three object oriented systems: S3, S4, and Reference Classes.
- Reference Classes are the most similar to classes and objects in other programming languages.
- Classes are blueprints for an object.
- Objects are individual instances of a class.
- Methods are functions that are associated with a particular class.
- Constructors are methods that create objects.
- Everything in R is an object.
- S3 is a liberal object oriented system that allows you to assign a class to any object.
- S4 is a more strict object oriented system that build upon ideas in S3.
Additional practice
Here are some additional practice questions to help you think about the material discussed.