Description Usage Arguments Author(s) References See Also Examples
Creates an instance of an R6 class defined on the fly via
createR6Class
.
1 | createR6Instance(input, name = ".DerivedInternal", lazy = FALSE)
|
input |
|
name |
|
lazy |
|
Janko Thyson janko.thyson@rappster.de
http://github.com/rappster/classr
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.