S4 is a formal and rigorous object-oriented system in R that allows defining classes with explicit representation and methods with formal signatures.
Click to reveal answer
beginner
How do you define a new S4 class in R?
Use the <code>setClass()</code> function with the class name and slots (attributes) specified as a named list of classes.
Click to reveal answer
beginner
What is a slot in the S4 system?
A slot is a named component (attribute) of an S4 object that holds data of a specified class or type.
Click to reveal answer
intermediate
How do you define a method for an S4 generic function?
Use <code>setMethod()</code> specifying the generic function name, the class signature for which the method applies, and the function implementation.
Click to reveal answer
intermediate
What is the difference between S3 and S4 object systems in R?
S3 is informal and uses naming conventions for methods, while S4 is formal with explicit class definitions, slot types, and method signatures.
Click to reveal answer
Which function is used to create a new S4 class in R?
AsetClass()
BnewClass()
CdefineClass()
DcreateClass()
✗ Incorrect
The setClass() function is used to define new S4 classes in R.
In S4, what is a 'slot'?
AA package dependency
BA method for an S4 class
CA generic function
DA named attribute of an S4 object
✗ Incorrect
Slots are named components (attributes) of S4 objects that hold data.
Which function defines a method for an S4 generic function?
AsetMethod()
BdefineMethod()
CnewMethod()
DcreateMethod()
✗ Incorrect
The setMethod() function is used to define methods for S4 generics.
What is a key feature of S4 compared to S3?
AUses only lists
BNo method dispatch
CFormal class and method definitions
DNo inheritance
✗ Incorrect
S4 uses formal class definitions with explicit slots and method signatures.
How do you create an instance of an S4 class?
AUsing the create() function
BUsing the new() function
CUsing the instantiate() function
DUsing the build() function
✗ Incorrect
The new() function creates instances of S4 classes.
Explain how to define an S4 class and create an object of that class in R.
Think about the functions used to define the class structure and to make an object.
You got /5 concepts.
Describe the difference between S3 and S4 object systems in R.
Focus on how strict and explicit each system is.
You got /5 concepts.
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?