John Chambers, creator of S programming language
#> R version 4.5.1 (2025-06-13 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 26100)
#>
#> Matrix products: default
#> LAPACK version 3.12.1
#>
#> locale:
#> [1] LC_COLLATE=English_United States.utf8
#> [2] LC_CTYPE=English_United States.utf8
#> [3] LC_MONETARY=English_United States.utf8
#> [4] LC_NUMERIC=C
#> [5] LC_TIME=English_United States.utf8
#>
#> time zone: America/Chicago
#> tzcode source: internal
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] DiagrammeR_1.0.11
#>
#> loaded via a namespace (and not attached):
#> [1] digest_0.6.37 RColorBrewer_1.1-3 R6_2.6.1 fastmap_1.2.0
#> [5] xfun_0.52 magrittr_2.0.3 glue_1.8.0 knitr_1.50
#> [9] htmltools_0.5.8.1 rmarkdown_2.29 cli_3.6.5 visNetwork_2.1.2
#> [13] compiler_4.5.1 tools_4.5.1 evaluate_1.0.4 yaml_2.3.10
#> [17] rlang_1.1.6 jsonlite_2.0.0 htmlwidgets_1.6.4 keyring_1.4.1
# imagine a function with object x as an argument
# from the outside, users interact with the same function
# but inside the function, there are provisions to deal with objects of different classes
some_function <- function(x) {
if is.numeric(x) {
# implementation for numeric x
} else if is.character(x) {
# implementation for character x
} ...
}
#> mpg cyl disp hp
#> Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
#> 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
#> Median :19.20 Median :6.000 Median :196.3 Median :123.0
#> Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
#> 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
#> Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
#>
#> Call:
#> lm(formula = mpg ~ hp, data = mtcars)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -5.7121 -2.1122 -0.8854 1.5819 8.2360
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 30.09886 1.63392 18.421 < 2e-16 ***
#> hp -0.06823 0.01012 -6.742 1.79e-07 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 3.863 on 30 degrees of freedom
#> Multiple R-squared: 0.6024, Adjusted R-squared: 0.5892
#> F-statistic: 45.46 on 1 and 30 DF, p-value: 1.788e-07
1. Encapsulated OOP
object.method(arg1, arg2)
object
, apply method
for object
’s class with arguments arg1
and arg2
2. Functional OOP
generic(object, arg2, arg3)
Bioconductor
ggplot2
Two functions:
base::is.object()
, which yields TRUE/FALSE about whether is OOP objectsloop::otype()
, which says what type of object type: "base"
, "S3"
, etc.An few examples:
#> [1] FALSE
#> [1] "base"
#> [1] TRUE
#> [1] "S3"
OO objects have a “class” attribute:
Only OO objects have a “class” attribute, but every object–whether base or OO–has class
#> [1] "NULL"
#> [1] "character"
#> [1] "integer"
#> [1] "complex"
#> [1] "closure"
#> [1] "special"
#> [1] "builtin"
#> [1] "symbol"
#> [1] "language"
#> [1] "pairlist"
Base types in R
The graph above was made with SankeyMATIC
// toggle "Show Values"
// set Default Flow Colors from "each flow's Source"
base\ntypes [8] vectors
base\ntypes [3] functions
base\ntypes [1] environments
base\ntypes [1] S4 OOP
base\ntypes [3] language\ncomponents
base\ntypes [6] C components
vectors [1] NULL
vectors [1] logical
vectors [1] integer
vectors [1] double
vectors [1] complex
vectors [1] character
vectors [1] list
vectors [1] raw
functions [1] closure
functions [1] special
functions [1] builtin
environments [1] environment
S4 OOP [1] S4
language\ncomponents [1] symbol
language\ncomponents [1] language
language\ncomponents [1] pairlist
C components [1] externalptr
C components [1] weakref
C components [1] bytecode
C components [1] promise
C components [1] ...
C components [1] any
#> [1] "double"
#> [1] "integer"
#> [1] "double"
#> [1] "double"
is.numeric()
tests whether an object behaves like a numberBut Advanced R consistently uses numeric to mean integer or double type.