12.7 How can you tell if an object is base or OOP?

12.7.1 Functions

Two functions:

  • base::is.object(), which yields TRUE/FALSE about whether is OOP object
  • sloop::otype(), which says what type of object type: "base", "S3", etc.

An few examples:

# Example 1: a base object
is.object(1:10)
#> [1] FALSE
sloop::otype(1:10)
#> [1] "base"

# Example 2: an OO object
is.object(mtcars)
#> [1] TRUE
sloop::otype(mtcars)
#> [1] "S3"

12.7.2 sloop

  • S Language Object-Oriented Programming

XKCD 927

12.7.3 Class

OO objects have a “class” attribute:

# base object has no class
attr(1:10, "class")
#> NULL

# OO object has one or more classes
attr(mtcars, "class")
#> [1] "data.frame"