createR6Class: Define R6 classes on the fly

Description Usage Arguments Author(s) References See Also Examples

View source: R/r6.r

Description

Creates R6 class generator objects on the fly.

Usage

1
createR6Class(input, name = ".DerivedInternal", lazy = FALSE)

Arguments

input

list. Input for class fields. If elements with names public, private and active are found, then the respective values will be used as respective arguments value in the call to R6Class. Otherwise the list will be mapped to public fields (see examples).

name

character. Class name.

lazy

logical. TRUE: evaluate expression and thus return object generator; FALSE: return expression that will generate the object generator.

Author(s)

Janko Thyson janko.thyson@rappster.de

References

http://github.com/rappster/classr

See Also

createR6Instance

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
##------------------------------------------------------------------------------
## Default input //
##------------------------------------------------------------------------------

## This is ideal for rapid prototyping. All list elements are mapped to 
## publicly available fields.

inst <- createR6Class(input = list(x = 1, y = 2))
inst

##------------------------------------------------------------------------------
## Special input format //
##------------------------------------------------------------------------------

## For more fine control over the class structure.

input <- list(
  public = list(x = 10, foo = function() private$bar()),
  private = list(bar = function() "Hello World!"),
  active = list(
    x2 = function(value) {
      if (missing(value)) return(self$x * 2) else self$x <- value/2
    }
  )
)

## To see what's actually going on:
(inst <- createR6Class(input))
(inst <- inst$new())

inst$x
inst$foo
inst$foo()
inst$x2
inst$x2 <- 100
inst$x

##------------------------------------------------------------------------------
## Return expression //
##------------------------------------------------------------------------------

## Useful for lazy evaluation
(inst <- createR6Class(input, lazy = TRUE))
(inst <- eval(inst))
(inst <- inst$new())

##------------------------------------------------------------------------------
## Explicit name //
##------------------------------------------------------------------------------

createR6Class(input, name = "TestClass")

##------------------------------------------------------------------------------
## Empty //
##------------------------------------------------------------------------------

createR6Class(lazy = TRUE)

rappster/classr documentation built on May 26, 2019, 11:11 p.m.