+ 10

R language Classes and OOP

R language has multiple systems available for doing object oriented programming, namely: S3, S4, Reference Classes (R5), R6. I am well aware that using a functional language to do OOP is very much an oxymoron, but sometimes it is good to be able to define your own data structures. All these OOP systems have very large differences, and so far it seems I like most S3 and R6. My question is: In what situations should I be using either one of these systems? Especially, when should I choose S4 over S3 or R6 over R5 or vice versa?

13th Apr 2021, 1:53 PM
ChillPill 🌶
ChillPill 🌶 - avatar
2 Answers
+ 8
R6 is currently not a built-in system (correct me if I am wrong) which might be consideration... R6 is what comes closest to usual OOP in other languages, but, as you pointed out yourself, R isn't designed for OOP, so: if you do R6, are you really really really sure no other system would do? And are you really really really sure you want to do the task with R? For users who predominantly work with R and are not experienced with other programming languages might find R6 a little strange in the beginning. S3 and S4 are more common than reference classes. S3 is very list-like in structure. I think this is comfortable if the user wants to process data in another format/ structure. S4 has more formal restrictions than S3 which means less flexibility but more "safety"/ " clarity" (?). To be fair: I myself never really tried for classes (or OOP in general) in R.
14th Apr 2021, 6:58 AM
Lisa - avatar
+ 4
thanks Lisa
14th Apr 2021, 7:51 AM
ChillPill 🌶
ChillPill 🌶 - avatar