Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This brief tutorial illustrates how to combine S4 object oriented capabilities with function closures in order to develop classes with built in methods. Thanks to Hadley Wickham for the great contribution of material and tutorials made available on the web and to Bill Venables and Stefano Iacus for their kind reviews.

## Regular polygons

As Wikipedia states:

In Euclidean geometry, a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). Square, pentagon, hexagon are regular polygons.

In order to define a regular polygon we need to state the number of sides the polygon is made of (n) and the dimension of its side (s).

## S4

We want to define a define a simple S4 based system to create and plot of regular polygons.

As a first step we define a very simple class for regular polygons, say rolygon, as:

 > setClass("rolygon", representation(n = "numeric", + s = "numeric"))

In order to plot our rolygons we need a plot method for class rolygon. By mean of basic trigonometry we get:

 > setMethod(f = "plot", signature = "rolygon", + definition = function(x, y) { + object = x + s = [email protected] + n = [email protected] + rho = (2*pi)/n + h = .5*s*tan((pi/2)-(pi/n)) + r = sqrt(h^2+(s/2)^2) + sRho = ifelse(n %% 2 == 0, + (pi/2- rho/2), pi/2) + cumRho = cumsum(c(sRho, rep(rho, n))) + cumRho = ifelse(cumRho > 2*pi, + cumRho-2*pi, cumRho) + x = r*cos(cumRho) + y = r*sin(cumRho) + par(pty = "s") + plot(x, y, type = "n", xlab = "", ylab = "") + lines(x, y, col = "red", lwd = 2) + points(0, 0, pch = 16, col = "red") + grid() + invisible(NULL) + })   [1] "plot"

And, as a result:

 > plot(new("rolygon", n = 5, s = 2))

## Function Closures

The R Language Definition manual states:

Functions … have three basic components: a formal argument list, a body and an environment. A function’s environment is the environment that was active at the time that the function was created. Any symbols bound in that environment are captured and available to the function. This combination of the code of the function and the bindings in its environment is called a ‘function closure’, a term from functional programming theory.

Moreover, any time a function is called, a new environment is created, whose enclosure is the environment where the function was defined. The computation, as expressed by the body of the function, occurs in the newly created environment. Thus, whenever we call a function we have at least two environments: the environment the function was defined in and the environment where the function evaluation takes place.

By using this idea, we can define a function f() that returns a function g(). As g() is created within the evaluation environment of f(), this last environment is the enclosure of g(). Therefore, g() remembers all symbols bound in that environment.

As a practical application of this idea consider this function:

 > f <- function(x) { + g = function(y){x+y} + g }

As g() is created within the evaluation environment of f()g() "remembers" the value of x. Therefore we can define a simple function f1() that adds one to the given y argument as:

 > f1 <- f(x = 1) > f1(y = 3)

Note that f1() remembers the value of x. As a result:

 > ls(env=environment(f1)) [1] "g" "x" > get("x", env=environment(f1)) [1] 1

The environment of f1() can be directly accessed and manipulated: