📖 Scenario: You are working with a library system that needs to keep track of books. Each book has a title and a number of pages.
🎯 Goal: You will create an S4 class called Book with slots for title and pages. Then you will create an object of this class and display its details.
📋 What You'll Learn
Create an S4 class named Book with slots title (character) and pages (numeric).
Create a variable called my_book that is an object of class Book with title 'The Great Gatsby' and pages 180.
Write a method to show the book details using the show function.
Print the my_book object to display its details.
💡 Why This Matters
🌍 Real World
Libraries and bookstores use object systems to manage book information efficiently and consistently.
💼 Career
Understanding S4 classes is important for R programmers working in data science, bioinformatics, or any field requiring structured data management.
Progress0 / 4 steps
1
Define the S4 class Book
Create an S4 class called Book with slots title of type character and pages of type numeric using setClass.
R Programming
Hint
Use setClass with slots = list(title = "character", pages = "numeric").
2
Create an object my_book of class Book
Create a variable called my_book that is a new object of class Book with title set to 'The Great Gatsby' and pages set to 180 using new.
R Programming
Hint
Use new("Book", title = "The Great Gatsby", pages = 180) to create the object.
3
Define a show method for Book
Define a show method for objects of class Book that prints the book's title and number of pages in a friendly message using setMethod.
R Programming
Hint
Use setMethod("show", "Book", function(object) { ... }) and inside use cat to print the details.
4
Print the my_book object
Write a line of code to print the my_book object so that the show method runs and displays the book details.
R Programming
Hint
Use print(my_book) to display the book details using the show method.
Practice
(1/5)
1. What is the main purpose of the S4 object system in R?
easy
A. To visualize data with plots
B. To perform fast numerical calculations
C. To create formal classes with defined slots and types
D. To manage file input and output
Solution
Step 1: Understand S4 system basics
The S4 system in R is designed to create formal classes with named slots and specific data types.
Step 2: Compare with other options
Options A, B, and D describe other R functionalities unrelated to S4 classes.
Final Answer:
To create formal classes with defined slots and types -> Option C
Quick Check:
S4 system = formal classes with slots [OK]
Hint: S4 means formal classes with slots, not calculations or plots [OK]
Common Mistakes:
Confusing S4 with data visualization
Thinking S4 is for fast math
Mixing S4 with file handling
2. Which of the following is the correct way to define an S4 class named Person with slots name (character) and age (numeric)?
easy
A. setClass("Person", slots = list(name = "character", age = "numeric"))
B. class(Person) <- list(name = character, age = numeric)
C. Person <- function(name, age) list(name = name, age = age)
D. newClass("Person", fields = c(name = "character", age = "numeric"))
Solution
Step 1: Recall S4 class definition syntax
In R, setClass is used with the class name and a named list of slots with their types.
Step 2: Check each option
setClass("Person", slots = list(name = "character", age = "numeric")) uses setClass correctly. Options A, B, and C use incorrect syntax or functions not related to S4.
Final Answer:
setClass("Person", slots = list(name = "character", age = "numeric")) -> Option A
Quick Check:
Use setClass with slots list [OK]
Hint: Use setClass() with slots list to define S4 classes [OK]
Common Mistakes:
Using base R list or function instead of setClass
Wrong function names like newClass
Assigning class with <- instead of setClass
3. What will be the output of this R code?
setClass("Car", slots = list(make = "character", year = "numeric"))
myCar <- new("Car", make = "Toyota", year = 2020)
myCar@make
medium
A. "Toyota"
B. 2020
C. Error: object 'myCar' not found
D. "make"
Solution
Step 1: Understand class and object creation
The class "Car" has slots "make" (character) and "year" (numeric). The object myCar is created with make = "Toyota" and year = 2020.
Step 2: Access the slot value
Using myCar@make accesses the "make" slot, which holds the string "Toyota".
Final Answer:
"Toyota" -> Option A
Quick Check:
Slot access with @ returns "Toyota" [OK]
Hint: Use @ to access slot values in S4 objects [OK]
Common Mistakes:
Using $ instead of @ to access slots
Confusing slot names with values
Expecting numeric output instead of character
4. Identify the error in this S4 method definition and select the correct fix:
B. Use object@name instead of object$name in the method
C. Remove the function argument 'object' from setGeneric
D. Use $ instead of @ to access slots
Solution
Step 1: Check slot access in S4 objects
S4 objects require slot access with @, not $. The code uses object$name which is incorrect.
Step 2: Correct the slot access
Replace object$name with object@name to properly access the slot in the method.
Final Answer:
Use object@name instead of object$name in the method -> Option B
Quick Check:
S4 slots accessed with @, not $ [OK]
Hint: Always use @ to access S4 slots inside methods [OK]
Common Mistakes:
Using $ to access slots in S4 objects
Confusing setGeneric and setMethod roles
Omitting function argument in setGeneric
5. You want to create an S4 class Rectangle with slots length and width (both numeric). Then define a method area that returns the product of length and width. Which code correctly implements this?