Description Usage Arguments See Also Examples
This is a wrapper around setRefClass
. All arguments are defined in an expression (instead of lists) which improves readability of the code. Besides that, no additional features are added.
1 | defineRefClass(expr)
|
expr |
an expression |
Private-class
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 | ## Not run:
vignette("Introduction", "aoos")
## End(Not run)
# Minimal example:
Test <- defineRefClass({
Class <- "Test" # this is passed as argument to setRefClass
x <- "character" # all objects which are not functions are fields
do <- function() cat("Yes, Yes, I'm working...") # a method
})
test <- Test()
test$x <- "a"
test$do()
# Inheritance and privacy:
pTest <- defineRefClass({
Class <- "pTest"
# Privacy is solved by inheriting from a class 'Private' which redefines
# the methods for access.
contains <- c("Test", "Private") # passed as argument to setRefClass
.y <- "numeric" # this is going to be 'private'
doSomething <- function() {
.self$.y <- 42
cat(x, .y, "\n")
invisible(.self)
}
})
instance <- pTest()
instance$x <- "Value of .y:"
instance$doSomething()
# A notion of privacy:
stopifnot(inherits(try(instance$.y), "try-error"))
stopifnot(inherits(try(instance$.y <- 2), "try-error"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.