Method Lookup for Primitive Generics

Object-Oriented Programming with S3 and R6 in R

Richie Cotton

Data Evangelist at DataCamp

 

  • Writing code
  • Debugging code
  • Maintaining code

 

  • Running code
Object-Oriented Programming with S3 and R6 in R

R vs. C

  • C code often runs faster
  • R code is usually easier to write
  • ... and easier to debug
Object-Oriented Programming with S3 and R6 in R

ch2_3-method-lookup-for-primitive-generics.012.png

Object-Oriented Programming with S3 and R6 in R

ch2_3-method-lookup-for-primitive-generics.013.png

Object-Oriented Programming with S3 and R6 in R
exp
function (x)  .Primitive("exp")
+
function (e1, e2)  .Primitive("+")
if
.Primitive("if")
sin
function (x)  .Primitive("sin")
-
function (e1, e2)  .Primitive("-")
for
.Primitive("for")
Object-Oriented Programming with S3 and R6 in R
.S3PrimitiveGenerics
"anyNA"          "as.character"   "as.complex"
"as.double"      "as.environment" "as.integer"     
"as.logical"     "as.call"        "as.numeric"
"as.raw"         "c"              "dim"           
"dim<-"          "dimnames"       "dimnames<-"     
"is.array"       "is.finite"      "is.infinite"    
"is.matrix"      "is.na"          "is.nan"
"is.numeric"     "length"         "length<-"      
"levels<-"       "names"          "names<-"
"rep"            "seq.int"        "xtfrm" 
Object-Oriented Programming with S3 and R6 in R
all_of_time <- c("1970-01-01", "2012-12-21")
as.Date(all_of_time)
"1970-01-01" "2012-12-21"
class(all_of_time) <- "date_strings"
as.Date(all_of_time)
Error in as.Date.default(all_of_time) : 
  do not know how to convert 'all_of_time' to class"Date"
length(all_of_time)
2
Object-Oriented Programming with S3 and R6 in R

Summary

  • Some R functions are actually written in C
  • The primitive interface gives best performance
  • .S3PrimitiveGenerics lists primitive S3 generics
  • Primitive generics don't throw an error when no method is found
Object-Oriented Programming with S3 and R6 in R

Let's practice!

Object-Oriented Programming with S3 and R6 in R

Preparing Video For Download...