################################################### ### chunk number 1: A0 ################################################### S <- function(){ options(useFancyQuotes=FALSE) # options(prompt=" ",continue=" ") # setwd("~/My Documents/Docs articles polys résumés/R/tutorial S4 en") setwd("~/Mes Documents/Docs articles polys résumés/R/tutorial S4 en") Sweave("S4tutorialV0-5en.Rnw") } catError <- function(err,long=90){ cat(strsplit(err,split=" ")[[1]],fill=long) } ################################################### ### chunk number 2: A1 ################################################### ### Traditional programming, BMI weight <- 85 size <- 1.84 (BMI <- weight/size^2) ################################################### ### chunk number 3: A2 ################################################### ### Traditional programming, my BMI weightMe <- 85 sizeMe <- 1.84 (BMIMe <- weightMe/sizeMe^2) ### Traditional programming, her BMI weightHer <- 62 sizeHer <- 1.60 (BMIHer <- weightMe/sizeHer^2) ################################################### ### chunk number 4: A3 ################################################### ### Definition of an object BMI setClass("BMI", representation(weight="numeric", size="numeric")) setMethod("show", "BMI", function(object){cat("BMI=",object@weight/(object@size^2)," \n ")} ) ################################################### ### chunk number 5: A4 ################################################### ### Creation of an object for me, and posting of my BMI (myBMI <- new("BMI",weight=85,size=1.84)) ### Creation of an object for her, and posting of her BMI (herBMI <- new("BMI",weight=62,size=1.60)) ################################################### ### chunk number 6: A5a ################################################### ### traditional programming, no type (weight <- "Hello") ### object programming, type ################################################### ### chunk number 7: A5b eval=FALSE ################################################### ## new("BMI",weight="Hello",size=1.84) ################################################### ### chunk number 8: A5c ################################################### catError(try(new("BMI",weight="Hello",size=1.84))) ################################################### ### chunk number 9: A6a ################################################### ### Traditional programming, without control (SizeMe <- -1.84) ### Object programming, control setValidity("BMI", function(object){if(object@size<0){return("negative Size")}else{return(TRUE)}} ) ################################################### ### chunk number 10: A6b eval=FALSE ################################################### ## new("BMI",weight=85,size=-1.84) ## # Erreur ################################################### ### chunk number 11: A6c ################################################### catError(try(new("BMI",weight=85,size=-1.84))) ################################################### ### chunk number 12: A7 ################################################### ### Definition of the heir setClass("BMIplus",representation(sex="character"),contains="BMI") he <- new("BMIplus",size=1.76,weight=84,sex="Male") ### Posting which uses what was defined for "BMI" he ################################################### ### chunk number 13: A8 ################################################### setClass( Class="Trajectories", representation=representation( times = "numeric", traj = "matrix" ) ) ################################################### ### chunk number 14: A10 ################################################### new(Class="Trajectories") ################################################### ### chunk number 15: A11 ################################################### new(Class="Trajectories",times=c(1,3,4)) new(Class="Trajectories",times=c(1,3),traj=matrix(1:4,ncol=2)) ################################################### ### chunk number 16: A12 ################################################### trajPitie <- new(Class="Trajectories") trajCochin <- new( Class= "Trajectories", times=c(1,3,4,5), traj=rbind ( c(15,15.1, 15.2, 15.2), c(16,15.9, 16,16.4), c(15.2, NA, 15.3, 15.3), c(15.7, 15.6, 15.8, 16) ) ) trajStAnne <- new( Class= "Trajectories", times=c(1: 10, (6: 16) *2), traj=rbind( matrix (seq (16,19, length=21), ncol=21, nrow=50, byrow=TRUE), matrix (seq (15.8, 18, length=21), ncol=21, nrow=30, byrow=TRUE) )+rnorm (21*80,0,0.2) ) ################################################### ### chunk number 17: A13 ################################################### trajCochin@times trajCochin@times <- c(1,2,4,5) trajCochin ################################################### ### chunk number 18: A14 ################################################### setClass( Class = "TrajectoriesBis", representation=representation( time = "numeric", traj = "matrix" ), prototype=prototype( time = 1, traj = matrix (0) ) ) ################################################### ### chunk number 19: A15a ################################################### removeClass("TrajectoriesBis") ################################################### ### chunk number 20: A15b eval=FALSE ################################################### ## new(Class="TrajectoiresBis") ################################################### ### chunk number 21: A15c ################################################### catError(try(new(Class="TrajectoiresBis"))) ################################################### ### chunk number 22: A16 ################################################### identical(numeric(),integer()) ################################################### ### chunk number 23: A17 ################################################### slotNames("Trajectories") getSlots ("Trajectories") getClass ("Trajectories") ################################################### ### chunk number 24: A18a eval=FALSE ################################################### ## size <- rnorm(10,1.70,10) ## weight <- rnorm(10,70,5) ## group <- as.factor(rep(c("A","B"),5)) ## ## par(mfrow=c(1,2)) ## plot (size~weight) ## plot (size~group) ################################################### ### chunk number 25: A18b ################################################### size <- rnorm(10,1.70,10) weight <- rnorm(10,70,5) group <- as.factor(rep(c("A","B"),5)) par(mfrow=c(1,2)) plot (size~weight) plot (size~group) ################################################### ### chunk number 26: A19a eval=FALSE ################################################### ## setMethod( ## f= "plot", ## signature= "Trajectories", ## definition=function (X,y,...){ ## matplot(x@times,t(x@traj),xaxt="n",type="l",ylab= "",xlab="", pch=1) ## axis(1,at=x@times) ## } ## ) ## par(mfrow=c (1,2)) ## plot(trajCochin) ## plot(trajStAnne) ################################################### ### chunk number 27: A19b ################################################### setMethod( f="plot", signature="Trajectories", definition=function(x,y,...){ matplot(x@times,t(x@traj),xaxt="n",type="l",ylab="",xlab="",pch=1) axis(1,at=x@times) } ) par(mfrow=c(1,2)) plot(trajCochin) plot(trajStAnne) ################################################### ### chunk number 28: A20 ################################################### args (plot) ################################################### ### chunk number 29: A21 ################################################### setMethod ("print","Trajectories", function(x,...){ cat("*** Class Trajectories, method Print *** \n") cat("* Times ="); print (x@times) cat("* Traj = \n"); print (x@traj) cat("******* End Print (trajectories) ******* \n") } ) print(trajCochin) ################################################### ### chunk number 30: A22 ################################################### setMethod("show","Trajectories", function(object){ cat("*** Class Trajectories, method Show *** \n") cat("* Times ="); print(object@times) nrowShow <- min(10,nrow(object@traj)) ncolShow <- min(10,ncol(object@traj)) cat("* Traj (limited to a matrix 10x10) = \n") print(formatC(object@traj[1:nrowShow,1:ncolShow]),quote=FALSE) cat("******* End Show (trajectories) ******* \n") } ) trajStAnne ################################################### ### chunk number 31: A23a eval=FALSE ################################################### ## new("Trajectories") ################################################### ### chunk number 32: A23b ################################################### catError(try(show(new("Trajectories")))) ################################################### ### chunk number 33: A24 ################################################### setMethod("show","Trajectories", function(object){ cat("*** Class Trajectories, method Show *** \n") cat("* Times = "); print (object@times) nrowShow <- min(10,nrow(object@traj)) ncolShow <- min(10,ncol(object@traj)) cat("* Traj (limited to a matrix 10x10) = \n") if(length(object@traj)!=0){ print(formatC(object@traj[1:nrowShow,1:ncolShow]),quote=FALSE) }else{} cat("******* End Show (trajectories) ******* \n") } ) new("Trajectories") ################################################### ### chunk number 34: A25 ################################################### setGeneric ( name= "countMissing", def=function(object){standardGeneric("countMissing")} ) ################################################### ### chunk number 35: A26 ################################################### setMethod( f= "countMissing", signature= "Trajectories", definition=function(object){ return(sum(is.na(object@traj))) } ) countMissing(trajCochin) ################################################### ### chunk number 36: A27a ################################################### lockBinding("countMissing",.GlobalEnv) ################################################### ### chunk number 37: A27b eval=FALSE ################################################### ## setGeneric( ## name="countMissing", ## def=function(object,value){standardGeneric("countMissing")} ## ) ################################################### ### chunk number 38: A27c ################################################### catError( try( setGeneric(name="countMissing",def=function(object,value){standardGeneric("countMissing")}) ) ) unlockBinding("countMissing",.GlobalEnv) ################################################### ### chunk number 39: A27d eval=FALSE ################################################### ## setGenericVerif <- function(x,y){if(!isGeneric(x)){setGeneric(x,y)}else{}} ################################################### ### chunk number 40: A28 ################################################### showMethods(class="Trajectories") ################################################### ### chunk number 41: A29a ################################################### getMethod(f="plot",signature="Trajectories") ################################################### ### chunk number 42: A29b eval=FALSE ################################################### ## getMethod(f="plot",signature="Trjectoires") ################################################### ### chunk number 43: A29c ################################################### catError(try(getMethod(f="plot",signature="Trjectoires"))) ################################################### ### chunk number 44: A30 ################################################### existsMethod(f="plot",signature="Trajectories") existsMethod(f="plot",signature="Partition") ################################################### ### chunk number 45: A31 ################################################### setClass( Class="Trajectories", representation(times="numeric",traj="matrix"), validity=function(object){ cat("~~~ Trajectories: inspector ~~~ \n") if(length(object@times)!=ncol(object@traj)){ stop ("[Trajectories: validation] the number of temporal measurements does not correspond to the number of columns of the matrix") }else{} return(TRUE) } ) new(Class="Trajectories",times=1:2,traj=matrix(1:2,ncol=2)) ################################################### ### chunk number 46: A31b eval=FALSE ################################################### ## new(Class="Trajectories",times=1:3,traj=matrix(1:2,ncol=2)) ################################################### ### chunk number 47: A31c ################################################### catError(try(new(Class="Trajectories",times=1:3,traj=matrix(1:2,ncol=2)))) ################################################### ### chunk number 48: A32 ################################################### trajStLouis <- new(Class="Trajectories",times=c(1),traj=matrix(1)) ### No checking, the number of temporal measurements will no longer ### correspond to the trajectories (trajStLouis@times <- c(1,2,3)) ################################################### ### chunk number 49: A33 ################################################### setMethod( f="initialize", signature="Trajectories", definition=function(.Object,times,traj){ cat("~~~ Trajectories: initializator ~~~ \n") rownames(traj) <- paste("I",1:nrow(traj),sep= "") .Object@traj <- traj # Assignment of the slots .Object@times <- times return(.Object) # return of the object } ) new(Class="Trajectories",times=c(1,2,4,8),traj=matrix(1:8,nrow=2)) ################################################### ### chunk number 50: A34 ################################################### new(Class="Trajectories",times=c(1,2,48),traj=matrix(1:8,nrow=2)) ################################################### ### chunk number 51: A35 ################################################### setMethod ( f="initialize", signature="Trajectories", definition=function(.Object,times,traj){ cat ("~~~~~ Trajectories: initializator ~~~~~ \n") if(!missing(traj)){ colnames(traj) <- paste("T",times,sep="") rownames(traj) <- paste("I",1:nrow(traj),sep="") .Object@times <- times .Object@traj <- traj validObject(.Object) # call of the inspector } return(.Object) } ) new(Class="Trajectories",times=c(1,2,4,8),traj=matrix(1:8,nrow=2)) ################################################### ### chunk number 52: A35b eval=FALSE ################################################### ## new(Class="Trajectories",times=c(1,2,48),traj=matrix(1:8,nrow=2)) ################################################### ### chunk number 53: A35c ################################################### catError(try(new(Class="Trajectories",times=c(1,2,48),traj=matrix(1:8,nrow=2)))) ################################################### ### chunk number 54: A36 ################################################### setClass ( Class="TrajectoriesBis", representation( times = "numeric", traj = "matrix" ) ) setMethod ("initialize", "TrajectoriesBis", function(.Object,nbWeek,BMIinit){ traj <- outer(BMIinit,1:nbWeek,function(init,week){return(init+0.1*week)}) colnames(traj) <- paste("T",1:nbWeek,sep="") rownames(traj) <- paste("I",1:nrow(traj),sep="") .Object@times <- 1:nbWeek .Object@traj <- traj return(.Object) } ) new(Class="TrajectoriesBis",nbWeek=4,BMIinit=c(16,17,15.6)) ################################################### ### chunk number 55: A37 ################################################### tr <- trajectories <- function(times,traj){ cat ("~~~~~ Trajectories: constructor ~~~~~ \n") new (Class="Trajectories",times=times,traj=traj) } trajectories(time=c(1,2,4),traj=matrix(1:6,ncol=3)) ################################################### ### chunk number 56: A38 ################################################### trajectories <- function(times,traj){ if(missing(times)){times <- 1:ncol(traj)} new(Class="Trajectories",times=times,traj=traj) } trajectories(traj=matrix(1:8,ncol=4)) ################################################### ### chunk number 57: A39 ################################################### regularTrajectories <- function(nbWeek,BMIinit) { traj <- outer(BMIinit,1:nbWeek,function(init,week){return(init+0.1*week)}) times <- 1: nbWeek return(new(Class="Trajectories",times=times,traj=traj)) } regularTrajectories(nbWeek=3,BMIinit=c(14,15,16)) ################################################### ### chunk number 58: A39 ################################################### ### Getter for "times" setGeneric("getTimes",function(object){standardGeneric ("getTimes")}) setMethod("getTimes","Trajectories", function(object){ return(object@times) } ) getTimes(trajCochin) ### Getter for "traj" setGeneric("getTraj",function(object){standardGeneric("getTraj")}) setMethod("getTraj","Trajectories", function(object){ return(object@traj) } ) getTraj(trajCochin) ################################################### ### chunk number 59: A40 ################################################### ### Getter for the inclusion BMI (first column of "traj") setGeneric("getTrajInclusion",function(object){standardGeneric("getTrajInclusion")}) setMethod ("getTrajInclusion","Trajectories", function(object){ return(object@traj[,1]) } ) getTrajInclusion(trajCochin) ################################################### ### chunk number 60: A41 ################################################### setGeneric("setTimes<-",function(object,value){standardGeneric("setTimes<-")}) setReplaceMethod( f="setTimes", signature="Trajectories", definition=function(object,value){ object@times <-value return (object) } ) getTimes(trajCochin) setTimes(trajCochin) <- 1:3 getTimes(trajCochin) ################################################### ### chunk number 61: A42a ################################################### setReplaceMethod( f="setTimes", signature="Trajectories", definition=function(object,value){ object@times <- value validObject(object) return(object) } ) setTimes(trajCochin) <- c(1,2,4,6) ################################################### ### chunk number 62: A42b eval=FALSE ################################################### ## setTimes(trajCochin) <- 1:4 ################################################### ### chunk number 63: A42c ################################################### catError(try(setTimes(trajCochin) <- 1:2)) ################################################### ### chunk number 64: A43 ################################################### setMethod( f= "[", signature="Trajectories", definition=function(x,i,j,drop){ if(i=="times"){return(x@times)}else {} if(i=="traj"){return(x@traj)}else {} } ) trajCochin["times"] trajCochin["traj"] ################################################### ### chunk number 65: A44 ################################################### setReplaceMethod( f="[", signature="Trajectories", definition=function(x,i,j,value){ if(i=="times"){x@times<-value}else{} if(i=="traj"){x@traj<-value}else{} validObject(x) return (x) } ) trajCochin["times"] <- 2:5 ################################################### ### chunk number 66: A45 ################################################### setClass( Class="Partition", representation=representation ( nbGroups="numeric", part="factor" ) ) setGeneric("getNbGroups",function(object){standardGeneric("getNbGroups")}) setMethod("getNbGroups","Partition",function(object){return(object@nbGroups)}) setGeneric("getPart",function(object){standardGeneric("getPart")}) setMethod("getPart","Partition",function(object){return(object@part)}) partCochin <- new(Class="Partition",nbGroups=2,part=factor(c("A","B","A","B"))) partStAnne <- new(Class="Partition",nbGroups=2,part=factor(rep(c("A","B"),c(50,30)))) ################################################### ### chunk number 67: A46 ################################################### setGeneric("test",function(x,y,...){standardGeneric("test")}) ################################################### ### chunk number 68: A47a ################################################### setMethod("test","numeric",function(x,y,...){cat("x is numeric =",x,"\n")}) ### 3.17 being a numeric, R will apply the test method for the test numeric test(3.17) ################################################### ### chunk number 69: A47b eval=FALSE ################################################### ## ### "E" being a character, R will not find a method ## test("E") ################################################### ### chunk number 70: A47c ################################################### catError(try(test("E"))) ################################################### ### chunk number 71: A47d ################################################### setMethod("test","character",function(x,y,...){cat("x is character = ",x,"\n")}) ### Since "E" is a character, R now apply the method for character. test("E") ### But the method for numeric is still here: test(-8.54) ################################################### ### chunk number 72: A48 ################################################### ### For a method which combines numeric and character: setMethod( f="test", signature=c(x="numeric",y="character"), definition=function(x,y,...){ cat("more complicated: ") cat("x is numeric =",x," AND y is a character = ",y, "\n") } ) test(3.2, "E") ### The previous definition are still available test(3.2) test("E") ################################################### ### chunk number 73: A49a ################################################### setMethod( f="plot", signature=c(x="Trajectories",y="Partition"), definition=function(x,y,...){ matplot(x@times,t(x@traj[y@part=="A",]),ylim=range(x@traj,na.rm=TRUE), xaxt="n",type="l",ylab="",xlab="",col=2) for(i in 2:y@nbGroups){ matlines(x@times,t(x@traj[y@part==LETTERS[i],]),xaxt="n",type="l",col=i+1) } axis(1,at=x@times) } ) ################################################### ### chunk number 74: A49b ################################################### par(mfrow=c(2,2)) ### Plot for "Trajectory" plot(trajCochin) plot(trajStAnne) ### Plot for "Trajectory" plus "Partition" plot(trajCochin,partCochin) plot(trajStAnne,partStAnne) ################################################### ### chunk number 75: A49c ################################################### par(mfrow=c(2,2)) ### Plot pour `Trajectoire' plot(trajCochin) plot(trajStAnne) ### Plot pour `Trajectoire' plus `Partition' plot(trajCochin,partCochin) plot(trajStAnne,partStAnne) ################################################### ### chunk number 76: A50 ################################################### showMethods(test) ################################################### ### chunk number 77: A51 ################################################### setMethod ( f="test", signature=c(x="numeric",y="missing"), definition=function(x,y,...){cat("x is numeric = ",x," and y is 'missing' \n")} ) ### Method without y thus using the missing test(3.17) ### Method with y='character' test(3.17, "E") ### Method with y='numeric'. y is not missing, y is not character, therefore "ANY" is used test (3.17, 2) ################################################### ### chunk number 78: A52 ################################################### setClass( Class="TrajPartitioned", representation=representation(listPartitions="list"), contains= "Trajectories" ) tdPitie <- new("TrajPartitioned") ################################################### ### chunk number 79: A53 ################################################### tdPitie ################################################### ### chunk number 80: A53a ################################################### unclass(tdPitie) ################################################### ### chunk number 81: A53b ################################################### cat(' attr(,"listPartitions") list() attr(,"times") numeric(0) attr(,"traj") <0 x 0 matrix>') ################################################### ### chunk number 82: A54a ################################################### partCochin2 <- new("Partition",nbGroups=3,part=factor(c("A","C","C","B"))) ################################################### ### chunk number 83: A54b eval=FALSE ################################################### ## tdCochin <- new( ## Class="TrajPartitioned", ## times=c(1,3,4,5), ## traj=trajCochin@traj, ## listPartitions=list(partCochin,partCochin2) ## ) ################################################### ### chunk number 84: A54c ################################################### catError( try(tdCochin <- new( Class="TrajDecoupees", times=c(1,3,4,5), traj=trajCochin@traj, listePartitions=list(partCochin,partCochin2) )) ) ################################################### ### chunk number 85: A55a eval=FALSE ################################################### ## getMethod("initialize","TrajPartitioned") ################################################### ### chunk number 86: A55b ################################################### removeMethod("initialize","TrajPartitioned") catError(try(getMethod("initialize","TrajPartitioned"))) ################################################### ### chunk number 87: A56 ################################################### existsMethod("initialize","TrajPartitioned") ################################################### ### chunk number 88: A57 ################################################### hasMethod("initialize","TrajPartitioned") ################################################### ### chunk number 89: A58 ################################################### selectMethod ("initialize", "TrajPartitioned") ################################################### ### chunk number 90: A59 ################################################### setMethod("initialize","TrajPartitioned", function(.Object,times,traj,listPartitions){ cat("~~~~TrajPartitioned: initializator ~~~~ \n") if(!missing(traj)){ .Object@times <- times .Object@traj <- traj # Assignment of attributes .Object@listPartitions <- listPartitions } return(.Object) # return of the object } ) tdCochin <- new( Class="TrajPartitioned", traj=trajCochin@traj, times=c(1,3,4,5), listPartitions=list(partCochin,partCochin2) ) ################################################### ### chunk number 91: A60 ################################################### print (tdCochin) ################################################### ### chunk number 92: A61 ################################################### setMethod( f="print", signature="TrajPartitioned", definition=function(x,...){ callNextMethod() cat("the object also contains",length(x@listPartitions),"partition") cat("\n ***** Fine of print (TrajPartitioned) ***** \n") return(invisible()) } ) print(tdCochin) ################################################### ### chunk number 93: A62 ################################################### print(as(tdPitie,"Trajectories")) ################################################### ### chunk number 94: A63 ################################################### setMethod ( f="show", signature="TrajPartitioned", definition=function(object){ show(as(object,"Trajectories")) lapply(object@listPartitions,show) } ) ################################################### ### chunk number 95: A64 ################################################### is(trajCochin,"TrajPartitioned") is(tdCochin,"Trajectories") ################################################### ### chunk number 96: A65 ################################################### ### Creation of empty TrajPartitioned tdStAnne <- new("TrajPartitioned") ### Assignment of Trajectories to the attributes of TrajPartitioned as(tdStAnne,"Trajectories") <- trajStAnne tdStAnne ################################################### ### chunk number 97: A66 ################################################### setIs( class1="TrajPartitioned", class2="Partition", coerce=function(from,to){ numberGroups <- sapply(tdCochin@listPartitions,getNbGroups) Smallest <- which.min(-numberGroups) to<-new("Partition") to@nbGroups <- getNbGroups(from@listPartitions[[Smallest]]) to@part <- getPart(from@listPartitions[[Smallest]]) return(to) } ) is(tdCochin,"Partition") as(tdCochin,"Partition") ################################################### ### chunk number 98: A67 ################################################### setIs( class1="TrajPartitioned", class2="Partition", coerce=function(from,to) { numberGroups <- sapply(tdCochin@listPartitions,getNbGroups) largest <- which.min(-numberGroups) to <- new("Partition") to@nbGroups <- getNbGroups(from@listPartitions[[largest]]) to@part <- getPart(from@listPartitions[[largest]]) return(to) }, replace=function(from,value){ numberGroups <- sapply(tdCochin@listPartitions,getNbGroups) smallest <- which.min(numberGroups) from@listPartitions[[smallest]] <- value return(from) } ) as(tdCochin,"Partition") as(tdCochin,"Partition") <- partCochin2 ################################################### ### chunk number 99: A68 ################################################### setClass( Class="PartitionFather", representation=representation(nbGroups="numeric","VIRTUAL") ) setClass( Class="PartitionSimple", representation=representation(part="factor"), contains="PartitionFather" ) setClass( Class="PartitionEval", representation=representation(part="ordered"), contains="PartitionFather" ) setGeneric("nbMultTwo",function(object){standardGeneric("nbMultTwo")}) setMethod("nbMultTwo","PartitionFather", function(object){ object@nbGroups <- object@nbGroups*2 return (object) } ) a <- new("PartitionSimple",nbGroups=3,part=factor(LETTERS[c(1,2,3,2,2,1)])) nbMultTwo(a) b <- new("PartitionEval",nbGroups=5,part=ordered(LETTERS[c(1,5,3,4,2,4)])) nbMultTwo(b) ################################################### ### chunk number 100: A69 ################################################### func <- function () { x <- 5 cat(x) return(invisible ()) } ### Creation of x in "global" x <- 2 ### Call of the function: "local" x is create and printed func() ### Return to global: "local" x is removed x ################################################### ### chunk number 101: A70 ################################################### meanWithoutNa <- function (x){mean(x,na.rm=TRUE)} setGeneric("impute",function (.Object){standardGeneric("impute")}) setMethod( f="impute", signature="Trajectories", def=function(.Object){ average <- apply(.Object@traj,2,meanWithoutNa) for (iCol in 1:ncol(.Object@traj)){ .Object@traj[is.na(.Object@traj[,iCol]),iCol] <- average[iCol] } return(.Object) } ) impute(trajCochin) ################################################### ### chunk number 102: A71 ################################################### trajCochin ################################################### ### chunk number 103: A72 ################################################### trajCochin <- impute(trajCochin) ################################################### ### chunk number 104: A73 ################################################### testCarre <- function(x){ nameObject <- deparse(substitute(x)) print(nameObject) assign(nameObject,x^2,envir=parent.frame()) return(invisible()) } a<-2 testCarre(a) a ################################################### ### chunk number 105: A74 ################################################### setMethod( f="impute", signature="Trajectories", def=function(.Object){ nameObject<-deparse(substitute(.Object)) average <- apply(.Object@traj,2,meanWithoutNa) for (iCol in 1:ncol(.Object@traj)){ .Object@traj[is.na(.Object@traj[,iCol]),iCol] <- average [iCol] } assign(nameObject,.Object,envir=parent.frame()) return(invisible()) } ) impute(trajCochin) ################################################### ### chunk number 106: A75 eval=FALSE ################################################### ## setMethod ( ## f= " methodA ", ## signature= " classA ", ## definition=function () {cat ("Blah")} ## ) ################################################### ### chunk number 107: A76 eval=FALSE ################################################### ## .classA.methodA <- function () {cat ("Blah")} ## ### Here, we test .classA.methodA: ## ### - seek bugs ## ### - detection of global variables with findGlobals ## ### - testing values ## ### - ... ## ## ### Then the method definition ## setMethod ( ## f="methodA", ## signature="classA", ## definition=.classA.methodA ## ) ## ## ### And the cleaning ## rm(.classA.methodA) ################################################### ### chunk number 108: A77 eval=FALSE ################################################### ## ### Class creation ## setClass ( ## Class= "NewClass", ## representation=representation(x="numeric",y="character"), ## prototype=prototype (x=1,y="A"), ## contains=c("FatherClass"), ## validity=function(object){return(TRUE)} ## ) ## ## ### Object creation ## new(Class="NewClass") ## A <- new(Class="NewClass",x=2,y="B") ## ## ### Slot manipulation ## A@x <- 4 # (iark!) ## ## ### Class destruction (partial) ## removeClass("NewClass") ## ## ### Constructor ## newClass <- function(){ ## .... ## return(new(Class="NewClass")) ## } ################################################### ### chunk number 109: A78 eval=FALSE ################################################### ## setMethod(f="initialize",signature="NewClass", ## definition=function(.Object,value){ ## if(....){stop("initialize (NewClass): Error")}else{} ## .Object@x <- value; ## validObject(.Object) ## return(.Object) ## } ## ) ################################################### ### chunk number 110: A79 eval=FALSE ################################################### ## ### Getter ## setGeneric(name="getX",def=function(object){standardGeneric("getX")}) ## setMethod(f="getX",signature="NewClass", ## definition=function(object){return(object@x)} ## ) ## ## ### Setter ## setGeneric(name="setX<-",def=function(object,value){standardGeneric("setX<-")}) ## setReplaceMethod(f="setX",signature="NewClass", ## def=function(object,value){object@x<-value;return(object)} ## ) ################################################### ### chunk number 111: A80 eval=FALSE ################################################### ## ### To create a generic method ## setGeneric(f="newFunction",def=function(z,r){standardGeneric("newFunction")}) ## ## ### To declare a method ## setMethod(f="newFunction",signature="NewClass", ## def=function(z,r){....;return(....)} ## ) ## ## ### To get the arguments of a function ## args(NewFunction)