Class Attribute of an Object

DESCRIPTION:

Return or change the class attribute of an object.

USAGE:

class(x) 
class(x) <- value 
unclass(x) 

REQUIRED ARGUMENTS:

x
a S-PLUS object.

OPTIONAL ARGUMENTS:

value
character string giving the new class of x.

VALUE:

class returns the class of the object x as a character string.

When used on the left of an assignment, class sets the class of its argument to the string on the right of the assignment. This is used in functions that create objects from objects of a particular class. In this case, x should be a prototype for the intended class. If you want to coerce x to be a of a particular class, use the as function instead.

unclass returns the representation of x as an object with the class of the prototype for class(x). If x has a representation with slots, unclass(x) returns a named list or structure, containing the slots in that representation. Otherwise, unclass(x) returns an object of the same class as the prototype of the class.

Use the function oldClass to retrieve and set classes for old-style objects in which the class has no representation and may have more than one character string in the value.

DETAILS:

The class of an object is used to decide which method of a generic function is to be used in a given instance.

Functions may use unclass in order to deal with an object in raw form without any assistance from methods that may be available to process that class. Setting class(x) to NULL is equivalent (for backward compatibility) but is now deprecated; it will not actually set the class to NULL because all objects are now classed.

Assigning a class to an object is a primitive operation. Assigning class "matrix" to object x, for example, does notcause x to be coerced to have all the attributes of a matrix object: if you wanted that to happen, use as.matrix(x). Similarly, class is not a generic function and methods cannot be written for it.

SEE ALSO:

, , , , , .

EXAMPLES:

class(state.x77) # returns "matrix" 
class(state.name) # returns "character"