The R Project SVN R

Rev

Rev 71366 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/methods/man/StructureClasses.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
59039 ripley 3
% Copyright 1995-2011 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
19087 jmc 6
\name{StructureClasses}
42839 maechler 7
\title{Classes Corresponding to Basic Structures}
19087 jmc 8
\docType{class}
42839 maechler 9
%
19087 jmc 10
\alias{structure-class}
11
\alias{matrix-class}
12
\alias{array-class}
13
\alias{ts-class}
42839 maechler 14
% Group Methods of these
15
\alias{Math,structure-method}
16
\alias{Ops,structure,vector-method}
17
\alias{Ops,structure,structure-method}
18
\alias{Ops,structure,array-method}
19
\alias{Ops,vector,structure-method}
20
\alias{Ops,array,structure-method}
21
\alias{Ops,array,array-method}
45653 jmc 22
\alias{initialize,array-method}
23
\alias{initialize,matrix-method}
24
\alias{initialize,ts-method}
46503 jmc 25
\alias{initialize,mts-method}
45653 jmc 26
\alias{show,ts-method}
42839 maechler 27
%
28
\description{
29
  The virtual class \code{structure} and classes that
19087 jmc 30
  extend it are formal classes analogous to S language structures such
42839 maechler 31
  as arrays and time-series.
32
}
19087 jmc 33
\usage{
40284 ripley 34
## The following class names can appear in method signatures,
19087 jmc 35
## as the class in as() and is() expressions, and, except for
36
## the classes commented as VIRTUAL, in calls to new()
37
 
38
"matrix"
39
"array"
40
"ts"
41
 
42
"structure" ## VIRTUAL
43
}
44
\section{Objects from the Classes}{
23531 hornik 45
  Objects can be created by calls of the form \code{new(Class, ...)},
46
  where \code{Class} is the quoted name of the specific class (e.g.,
47
  \code{"matrix"}), and the other arguments, if any, are interpreted as
48
  arguments to the corresponding function, e.g., to function
49
  \code{matrix()}.  There is no particular advantage over calling those
50
  functions directly, unless you are writing software designed to work
51
  for multiple classes, perhaps with the class name and the arguments
52
  passed in.
45653 jmc 53
 
56211 maechler 54
  Objects created from the classes \code{"matrix"} and \code{"array"}
55
  are unusual, to put it mildly, and have been for some time.  Although
56
  they may appear to be objects from these classes, they do not have the
57
  internal structure of either an S3 or S4 class object.  In particular,
58
  they have no \code{"class"} attribute and are not recognized as
59
  objects with classes (that is, both \code{\link{is.object}} and
45653 jmc 60
  \code{\link{isS4}} will return \code{FALSE} for such objects).
51966 jmc 61
  However, methods (both S4 and S3) can be defined for these
62
  pseudo-classes and new classes (both S4 and S3) can inherit from them.
45653 jmc 63
 
64
  That the objects still behave as if they came from the corresponding
65
  class (most of the time, anyway) results from special code
66
  recognizing such objects being built into the base code of \R.
67
  For most purposes, treating the classes in the usual way will work,
68
  fortunately.  One consequence of the special treatment is that these
69
  two classes\emph{may} be used as the data part of an S4 class; for
70
  example, you can get away with \code{contains = "matrix"} in a call
71
  to \code{\link{setGeneric}} to create an S4 class that is a subclass
72
  of \code{"matrix"}.  There is no guarantee that everything will work
73
  perfectly, but a number of classes have been written in this form
74
  successfully.
75
 
51966 jmc 76
  Note that a class containing \code{"matrix"} or \code{"array"} will
77
  have a  \code{.Data} slot with that class.  This is the only use of
78
  \code{.Data} other than as a pseudo-class indicating the type of the
79
  object.  In this case the type of the object will be the type of the
71366 jmc 80
  contained matrix or array. See \code{\link{Classes_Details}} for a general
51966 jmc 81
  discussion.
82
 
45653 jmc 83
  The class \code{"ts"}  is basically an S3 class
84
  that has been registered with S4, using the
85
  \code{\link{setOldClass}} mechanism.  Versions of \R through 2.7.0
86
  treated this class as a pure S4 class, which was in principal a good
87
  idea, but in practice did not allow subclasses to be defined and had
88
  other intrinsic problems.  (For example, setting the
89
  \code{"tsp"} parameters as a slot often fails because the built-in
90
  implementation does not allow the slot to be temporarily
91
  inconsistent with the length of the data. Also, the S4 class
92
  prevented the correct specification of the S3 inheritance for class
93
  \code{"mts"}.)
94
 
51966 jmc 95
  Time-series objects, in contrast to matrices and arrays, have a valid
96
  S3 class, \code{"ts"}, registered  using an S4-style definition (see the
46503 jmc 97
  documentation for \code{\link{setOldClass}} in the examples section
74363 maechler 98
  for an abbreviated listing of how this is done).  The S3
49587 hornik 99
  inheritance of \code{"mts"} in package \pkg{stats} is also
45824 jmc 100
  registered.
46503 jmc 101
  These classes, as well as \code{"matrix"} and \code{"array"} should
102
  be valid in most examples as superclasses for new S4 class
103
  definitions.
45824 jmc 104
 
51966 jmc 105
  All of these classes have special S4 methods for
106
  \code{\link{initialize}} that accept the same arguments as the basic
107
  generator functions, \code{\link{matrix}},
108
  \code{\link{array}}, and \code{\link{ts}}, in so far as possible.
109
  The limitation is that a class that has more than one non-virtual
110
  superclass must accept objects from that superclass in the call to
111
  \code{\link{new}}; therefore, a such a class (what is called a
112
  \dQuote{mixin} in some languages) uses the default method for
113
  \code{\link{initialize}}, with no special arguments.
56211 maechler 114
 
19087 jmc 115
}
116
\section{Extends}{
23531 hornik 117
  The specific classes all extend class \code{"structure"}, directly, and
118
  class \code{"vector"}, by class \code{"structure"}.
19087 jmc 119
}
120
\section{Methods}{
121
  \describe{
122
    \item{coerce}{Methods are defined to coerce arbitrary objects to
123
      these classes, by calling the corresponding basic function, for
48062 jmc 124
      example, \code{as(x, "matrix")} calls \code{as.matrix(x)}.
125
      If \code{strict = TRUE} in the call to \code{as()}, the method
126
      goes on to delete all other slots and attributes other than the
127
      \code{dim} and \code{dimnames}.
128
    }
47924 jmc 129
    \item{Ops}{Group methods (see, e.g., \code{\link{S4groupGeneric}})
42839 maechler 130
      are defined for combinations of structures and vectors (including
131
      special cases for array and matrix), implementing the concept of
47924 jmc 132
      vector structures as in the reference.  Essentially, structures
133
      combined with vectors retain the structure as long as the
134
      resulting object has the same length.  Structures combined with
135
      other structures remove the structure, since there is no
136
      automatic way to determine what should happen to the slots
137
      defining the structure.
138
 
139
      Note that these methods will be activated when a package is loaded
140
      containing a class that inherits from any of the structure
141
      classes or class \code{"vector"}.
42839 maechler 142
    }
19087 jmc 143
  }
144
}
48062 jmc 145
\seealso{Class \linkS4class{nonStructure}, which enforces the
146
  alternative model, in which all slots are dropped if any math
56211 maechler 147
  transformation or operation is applied to an object from a class
148
  extending one of the basic classes.
48062 jmc 149
}
42839 maechler 150
\references{
45653 jmc 151
 Chambers, John M. (2008)
152
 \emph{Software for Data Analysis: Programming with R}
153
  Springer.  (For the R version.)
154
 
155
 Chambers, John M. (1998)
156
 \emph{Programming with Data}
157
 Springer (For the original S4 version.)
158
 
42839 maechler 159
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
160
  \emph{The New S Language}.
47262 ripley 161
  Wadsworth & Brooks/Cole (for the original vector structures).
42839 maechler 162
}
163
\examples{
164
showClass("structure")
165
 
166
## explore a bit :
167
showClass("ts")
168
(ts0 <- new("ts"))
169
str(ts0)
170
 
171
showMethods("Ops") # six methods from these classes, but maybe many more
172
}
19087 jmc 173
\keyword{classes}