Create S4 class and its new method in R?

New to R language and don't know much about S4 classes. I am trying to solve a question

Q. How would you create a new S4 class and its new method? 
Create a laptop class, then create an Apple Macbook Pro object
and define a show function to display its CPU type to demonstrate the process.

I created the class using:

setClass("Laptop", slots=list(name="character", CPU_type ="character"))
cl <- new("Laptop",name="Apple Macbook Pro", CPU_type = "8-core CPU")
# Output
An object of class "Laptop"
Slot "name":
[1] "Apple Macbook Pro"
Slot "CPU_type":
[1] "8-core CPU"

Now, I have 3 concerns.

  1. Is this S4 class correct?
  2. I don't know how to define show function that will display the CPU type to demonstrate the process.
  3. I don't understand what does this and its new method means?

Can some one help me out?


Hi, it looks fine.

For #2, I think you are after


which returns "8-core CPU".

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.