Module 7: Object S3 vs S4

 Module 7: Object S3 vs S4


    This week we learnt about S3 and S4 systems, and I got to play around with a lot of code this week attempting to learn about it and satisfy the homework requirements, which I believe I have done to quite a sufficient level. 
First, I created my own dataset, then, explored it using a variety of the generic functions to show it worked as intended. Then, I began with my first S3 addition to my created data frame, followed by my first S4 addition. I then added one more example of each to satisfy the fifth questions need for 2 examples of each and all of it will be available on my github linked here!
Now I will answer the questions as they were asked:

1. How do you tell what OO system (S3 vs. S4) an object is associated with?
   The easiest way to tackle this question is to explain what both are. An S3 object is informal, a list, data frame, vector with a class attribute attached. S4 objects however are formal, have slots and class definitions. In other words, if you check and ask if the system is S4, it will either tell you it is, or you know it falls under the S3 class. 

2. How do you determine the base type (like integer or list) of an object?
    To determine the base type of an object you simply put the object you wish to inspect into the function typeof(), or if you wish to know what the layer above the base is, you can use class () to identify if it is S3 or S4.

3. What is a generic function?
    A generic function is just a function that checks the object entered through it, checks its class and then chooses what method to run. In other words, its like plot(), or str(), they are the fundamental functions we use when analyzing or manipulating data in R.

4.What are the main differences between S3 and S4?
    The main differences between them boils down to their structure. One (S3) is informal, flexible in a sense, whereas the S4 objects are much stricter, requiring the use of slots and demanding classes have formal class definitions. From validation to object structure to the way they dispatch their methods, the classes of objects are extremely different and as a result have real practical differences you can see when coding. 

For question 5 I will just display my code and some output to highlight maybe some of the differences between the systems.

The above is simply the creation of and exploration of the dataframe entitled studentData.

For the purposes of not creating an overly long and technical blog I will only show one example of each here, as the question 5 asks for 2 examples to be put on github, where they are available for view now. However, the first example of S3 addition to my above is shown here:


Above you can see me add a new class called studentS3 to my data frame, keeping dataframe still as the second class to make sure it still behaves normally. Then I defined the custom print method for the dataset, before finally calling the function all together and printing the header, the number of students and then the raw data.

For the S4 example I have, it can be seen below:




The above shows me create a new S4 class, "StudentData", leaving the one slot, named df and stating that it can hold any R object, when I had it as data frame, it broke the code. I then created a new instance of the Student Data class and filled the existing df slot with the preexisting studentData (capitalization is important! It is referencing my previously used dataframe). It then stores all that data in studentObject and then we create the S4 show method. This is like print, in the sense that the goal is to control how the object displays. We again print a header, show the number of students, and then print the dataframe at the end of the function. You can also note that the S3 Object is still displayed as it was an addition to the original dataframe.

Overall this has been a very fascinating week, I enjoyed the lectures, and the readings as this was a rather difficult week in terms of time taken to code. However, again it was enjoyable and I continue to look forward to the lessons I have yet to take!






Comments

Popular posts from this blog

Week 4: Programming Structure in R