### Name: S3Part ### Title: S3-style Objects and S4-class Objects ### Aliases: S3Part S3Part<- S3Class S3Class<- isXS3Class slotsFromS3 S4 S3 ### coerce,ANY,S3-method coerce,oldClass,S3-method coerce,ANY,S4-method ### S3-class ### Keywords: programming classes ### ** Examples ## two examples extending S3 class "lm", class "xlm" directly and "ylm" indirectly setClass("xlm", representation(eps = "numeric"), contains = "lm") setClass("ylm", representation(header = "character"), contains = "xlm") ## Don't show: ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2,10,20, labels=c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) ## End Don't show ## lm.D9 is as computed in the example for stats::lm y1 = new("ylm", lm.D9, header = "test", eps = .1) xx = new("xlm", lm.D9, eps =.1) y2 = new("ylm", xx, header = "test") stopifnot(inherits(y2, "lm")) stopifnot(identical(y1, y2)) stopifnot(identical(S3Part(y1, strict = TRUE), lm.D9)) ## note the these classes can insert an S3 subclass of "lm" as the S3 part: myData <- data.frame(time = 1:10, y = (1:10)^.5) myLm <- lm(cbind(y, y^3) ~ time, myData) # S3 class: c("mlm", "lm") ym1 = new("ylm", myLm, header = "Example", eps = 0.) ##similar classes to "xlm" and "ylm", but extending S3 class c("mlm", "lm") setClass("xmm", representation(eps = "numeric"), contains = "mlm") setClass("ymm", representation(header="character"), contains = "xmm") ym2 <- new("ymm", myLm, header = "Example2", eps = .001) # but for class "ymm", an S3 part of class "lm" is an error: try(new("ymm", lm.D9, header = "Example2", eps = .001)) setClass("dataFrameD", representation(date = "Date"), contains = "data.frame") myDD <- new("dataFrameD", myData, date = Sys.Date()) ## S3Part() applied to classes with a data part (.Data slot) setClass("NumX", contains="numeric", representation(id="character")) nn = new("NumX", 1:10, id="test") stopifnot(identical(1:10, S3Part(nn, strict = TRUE))) m1 = cbind(group, weight) setClass("MatX", contains = "matrix", representation(date = "Date")) mx1 = new("MatX", m1, date = Sys.Date()) stopifnot(identical(m1, S3Part(mx1, strict = TRUE))) ## Don't show: for(cl in c("ylm", "xlm", "ymm", "xmm", "dataFrameD", "NumX", "MatX")) removeClass(cl) ## End Don't show