Learn R Programming

Xmisc (version 0.2.1)

xRefClass-class: Extended Reference Class

Description

The Extended Reference Class (xRefClass) inherits directly from envRefClass. Listed are some of its key features:
  • Method initialize passes arguments by position or name.
  • Method copy2, a modified version of copy, is tolerant to activeBindingFunction as fields.
  • Method update updates a class instance's methods according to any update of the class.

Arguments

Fields

.index
named_numeric indexes of args
.default
named_list default values of args
.meta
named_list additional args (meta information)
.envir
environment. Default: as.environment(.self)
.tmp.list
list for temporary storage
.out.list
list for outputting

Methods

copy2(shallow = FALSE)
Modified version of `copy' to allow `activeBindingFunction' as fields.
update(x)
Modify method definition without re-create the class instance. x: character, methods to be updated.

Details

Extended Reference Class

See Also

methods::ReferenceClasses

Examples

Run this code
## Not run: 
# 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!
# ## End(Not run)

Run the code above in your browser using DataLab