9

If given an object x, is there a way to classify whether or not it is S3 or S4 (or "other")? I have looked at is.object() and isS4(), and can identify that something is an object (or not) and that it is an S4 object (or not). However, it doesn't seem to me that S3 objects are the complement of all objects that are not S4 objects.

Therefore, how can these assignments be done programmatically?

Here is an example of something that bugs me, taken from the help for is.object():

a = as.factor(1:3)
is.object(a)  # TRUE
isS4(a) # FALSE

Does that mean that a is an S3 object?

Iterator
  • 20,250
  • 12
  • 75
  • 111

1 Answers1

10

If it is an object and is not an S4 then it is an S3:

is.object(foo) & !isS4(foo)

is.object checks for some magic OBJECT bit that gets set when the thing has a class attribute, so its essentially a fast way of doing any(names(attributes(foo))=="class"), which is what defines an S3 object.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Well that pretty much answers it. :) – Iterator Aug 05 '11 at 17:16
  • I now think there should be an `isS3` along the lines of what you suggest with `any...`. As it is, basing an assignment on the logical operation `(A & !B)` doesn't allow for a `C` that is pairwise mutually exclusive with each of `A` and `B` (e.g. some future "S5" class that is neither S3 nor S4). For now, this works, but I feel like I'm walking toward a Y2K-type bug. :( – Iterator Aug 06 '11 at 13:51