1. Learn
  2. /
  3. Courses
  4. /
  5. Object-Oriented Programming with S3 and R6 in R

Exercise

Primitive Generic Functions

Some core functionality of R is defined using primitive functions, which use a special technique for accessing C-code, for performance reasons. Examples of primitive functions include language elements, like if and for, operators like + and $, and mathematical functions like exp and sin. Primitive functions include S3 generics; the complete list of S3 primitive generics can be found using .S3PrimitiveGenerics.

When an S3 generic is primitive, its lookup process to find methods works slightly differently. R will look for methods using the class, as normal, but if nothing is found, the internal C-code function will be called. (Compare this to regular generics, where an error is thrown if no method is found.) This means that if you override the class of an object, fundamental behavior like calculating the length will not be broken.

Instructions

100 XP

A list object, with the class overridden to be "hairstylist" has been assigned in your workspace to the variable hair.

  • View the structure of hair with str().
  • List all the S3 primitive generics that R knows about.
  • Use the exists() function to check that no function named length.hairstylist exists. Note that this function takes a character input.
  • Calculate the length() of the hair variable.