formalizeClasses: Create formal S4 equivalents of R6 classes

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

Description

Takes the S3 class information buffered in getOption(".classes") and uses it accordingly for calls to setOldClass.

Usage

1
2
3
formalizeClasses(where = eval(substitute(as.environment(NAME), list(NAME =
  sprintf("package:%s", as.list(read.dcf("DESCRIPTION")[1, ])[["Package"]])))),
  overwrite = TRUE, clean_up = TRUE)

Arguments

where

environment or NULL. Environment in which tne S4 equivalents should be defined. If it exists, this defaults to your packagte's namespace environment. If that does not exist, it is set to .GlobalEnv.

overwrite

logical. TRUE: ensure previously assigned S4 equivalents are removed from where before subsequent calls to setOldClass; FALSE: state of classes in where is not modified before subsequent calls to setOldClass. See section Recommendation.

clean_up

logical. TRUE: remove buffered S3 class information in options; FALSE: keep buffered S3 class information in options.

Details

The S4 equivalents are assigned to the package's namespace if it is available (i.e. as.environment("package:<package-name>")). This is definitely the case at the stage when .onLoad is calles, hence the call to this function should happen there.

Value

Return value of R6Class.

Recommendation

It seems to be a good advice to set overwrite = TRUE as this ensures that previously defined S4 equivalents are removed prior to the respective calls to setOldClass. Not doing so seems to have the undesired side effect that getClasses(where = as.environment("package:<package-name>")) returns character() for subsequent package loads via load_all (and thus subsequent calls of .onLoad).

Author(s)

Janko Thyson janko.thyson@gmail.com

References

http://github.com/rappster/r6x

See Also

withFormalClass

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
##------------------------------------------------------------------------------
## Stand-alone call
##------------------------------------------------------------------------------

##########
## NOTE ##
##########

## This example only serves the purpose of illustrating what's going on.
## In order for this function to be actually useful when developing own
## packages, it should be called inside `.onLoad()` (see example below)

## Preliminaries //
## Run example of `?withFormalClass`

## Buffered S3 class information //
clss <- getOption(".classes")
ls(clss)
lapply(ls(clss), function(ii) clss[[ii]])

## Note //
## Currently, there are no formal S4 equivalents for our R6 classes yet:
isClass("Test")
isClass("Test2")
try(getClass("Test"))
try(getClass("Test2"))

## Formalize classes //
formalizeClasses()

## Note //
## 1) Formal S4 equivalents have been created:
isClass("Test")
isClass("Test2")
try(getClass("Test"))
try(getClass("Test2"))
## --> note how the S3 inheritance information has been preserved

## 2) The buffered information has been deleted due to `clean_up = TRUE`:
getOption(".classes")

##------------------------------------------------------------------------------
## Call inside of `.onLoad()`
##------------------------------------------------------------------------------

##########
## NOTE ##
##########

## This function should be called in `.onLoad()` as at this stage the package
## has already been attached and thus a namespace environment has been created.
## This environment is the recommended value for the function's `envir`
## argument (automatically set, you don't need to provide it explicitly)

## So this is a template of how your `.onLoad()` function should look like
## when using this function. Just copy it to a file inside your `R` directory
## so it is picked up by `devtools::load_all()` or the like:

.onAttach <- function(libname, pkgname) {
  formalizeClasses()
}

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