withFormalClass: Define R6 class and buffer formal S4 equivalent

Description Usage Arguments Value Author(s) References See Also Examples

Description

Takes a regular R6 class definition and does the following things with it:

  1. Evaluate the class definition (main effect)

  2. Extract and store S3 class graph (i.e. including inheritance structure) in a special options environment. This information can in turn be processed by formalizeClasses inside your package's call to .onLoad in order to produce S4 equivalents of your R6 classes

Usage

1
2
withFormalClass(def, envir = parent.frame(), auto_assign = FALSE,
  r6 = TRUE)

Arguments

def

Actual R6 class definition, i.e. a call to R6Class.

envir

environment. Parent environment for the assignment of the class generator object. Only relevant if auto_assign = TRUE.

auto_assign

logical. TRUE: assign class generator object to an object whose name corresponds to the class name in envir; FALSE: you must make sure that you assign the output of the function to the desired name.

r6

logical. TRUE: include "R6" in the S3 class graph in order to denote that any R6 class inherits from class R6; FALSE: only buffer the actual class name. Setting this to TRUE makes sense when you plan on defining S4 methods that should work for any R6 method (as opposed to methods for specific R6 classes only).

Value

Return value of R6Class.

Author(s)

Janko Thyson janko.thyson@gmail.com

References

http://github.com/rappster/r6x

See Also

formalizeClasses

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
##------------------------------------------------------------------------------
## Class without inheritance
##------------------------------------------------------------------------------

## Ensure clean initial state //
if (exists("Test")) {
  rm("Test")
}
exists("Test")
getOption(".classes")

## Define R6 class //
Test <- withFormalClass(
  R6Class(
    classname = "Test",
    portable = TRUE,
    public = list(
      foo = function() "hello world!"
    )
  )
)

## Investigate state after definition //
exists("Test")
Test
ls(getOption(".classes"))
getOption(".classes")$Test
## --> buffered S3 class information that is later used by `formalizeClasses()`

## Auto-assign //
rm(Test)
withFormalClass(
  R6Class(
    classname = "Test",
    portable = TRUE,
    public = list(
      foo = function() "hello world!"
    )
  ),
  auto_assign = TRUE
)
## --> note that the class generator object is automaticall assigned to `Test`
exists("Test")
Test

##------------------------------------------------------------------------------
## Class with inheritance
##------------------------------------------------------------------------------

Test2 <- withFormalClass(
  R6Class(
    classname = "Test2",
    portable = TRUE,
    inherit = r6x::Test,
    public = list(
      bar = function() "hello world!"
    )
  )
)

## Inspect buffered S3 class information //
ls(getOption(".classes"))
getOption(".classes")$Test2
## --> S3 inheritance graph

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