A regular (S4) class may contain an S3 class, if that class has been registered (by calling
setOldClass). The functions described here provide
information about contained S3 classes. See the section ‘Functions’.
In modern R, these functions are not usually needed to program with objects from the S4 class. Standard computations work as expected, including method selection for both S4 and S3. To coerce an object to its contained S3 class, use either of the expressions:
as(object, S3Class); as(object, "S3")
S3Class evaluates to the name of the contained class. These
return slightly different objects, which in rare cases may need to
be distinguished. See the section “Contained S3 Objects”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
S3Part(object, strictS3 = FALSE, S3Class) S3Class(object) isXS3Class(classDef) slotsFromS3(object) ## the replacement versions of the functions are not recommended ## Create a new object from the class or use the replacement version of as(). S3Part(object, strictS3 = FALSE, needClass = ) <- value S3Class(object) <- value
an object from some class that extends a registered S3 class, or a basic vector, matrix or array object type.
For most of the functions, an S3 object can also be supplied, with the interpretation that it is its own S3 part.
a class definition object, as returned by
The remaining arguments apply only to the replacement versions, which are not recommended.
Require that the replacement value be this class or a subclass of it.
S3Part: Returns an object from the S3 class that appeared
contains= argument to
If called with
strictS3 = TRUE,
S3Part() constructs the underlying
S3 object by eliminating
all the formally defined slots and turning off the S4 bit of the
strictS3 = FALSE the object returned is from
the corresponding S4 class. For consistency and generality,
S3Part() works also for classes that extend the basic vector,
matrix and array classes.
A call to is equivalent coercing the object to class
the strict case, or to whatever the specific S3 class was, for the
non-strict case. The
as() calls are usually easier for
readers to understand.
S3Class: Returns the character vector of S3 class(es) stored in
the object, if the class has the corresponding
Currently, the function defaults to
to whether the class defined by
extends S3 classes (specifically, whether it has the slot for
holding the S3 class).
slotsFromS3: returns a list of the relevant slot classes, or an
empty list for any other object.
slotsFromS3() is a generic function used
internally to access the slots associated with the S3 part of the
object. Methods for this function are created automatically when
setOldClass is called with the
argument. Usually, there is only one S3 slot, containing the S3
class, but the
S4Class argument may provide additional slots,
in the case that the S3 class has some guaranteed attributes that
can be used as formal S4 slots. See the corresponding section in
the documentation of
Registering an S3 class defines an S4 class. Objects from this
class are essentially identical in content to an object from the S3
class, except for two differences. The value returned by
class() will always be a single string for the S4
isS4() will return
FALSE in the two cases. See the example below. It is barely
possible that some S3 code will not work with the S4 object; if so,
Objects from a class that extends an S3 class will have some basic type and
possibly some attributes. For an S3 class that has an equivalent S4
"data.frame"), an extending S4 class will
have a data part and slots. For other S3 classes (e.g.,
object from the extending S4 class will be some sort of basic type,
nearly always a vector type (e.g.,
but the data part will not have a formal definition.
Registering an S3 class by a call to
setOldClass creates a class of the same name with a slot
".S3Class" to hold
the corresponding S3 vector of class strings.
New S4 classes that extend such
classes also have the same slot, set to the S3 class of the
contained S3 object,
which may be an
(S3) subclass of the registered class.
For example, an S4 class might contain the S3 class
an object from the class might contain an object from class
"mlm", as in the
R is somewhat arbitrary about what
it treats as an S3 class:
"ts" is, but
For classes that extend
those, assuming they contain an S3 class is incorrect and will cause some
confusion—not usually disastrous, but the better strategy
is to stick to the explicit “class”.
as(x, "matrix") rather than
as(x, "S3") or
Objects in R have an internal bit that indicates whether or not to
treat the object as coming from an S4 class. This bit is tested by
isS4 and can be set on or off by
The latter function, however, does no checking or interpretation;
you should only use it if you are very certain every detail has been
As a friendlier alternative, methods have been defined for coercing
to the virtual classes
"S4". The expressions
as(object, "S3") and
as(object, "S4") return S3
and S4 objects, respectively. In addition, they attempt
to do conversions in a valid way, and also check validity when
coercing to S4.
as(object, "S3") can be used in two ways. For
objects from one of the registered S3 classes, the expression will
ensure that the class attribute is the full multi-string S3 class
class(object). If the registered class has known
attribute/slots, these will also be provided.
Another use of
as(object, "S3") is to take an S4 object and
turn it into an S3 object with corresponding attributes. This is
only meaningful with S4 classes that have a data part. If you want
to operate on the object without invoking S4 methods, this
conversion is usually the safest way.
as(object, "S4") will use the attributes in
the object to create an object from the S4 definition of
class(object). This is a general mechanism to create
partially defined version of S4 objects via S3 computations (not
much different from invoking
new with corresponding
arguments, but usable in this form even if the S4 object has an
initialize method with different arguments).
Chambers, John M. (2016) Extending R, Chapman & Hall. (Chapters 9 and 10, particularly Section 10.8)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## an "mlm" object, regressing two variables on two others sepal <- as.matrix(datasets::iris[,c("Sepal.Width", "Sepal.Length")]) fit <- lm(sepal ~ Petal.Length + Petal.Width + Species, data = datasets::iris) class(fit) # S3 class: "mlm", "lm" ## a class that contains "mlm" myReg <- setClass("myReg", slots = c(title = "character"), contains = "mlm") fit2 <- myReg(fit, title = "Sepal Regression for iris data") fit2 # shows the inherited "mlm" object and the title identical(S3Part(fit2), as(fit2, "mlm")) class(as(fit2, "mlm")) # the S4 class, "mlm" class(as(fit2, "S3")) # the S3 class, c("mlm", "lm") ## An object may contain an S3 class from a subclass of that declared: xlm <- setClass("xlm", slots = c(eps = "numeric"), contains = "lm") xfit <- xlm(fit, eps = .Machine$double.eps) xfit@.S3Class # c("mlm", lm")