S3 Objects


Scott Chamberlain ( @sckottie)

rOpenSci

S3 objects


R's first and simplest OO (Object Oriented) system



In Adv. R book

S3 is informal and ad hoc, but it has a certain elegance in its minimalism: you can’t take away any part of it and still have a useful OO system

S3 - when to use S3 generics/methods


  • inputs can have many different classes

  • you want to do different things with different classes

  • you want something that's light weight

  • you want to build on top of a base R generic (e.g, plot)

S3 - beware!


  • consider S3 failure behavior - see .default

  • parameters should be consistent across S3 methods

  • pay attention to whether methods on generics are exported in your pkg

  • as a user: don't call S3 methods directly, call the generic

  • no validation of checking of contents

  • extended generic methods of base functions from diff. pkgs can conflict!

  • S3 classes can be easily dropped/lost (next slide)

further reading

slides: scotttalks.info/rs3

Made w/: reveal.js v3.7.0, FontAwesome v5.7.2