Classes Corresponding to Basic Structures
The virtual class
structure and classes that
extend it are formal classes analogous to S language structures such
as arrays and time-series.
1 2 3 4 5 6 7 8 9
## The following class names can appear in method signatures, ## as the class in as() and is() expressions, and, except for ## the classes commented as VIRTUAL, in calls to new() "matrix" "array" "ts" "structure" ## VIRTUAL
Objects from the Classes
Objects can be created by calls of the form
Class is the quoted name of the specific class (e.g.,
"matrix"), and the other arguments, if any, are interpreted as
arguments to the corresponding function, e.g., to function
matrix(). There is no particular advantage over calling those
functions directly, unless you are writing software designed to work
for multiple classes, perhaps with the class name and the arguments
Objects created from the classes
are unusual, to put it mildly, and have been for some time. Although
they may appear to be objects from these classes, they do not have the
internal structure of either an S3 or S4 class object. In particular,
they have no
"class" attribute and are not recognized as
objects with classes (that is, both
isS4 will return
FALSE for such objects).
However, methods (both S4 and S3) can be defined for these
pseudo-classes and new classes (both S4 and S3) can inherit from them.
That the objects still behave as if they came from the corresponding
class (most of the time, anyway) results from special code
recognizing such objects being built into the base code of R.
For most purposes, treating the classes in the usual way will work,
fortunately. One consequence of the special treatment is that these
two classesmay be used as the data part of an S4 class; for
example, you can get away with
contains = "matrix" in a call
setGeneric to create an S4 class that is a subclass
"matrix". There is no guarantee that everything will work
perfectly, but a number of classes have been written in this form
Note that a class containing
.Data slot with that class. This is the only use of
.Data other than as a pseudo-class indicating the type of the
object. In this case the type of the object will be the type of the
contained matrix or array. See
Classes_Details for a general
"ts" is basically an S3 class
that has been registered with S4, using the
setOldClass mechanism. Versions of R through 2.7.0
treated this class as a pure S4 class, which was in principal a good
idea, but in practice did not allow subclasses to be defined and had
other intrinsic problems. (For example, setting the
"tsp" parameters as a slot often fails because the built-in
implementation does not allow the slot to be temporarily
inconsistent with the length of the data. Also, the S4 class
prevented the correct specification of the S3 inheritance for class
Time-series objects, in contrast to matrices and arrays, have a valid
"ts", registered using an S4-style definition (see the
setOldClass in the examples section
for an abbreviated listing of how this is done. The S3
"mts" in package stats is also
These classes, as well as
be valid in most examples as superclasses for new S4 class
All of these classes have special S4 methods for
initialize that accept the same arguments as the basic
ts, in so far as possible.
The limitation is that a class that has more than one non-virtual
superclass must accept objects from that superclass in the call to
new; therefore, a such a class (what is called a
“mixin” in some languages) uses the default method for
initialize, with no special arguments.
The specific classes all extend class
"structure", directly, and
"vector", by class
Methods are defined to coerce arbitrary objects to these classes, by calling the corresponding basic function, for example,
strict = TRUEin the call to
as(), the method goes on to delete all other slots and attributes other than the
Group methods (see, e.g.,
S4groupGeneric) are defined for combinations of structures and vectors (including special cases for array and matrix), implementing the concept of vector structures as in the reference. Essentially, structures combined with vectors retain the structure as long as the resulting object has the same length. Structures combined with other structures remove the structure, since there is no automatic way to determine what should happen to the slots defining the structure.
Note that these methods will be activated when a package is loaded containing a class that inherits from any of the structure classes or class
Chambers, John M. (2008) Software for Data Analysis: Programming with R Springer. (For the R version.)
Chambers, John M. (1998) Programming with Data Springer (For the original S4 version.)
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole (for the original vector structures).
Class nonStructure, which enforces the alternative model, in which all slots are dropped if any math transformation or operation is applied to an object from a class extending one of the basic classes.
1 2 3 4 5 6 7 8
Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.