R6 versus S3

R6 is built on encapsulated objects, rather than generic functions.

Big differences: general trade-offs

  • A generic is a regular function so it lives in the global namespace. An R6 method belongs to an object so it lives in a local namespace. This influences how we think about naming.

  • R6’s reference semantics allow methods to simultaneously return a value and modify an object. This solves a painful problem called “threading state”.

  • You invoke an R6 method using $, which is an infix operator. If you set up your methods correctly you can use chains of method calls as an alternative to the pipe.