Callable S3/S4 Object in R

A function in R, like any other objects in R, can have additional attributes (or slots in S4), that is to say, a S3 or S4 object can inherit “function” semantics and become callable. The trick to enable the function to access its attributes or slots is to use sys.function function. Here is a simple example. pow <- function(x) { p <- attr(sys.function(), "p") if (is.null(p)) p <- 2 x^p } attr(pow, "p") <- 3 pow(2) ## [1] 8 We can modify the function’s attributes, leading to a different behavior of the function. [Read More]