Learn R Programming

Xmisc (version 0.0.9)

xRefClass-class: Extended Reference Class

Description

The Extended Reference Class (xRefClass) inherits directly from envRefClass. Listed are some of its key features:
  • Methodinitializepasses arguments by position or name.
  • Methodcopy2, a modified version ofcopy, is tolerant toactiveBindingFunctionas fields.
  • Methodupdateupdates a class instance's methods according to any update of the class.

Arguments

Details

Extended Reference Class

See Also

methods::ReferenceClasses

Examples

Run this code
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