typeof | mode | storage.mode | class |
---|---|---|---|
logical | logical | logical | logical |
integer | numeric | integer | integer |
double | numeric | double | double |
complex | complex | complex | complex |
character | character | character | character |
raw | raw | raw | raw |
S4 | S4 | S4 | <Class Details> |
# Create a kid object. > k1 <- list(name="Alice", gender="F", age=11) > class(k1) = "Kid" # Print the Kid object. > print(k1) $name [1] "Alice" $gender [1] "F" $age [1] 11 attr(,"class") [1] "Kid"
kid <- function(theName, theGender, theAge) { theObject <- list(name=theName, gender=theGender, age=theAge) class(theObject) = "Kid" return(theObject) }
> k2 <- kid("Mark", "M", 12) > print(k2) $name [1] "Alice" $gender [1] "F" $age [1] 11 attr(,"class") [1] "Kid"
> methods(print) [1] print.acf* [2] print.anova [3] print.aov* ... # 175 more print method namesThis method call returns the list of all 178 defined print methods, of which 146 are non-visible and marked with *.
> print.data.frame function (x, ..., digits = NULL, quote = FALSE, right = TRUE, row.names = TRUE) { n <- length(row.names(x)) if (length(x) == 0L) { cat(sprintf(ngettext(n, "data frame with 0 columns and %d row", "data frame with 0 columns and %d rows"), n), "\n", sep = "") } else if (n == 0L) { print.default(names(x), quote = FALSE) cat(gettext("<0 rows> (or 0-length row.names)\n")) } else { m <- as.matrix(format.data.frame(x, digits = digits, na.encode = FALSE)) if (!isTRUE(row.names)) dimnames(m)[[1L]] <- if (identical(row.names, FALSE)) rep.int("", n) else row.names print(m, ..., quote = quote, right = right) } invisible(x) } <bytecode: 0x0000000010f25020> <environment: namespace:base>
print.Kid <- function(theObject) { cat(theObject$name, theObject$gender, theObject$age, "\n") }
> print(k1) Alice F 11 > print(k2) Mark M 12
haveBirthday <- function(theObject) { UseMethod("haveBirthday", theObject) }
haveBirthday.Kid <- function(theObject) { theObject$age <- theObject$age + 1 return(theObject) }
> k1 <- haveBirthday(k1) > print(k1) Alice F 12 > k2 <- haveBirthday(k2) > print(k2) Mark M 13
haveBirthday.default <- function(theObject) { warning("Default haveBirthday method ", "called with unrecognized object") return(theObject) }
getName <- function(theObject) { UseMethod("getName", theObject) } getName.Kid <- function(theObject) { return(theObject$name) } getGender <- function(theObject) { UseMethod("getGender", theObject) } getGender.Kid <- function(theObject) { return(theObject$gender) } getAge <- function(theObject) { UseMethod("getAge", theObject) } getAge.Kid <- function(theObject) { return(theAge$name) }
> getName(k1) [1] "Alice" > getName(k2) [1] "Mark" > getGender(k1) [1] "F" > getGender(k2) [1] "M" > getAge(k1) [1] 12 > getAge(k2) [1] 12
> args(kid) function (thename, thegender, theage) NULL
> getClass("kid") Class "kid" [in ".GlobalEnv"] Slots: Name: name gender age Class: character character numericattributes Older function to get class information about an object. Example:
> attributes(k) $name [1] "Alice" $gender [1] "F" $age [1] 12 $class [1] "kid" attr(,"package") [1] ".GlobalEnv"getSlots Get the slots from the class definition. Example:
> getSlots("kid") name gender age "character" "character" "numeric"slotNames Get the slot names from an object of the class. Example:
> slotNames(k) [1] "name" "gender" "age"showMethods Show all the implementations of a generic method or all methods that are attached to a class. Examples:
> showMethods("show") Function: show (package methods) object="ANY" object="classRepresentation" object="envRefClass" object="function" (inherited from: object="ANY") object="genericFunction" object="genericFunctionWithTrace" object="kid" object="MethodDefinition" object="MethodDefinitionWithTrace" object="MethodSelectionReport" object="MethodsList" (inherited from: object="ANY") object="MethodWithNext" object="MethodWithNextWithTrace" object="namedList" object="nonstandardGenericFunction" (inherited from: object="genericFunction") object="ObjectsWithPackage" object="oldClass" object="refClassRepresentation" object="refMethodDef" object="refObjectGenerator" object="signature" object="sourceEnvironment" object="traceable" > showMethods("age") Function: age (package .GlobalEnv) x="kid" > showMethods(classes="kid") Function: age (package .GlobalEnv) x="kid" Function: age<- (package .GlobalEnv) x="kid" Function: gender (package .GlobalEnv) x="kid" Function: haveBirthday (package .GlobalEnv) x="kid" Function: initialize (package methods) .Object="kid" (inherited from: .Object="ANY") Function: name (package .GlobalEnv) x="kid" Function: show (package methods) object="kid"getMethod Get details of a method with a specified signature. Example:
> getMethod("haveBirthday", "kid") Method Definition: function (x) { x@age <- x@age + 1 x } Signatures: x target "kid" defined "kid"existsMethod Determine if a method exists with a specified signature. Example:
> existsMethod("haveBirthday", "kid") [1] TRUE