MyClass <-
setRefClass(
"MyClass",
list(
x="numeric",
y="numeric",
z=function(){x+y}
),
contains="xRefClass",
methods=list(
initialize=function(...){
.idx <- c(x=1,y=2)
callSuper(...,.index=.idx)
},
printme=function(){
cat('Hello World!','\n')
}
)
)
## Method initialize - pass by position
obj <- MyClass$new(1,2)
obj$x
obj$y
## Method initialize - pass by name
obj <- MyClass$new(y=2)
obj$x
obj$y
## Method copy
## obj <- MyClass$new(1,2)
## obk <- obj$copy() # Fail!
## ## Error in (function () : unused argument (quote("myclass"))
## Method copy2
obj <- MyClass$new(1,2) # No such error!
obk <- obj$copy2()
obk$z
## Method update
obj <- MyClass$new()
obj$printme()
MyClass <- # To modify one of the original functions
setRefClass(
"MyClass",
list(
x="numeric",
y="numeric",
z=function(){x+y}
),
contains="xRefClass",
methods=list(
initialize=function(...){
.idx <- c(x=1,y=2)
callSuper(...,.index=.idx)
},
printme=function(){ # This function is modified
cat('Hello R!','\n')
}
)
)
obj$printme() # The function is yet not modified
## Hello World!
obj$update("printme") # update the function
obj$printme() # The function is modified
## Hello R!Run the code above in your browser using DataLab