REVISED: Saturday, March 2, 2013
R Classes and Methods
R is object oriented. Objects are instances of classes. Vectors, data frames, matrices and lists are the major R structures; structures are different from classes.
R is object oriented. Objects are instances of classes. Vectors, data frames, matrices and lists are the major R structures; structures are different from classes.
I. INTRODUCTION TO R CLASSES AND METHODS
A. CLASS
The R code for classes is in the methods package, which is normally loaded by default. If it is not loaded, you can load it with the function library(methods).
setClass( ) function creates, defines, a class which is a description or blue print of a thing, a new data type.
Refer to ?Classes and ?setClass in the help documentation for details.
B. OBJECT
The new( ) function creates an object which is an instance of a class. The class( ) function can be used to determine the class of an object.
C. METHOD
Methods are dispatched by generic R functions. A method is the implementation of a generic function for an object of a particular class. Refer to the ?Methods, ?setMethod, and ?setGeneric in the help documentation for details. The showMethods() function can be used to determine methods for a particular generic function.
II. GENERIC FUNCTIONS
A class object is the first argument of a generic function.
The class of the object is checked by the generic function.
The existence of a method for the class is determined by a search. If it exists the method is called on the object. However, if there is no method for that class, a search is done for the default method for the generic. If a generic exists the generic is called.
If a generic does not exist an error is thrown.
You should never call methods directly. Rather, use the generic function and let the method be dispatched automatically.
If you write new methods for new classes, you should also consider writing methods for the following generics:
print/show
summary
plot
III. CREATING A NEW CLASS
A new class can be defined using the setClass( ) function.
You must specify the name of the class.
Specify data elements, also called slots.
Define methods for the class with the setMethods( ) function.
Use the showClass( ) function to get information about a class.
A. NEW CLASS EXAMPLE
setClass("polygon",
representation(x = "numeric",
y = "numeric"))
The slots for the polygon class are x and y and they can be accessed using the @ operator.
1. A plot method can be created using the setMethod( ) function. For setMethod( ) you need to specify a generic function (plot), and a signature. A signature is a character vector indicating the classes of objects accepted by the method.
In this example, the plot method will take one type of object; i.e., a polygon object.
setMethod("plot", "polygon"
function(x, y, ...){
plot(x@x, x@y, type = "n", ...)
xp <- c(x@x, x@x[1])
yp <- c(x@y, x@y[1])
lines(xp, yp)
})
Notice that the slots of the polygon, the x, y coordinates, are accessed with the @ operator.
After calling setClass( ) and setMethod( ) as shown above; you can call showMethods("plot").
B. Documentation
Refer to new class examples written in packages by other people on CRAN, or in the stats4 package that comes with R, to help you understand the process.
The R code for classes is in the methods package, which is normally loaded by default. If it is not loaded, you can load it with the function library(methods).
setClass( ) function creates, defines, a class which is a description or blue print of a thing, a new data type.
Refer to ?Classes and ?setClass in the help documentation for details.
B. OBJECT
The new( ) function creates an object which is an instance of a class. The class( ) function can be used to determine the class of an object.
C. METHOD
Methods are dispatched by generic R functions. A method is the implementation of a generic function for an object of a particular class. Refer to the ?Methods, ?setMethod, and ?setGeneric in the help documentation for details. The showMethods() function can be used to determine methods for a particular generic function.
II. GENERIC FUNCTIONS
A class object is the first argument of a generic function.
The class of the object is checked by the generic function.
The existence of a method for the class is determined by a search. If it exists the method is called on the object. However, if there is no method for that class, a search is done for the default method for the generic. If a generic exists the generic is called.
If a generic does not exist an error is thrown.
You should never call methods directly. Rather, use the generic function and let the method be dispatched automatically.
If you write new methods for new classes, you should also consider writing methods for the following generics:
print/show
summary
plot
III. CREATING A NEW CLASS
A new class can be defined using the setClass( ) function.
You must specify the name of the class.
Specify data elements, also called slots.
Define methods for the class with the setMethods( ) function.
Use the showClass( ) function to get information about a class.
A. NEW CLASS EXAMPLE
setClass("polygon",
representation(x = "numeric",
y = "numeric"))
The slots for the polygon class are x and y and they can be accessed using the @ operator.
1. A plot method can be created using the setMethod( ) function. For setMethod( ) you need to specify a generic function (plot), and a signature. A signature is a character vector indicating the classes of objects accepted by the method.
In this example, the plot method will take one type of object; i.e., a polygon object.
setMethod("plot", "polygon"
function(x, y, ...){
plot(x@x, x@y, type = "n", ...)
xp <- c(x@x, x@x[1])
yp <- c(x@y, x@y[1])
lines(xp, yp)
})
Notice that the slots of the polygon, the x, y coordinates, are accessed with the @ operator.
After calling setClass( ) and setMethod( ) as shown above; you can call showMethods("plot").
B. Documentation
Refer to new class examples written in packages by other people on CRAN, or in the stats4 package that comes with R, to help you understand the process.
Enjoy R Classes and Methods.
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to: ELCRIC OTTO CIRCLE's Home Page"