createR6Instance: Create R6 class instances

Description Usage Arguments Author(s) References See Also Examples

View source: R/r6.r

Description

Creates an instance of an R6 class defined on the fly via createR6Class.

Usage

1
createR6Instance(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

createR6Class

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
##------------------------------------------------------------------------------
## Default input //
##------------------------------------------------------------------------------

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

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

##------------------------------------------------------------------------------
## 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
    }
  )
)

## Step by step to see what's actually going on //
inst <- createR6Instance(input, lazy = TRUE)
inst
class(inst)
## --> call

inst <- eval(inst)
inst
class(inst)
ls(inst)
## --> object generator

inst <- inst$new()
inst
ls(inst)
## --> instance

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

##------------------------------------------------------------------------------
## Lazy evaluation //
##------------------------------------------------------------------------------

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

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

createR6Instance(input, name = "TestClass")

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

createR6Instance()
createR6Instance(lazy = TRUE)

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