{"id":76007,"date":"2020-01-30T10:35:44","date_gmt":"2020-01-30T05:05:44","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=76007"},"modified":"2020-01-30T10:35:44","modified_gmt":"2020-01-30T05:05:44","slug":"r-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/","title":{"rendered":"Object Oriented Programming (OOP) in R with S3, S4, and RC"},"content":{"rendered":"<p><strong>This TechVidvan article will give you the much-needed knowledge of object-oriented programming techniques to make your R programs more efficient and readable.<\/strong><\/p>\n<p>In this tutorial, we will go to the basics of object-oriented programming (OOP). We will study what are object-oriented and functional programming techniques and then look at how we can implement them in R programs. We will also take a look at various OOP approaches used in R.<\/p>\n<p>So, let\u2019s get started!<\/p>\n<h3>What is Object-Oriented Programming?<\/h3>\n<p>Object-oriented programming is a <strong>programming technique<\/strong> that manages the complexity of a program more efficiently. It uses techniques like <strong>abstraction<\/strong>, <strong>encapsulation<\/strong>, and <strong>polymorphism<\/strong>. It enacts an environment where the tasks are distributed among the different parts of the program or <strong>\u2018objects\u2019<\/strong>. These objects hide their internal working from other objects and only share concise details regarding what they can do and how to contact them.<\/p>\n<p>For example, think of it as a company. A company may have different departments dealing with different aspects of it. One department does not share its internal workings with another. All a department needs to know is what the other departments do and how to get their help when needed. How a department does whatever it does, is not a concern to the others. This way, the tasks are distributed to appropriate departments and they all focus only on their jobs.<\/p>\n<p>Similarly, in an object-oriented environment, we can think of the programs as <strong>interactions between different objects<\/strong> instead of steps of an algorithm.<\/p>\n<h3>What is Functional Programming?<\/h3>\n<p>Functional programming is a <strong>declarative<\/strong> programming style. It uses expressions that it evaluates to produce their values. Statements assign variables and expressions can be grouped together as functions to make organized routines of expressions. Functional programming is <strong>algorithmic<\/strong>.<\/p>\n<h3>Functional vs Object-Oriented Programming<\/h3>\n<p>Here are the key differences between functional and object-oriented programming:<\/p>\n<ol>\n<li>In functional programming, the primary unit is a function while in OOP, the primary unit is an object.<\/li>\n<li>Functional programming focuses on evaluating functions whereas OOP deals with objects and their interactions among each other.<\/li>\n<li>Data in functional programming is immutable that is when we modify data (which can be a function or a variable) the data is not simply modified. It is copied and the new copy has the changes made while the old copy is still saved in the memory. OOP has mutable data.<\/li>\n<li>Functional programming uses <a href=\"https:\/\/techvidvan.com\/tutorials\/recursion-in-r\/\"><strong>recursion<\/strong><\/a> for iterative processing while OOP uses <strong>loops<\/strong> for it.<\/li>\n<li>Functional programming supports parallel programming. Its functions do not affect code running on other processors. Object-oriented programming does not support parallel programming. Its methods may affect parallelly running parts of the program.<\/li>\n<li>In functional programming, statements can be executed in any order whereas in OOP, execution needs to be in a specific order.<\/li>\n<\/ol>\n<p>Learn how <a href=\"https:\/\/techvidvan.com\/tutorials\/r-control-structures\/\"><strong>loops work in R<\/strong><\/a> programming with examples.<\/p>\n<h3>Core Concepts of Object-Oriented Programming<\/h3>\n<p>Object-oriented programming has a few core techniques that distinguish it from other programming paradigms. Most OOP languages don\u2019t implement all of them but just some of these techniques instead. The most common ones are:<\/p>\n<ol>\n<li><strong>Polymorphism:<\/strong> Polymorphism means different shapes. In polymorphism, a single name can be used to call different objects based on the context it is used in. <strong>Overloading<\/strong> and <strong>overriding<\/strong> are good examples of polymorphism techniques.<\/li>\n<li><strong>Encapsulation:<\/strong> Encapsulation deals with <strong>abstraction<\/strong>. It ensures that the data encapsulated inside an object is not visible to other objects. Only the data and methods declared to be public knowledge are known to others.<\/li>\n<li><strong>Hierarchies:<\/strong> In OOP, hierarchies can be of two types: <strong>Inheritance<\/strong> or <strong>Composition<\/strong>.\n<ul>\n<li>Inheritance is when a class is a \u2018<strong>type of<\/strong>\u2019 or a \u2018<strong>special case of<\/strong>\u2019 another class known as the <strong>superclass<\/strong>. For example, an eagle is a type of bird. Here, eagle is a subclass and bird is its superclass.<\/li>\n<li>In Composition, a class has an <strong>instance<\/strong> of another class. For example, a car has wheels but wheels are not a type of car. There is no inheritance here but wheels are a part of the car so the class car should have access to the class wheels which implies composition.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>Object-Oriented Programming in R<\/h2>\n<p>In R, we solve problems by dividing them into simpler functions and not objects. Hence, functional programming is generally used in R. OOP languages have a single object model in place. These models define the properties and behavior of the objects we create. R has a few different object-oriented programming systems or object models in place. These systems are:<\/p>\n<ol>\n<li><strong>S3:<\/strong> S3 is the first and the simplest OOP system in R. It does not have many restrictions and we can create objects by simply adding a class attribute to it. S3 classes don\u2019t have a formally defined structure. The methods defined in an S3 class belong to generic functions.<\/li>\n<li><strong>S4:<\/strong> S4 classes have a definitive structure to them. This means that different S4 classes have a similar structure. We use the <strong><code>setClass()<\/code><\/strong> function to create a new class and the <strong><code>new()<\/code><\/strong> function to create an object. Like S3 classes, methods in S4 classes belong to generic functions rather than the classes themselves.<\/li>\n<li><strong>Reference classes(RC):<\/strong> Reference classes in R are similar to object-oriented classes in other programming languages. They have all the features of S4 classes with an added environment. To create a reference class, we use the <strong><code>setRefClass()<\/code><\/strong> function. The methods in a reference class belong to the class itself.<\/li>\n<\/ol>\n<p>While these three systems are in the base R packages, there are other packages on CRAN repository that provide more systems like the <a href=\"https:\/\/cran.r-project.org\/web\/packages\/R6\/index.html\">R6<\/a>.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/object-oriented-programming-in-R.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76028 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/object-oriented-programming-in-R.jpg\" alt=\"object oriented programming in R\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<h3>Generic Functions<\/h3>\n<p>Generic functions are a topic of much confusion in R. These functions are good examples of polymorphism. The most common example of a generic function is the <strong><code>print()<\/code><\/strong> function.<\/p>\n<p>The function prints the arguments provided. The input argument can be a string, a numeric or even an object. It does not need a description of the object, its type or structure. How does it do that? The <strong><code>print()<\/code><\/strong> function is a collection of various print functions dedicated to different data types and data structures in R. When we use it, the function finds the type and class of the input object and calls the appropriate function to print it. We can see the different functions under the generic <strong><code>print()<\/code><\/strong> function by using the <strong><code>method()<\/code><\/strong> command. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">methods(print)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] print.acf*<br \/>\n[2] print.anova*<br \/>\n[3] print.aov*<br \/>\n[4] print.aovlist*<br \/>\n.<br \/>\n.<br \/>\n.<br \/>\n[189] print.xgettext*<br \/>\n[190] print.xngettext*<br \/>\n[191] print.xtabs*<br \/>\nsee &#8216;?methods&#8217; for accessing help and source code<\/div>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-methodprint-11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76009 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-methodprint-11.png\" alt=\"method(print) (1) - OOP in R\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>S3 Classes<\/h3>\n<p>S3 classes are the most basic object-oriented classes in R. They implement the polymorphism principle of OOP but not much else. To create an S3 class, we need to add a class attribute to an object.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">emp &lt;- list(name=\"ramesh\",age=\"24\",\ndepartment=\"sales\",emp_id=\"00495\")\nclass(emp) &lt;- \"employee\"\nemp<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-creating-S3-class-12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76010 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-creating-S3-class-12.png\" alt=\"OOP in r - creating S3 class (1)\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can create methods for generic functions as well.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print.employee &lt;- function(obj){\n  cat(\"name: \" ,obj$name, \"\\n\")\n  cat(\"age: \", obj$age, \"\\n\")\n  cat(\"department: \", obj$department, \"\\n\")\n  cat(\"Id: \", obj$emp_id, \"\\n\")\n}\nprint(emp)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-S3-generic-methods-print.employee-13.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76011 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-S3-generic-methods-print.employee-13.png\" alt=\"S3 generic methods print.employee - object oriented programming in R\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Don&#8217;t know how to write functions in R? <\/strong>Learn with <a href=\"https:\/\/techvidvan.com\/tutorials\/r-functions\/\"><strong>user-defined functions<\/strong><\/a> in R.<\/p>\n<h4>Useful Functions for S3 Objects<\/h4>\n<p><strong>1. The is.object() function:<\/strong> the <strong><code>is.object()<\/code><\/strong> function returns <strong><code>TRUE<\/code><\/strong> if the input argument is an object of any of the object models of R.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">is.object(emp)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-useful-s3-functions-is.object-14.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76012 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-useful-s3-functions-is.object-14.png\" alt=\"useful s3 functions is.object() (1) - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>2. The getS3method() function:<\/strong> the <strong><code>getS3method()<\/code><\/strong> function returns the S3 method used by a generic function for a specific type of object.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">getS3method('print','employee')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-useful-S3-functions-getS3method-15.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76013 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-useful-S3-functions-getS3method-15.png\" alt=\"useful S3 functions getS3method() (1) - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>S4 Classes<\/h3>\n<p>S4 classes take OOP in R one step further than S3 classes. They have a definite structure to them which brings some uniformity to objects in R. We can create an S4 class by using the <strong><code>setClass()<\/code><\/strong> function and make a new object using the new function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">setClass(\"employeeS4\", slots=list(name=\n\"character\",age=\"numeric\",department=\n\"character\",emp_id=\"numeric\"))\nemp2 &lt;- new(\"employeeS4\",name=\"james\",age=27,\ndepartment=\"accounts\",emp_id=00564)\nemp2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-creating-S4-class-and-object-16.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76014 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-creating-S4-class-and-object-16.png\" alt=\"creating S4 class and object (1) - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Confused about the list in R?<\/strong> Learn to <a href=\"https:\/\/techvidvan.com\/tutorials\/r-list\/\"><strong>create R list<\/strong><\/a> again.<\/p>\n<p>We can access and modify the slots of an S4 object using the <strong><code>@<\/code><\/strong> or the <strong><code>slot()<\/code><\/strong> function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">emp2@name<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">emp2@name &lt;- \"jerry\"\nemp2@name<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">slot(emp2,\"age\")<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">slot(emp2,\"age\") &lt;- 28\nslot(emp2,\"age\")<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-accessing-and-modify-slots-of-an-S4-objects7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76015 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-accessing-and-modify-slots-of-an-S4-objects7.png\" alt=\"accessing and modify slots of an S4 objects - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can create a method for a generic function for S4 classes using the <strong><code>setMethod()<\/code><\/strong> function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">setMethod(\"show\",\n\"employeeS4\",\nfunction(object){\n  cat(\"name: \", object@name, \"\\n\")\n  cat(\"age: \",object@age,\"\\n\")\n  cat(\"department: \",object@department,\"\\n\")\n  cat(\"Id: \",object@emp_id,\"\\n\")\n}\n)<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">emp2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-creating-S4-method-for-generic-functions8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76016 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-creating-S4-method-for-generic-functions8.png\" alt=\"creating S4 method for generic functions - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>Useful S4 Methods in R<\/h4>\n<p>Here are a few functions that are useful when dealing with S4 objects:<\/p>\n<p><strong>1.<\/strong> <strong>The isS4() function<\/strong>: the <strong><code>isS4()<\/code><\/strong> function returns <strong><code>TRUE<\/code><\/strong> if the input argument is an S4 object.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">isS4(emp2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-useful-S4-functions-isS49.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76017 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-useful-S4-functions-isS49.png\" alt=\"useful S4 functions isS4() - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>2.<\/strong> <strong>The slotNames() function<\/strong>: the <strong><code>slotNames()<\/code><\/strong> function returns the names of all the slots in the input object.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">slotNames(emp2)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-useful-S4-functions-slotNames-copy10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76021 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-useful-S4-functions-slotNames-copy10.png\" alt=\"useful S4 functions slotNames() copy10 - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>Reference Classes (RC)<\/h3>\n<p>Reference classes are like object-oriented classes in other programming languages. They have all the properties of S4 classes with some added structure and restrictions. We can create a reference class using the <strong><code>setRefClass()<\/code><\/strong> function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">employeeRC &lt;- setRefClass(\"employeeRC\",\nfields=list(name=\"character\",age=\"numeric\",\ndepartment=\"character\",emp_id=\"numeric\"))\nemp3 &lt;- employeeRC(name=\"rahul\",age=25,\ndepartment=\"human resources\",emp_id=00243)\nemp3<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-creating-a-reference-class11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76018 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-creating-a-reference-class11.png\" alt=\"creating a reference class - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can access and modify the field in a reference class using the <strong><code>$<\/code><\/strong> symbol.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">emp3$name<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">emp3$name &lt;- \"suman\"\nemp3$name<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-accessing-and-modifying-fields-in-RC12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76019 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-accessing-and-modifying-fields-in-RC12.png\" alt=\"accessing and modifying fields in RC - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can define methods for reference classes using the <strong><code>methods<\/code><\/strong> argument of the <strong><code>setRefClass()<\/code><\/strong> function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">employeeRC &lt;- setRefClass(\"employeeRC\",\nfields=list(name=\"character\",age=\"numeric\",\ndepartment=\"character\",emp_id=\"numeric\"),\nmethods=list(\nshow = function(){\n  cat(\"name: \", name, \"\\n\")\n  cat(\"age: \", age,\"\\n\")\n  cat(\"department: \", department,\"\\n\")\n  cat(\"Id: \", emp_id,\"\\n\")\n}\n))\nemp4 &lt;- employeeRC(name=\"jimmy\",age=26,department=\"RnD\"\n,emp_id=00342)\nshow(emp4)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-methods-for-RC13.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-76020 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/OOP-in-r-methods-for-RC13.png\" alt=\"methods for RC - object oriented programming in r\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>In this tutorial, we learned about Object-oriented programming in R. We learned what object-oriented programming and functional programming are.<\/p>\n<p>We looked at the various object models in R programming, how they are different from one another, and We saw how we can create classes and objects in these different models. We also learned what generic functions are. Finally, we looked at a few examples of S3, S4 and RC classes and their methods.<\/p>\n<p><strong>Any doubts while executing\u00a0Object-oriented programming in R?<\/strong><\/p>\n<p>Ask <strong>TechVidvan<\/strong> below.<\/p>\n<p>Happy Learning!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This TechVidvan article will give you the much-needed knowledge of object-oriented programming techniques to make your R programs more efficient and readable. In this tutorial, we will go to the basics of object-oriented programming&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":76028,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[1548,1549,1550,1551,1552,1553,1554,1555],"class_list":["post-76007","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-object-oriented-programming-in-r","tag-oop-in-r","tag-r-class","tag-r-is-object-oriented","tag-r-objects","tag-reference-class-r","tag-s3-object-r","tag-s4-object-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Object Oriented Programming (OOP) in R with S3, S4, and RC - TechVidvan<\/title>\n<meta name=\"description\" content=\"There are different approaches to object-oriented programming in R. In this article, you\u2019ll learn all three classes (S3, S4, and reference class(RC)) in R.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Object Oriented Programming (OOP) in R with S3, S4, and RC - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"There are different approaches to object-oriented programming in R. In this article, you\u2019ll learn all three classes (S3, S4, and reference class(RC)) in R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-30T05:05:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/object-oriented-programming-in-R.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Object Oriented Programming (OOP) in R with S3, S4, and RC - TechVidvan","description":"There are different approaches to object-oriented programming in R. In this article, you\u2019ll learn all three classes (S3, S4, and reference class(RC)) in R.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/","og_locale":"en_US","og_type":"article","og_title":"Object Oriented Programming (OOP) in R with S3, S4, and RC - TechVidvan","og_description":"There are different approaches to object-oriented programming in R. In this article, you\u2019ll learn all three classes (S3, S4, and reference class(RC)) in R.","og_url":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-30T05:05:44+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/object-oriented-programming-in-R.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Object Oriented Programming (OOP) in R with S3, S4, and RC","datePublished":"2020-01-30T05:05:44+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/"},"wordCount":1499,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/object-oriented-programming-in-R.jpg","keywords":["Object Oriented Programming in R","OOP in R","R class","R is object oriented","R objects","reference class R","S3 object R","S4 object R"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/","url":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/","name":"Object Oriented Programming (OOP) in R with S3, S4, and RC - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/object-oriented-programming-in-R.jpg","datePublished":"2020-01-30T05:05:44+00:00","description":"There are different approaches to object-oriented programming in R. In this article, you\u2019ll learn all three classes (S3, S4, and reference class(RC)) in R.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/object-oriented-programming-in-R.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/object-oriented-programming-in-R.jpg","width":802,"height":420,"caption":"object oriented programming in R"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/r-object-oriented-programming\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Object Oriented Programming (OOP) in R with S3, S4, and RC"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/76007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=76007"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/76007\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/76028"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=76007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=76007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=76007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}