ensureOptionContainer: Ensure Option Container (generic)

Description Usage Arguments Author(s) References See Also Examples

Description

Ensures the existence of an environment that serves as an option container. The container will be assigned to R option based on signature argument id.

Usage

1
2
3
4
ensureOptionContainer(id = tryCatch(devtools::as.package(".")$package, error =
  function(cond) {     stop("Invalid default value for `id`") }),
  container = new.env(), sub_id = character(), check = TRUE,
  hidden = TRUE, overwrite = FALSE, ...)

Arguments

id

Signature argument. Object containing suitable information to control the actual ensurance process. In the simplest case, this corresponds to the name/ID of a package/package project. But it can also be an instance of a custom class for which a suitable methods are defined.

container

Signature argument. Object containing container information.

sub_id

character. Optional ID for a sub layer. Useful for a hub-like option container structure.

check

logical. TRUE: check if an R option with name/ID according to the information in id already exists (in which case an error is thrown); FALSE: no check for existing R options. Note that overwrite will overrule check.

hidden

logical. TRUE: make sure name/ID information in id is preprended with a dot to hide it; FALSE: use name/ID information in id as is. The former reduces the risk of accidentially overwriting existing R options and thus is used by default.

overwrite

logical. TRUE: overwrite existing container; FALSE: keep existing container.

...

Further arguments to be passed to subsequent functions/methods.

Author(s)

Janko Thyson janko.thyson@gmail.com

References

http://github.com/Rappster/optionr

See Also

ensureOptionContainer-char-env-method, initializeOptionContainer

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
74
75
76
77
78
79
80
## Not run: 

##------------------------------------------------------------------------------
## Default ID //
##------------------------------------------------------------------------------

options(".optionr" = NULL)
res <- ensureOptionContainer()
".optionr" %in% names(options())
ensureOptionContainer(overwrite = TRUE)
identical(getOption(".optionr"), res)
## --> identical environment object is reused --> ensures pass-by-references
## keeps working even when `overwrite = TRUE`

## Clean up //
options(".optionr" = NULL)

##------------------------------------------------------------------------------
## Explicit ID //
##------------------------------------------------------------------------------

res <- ensureOptionContainer(id = "abcd")
".abcd" %in% names(options())
ensureOptionContainer(id = "abcd", overwrite = TRUE)
identical(getOption(".abcd"), res)
## --> identical environment object is reused --> ensures pass-by-references
## keeps working even when `overwrite = TRUE`

## Clean up //
options(".abcd" = NULL)

##------------------------------------------------------------------------------
## Sub ID //
##------------------------------------------------------------------------------

res <- ensureOptionContainer(id = "abcd", sub_id = "a")
"a" %in% ls(res)

## Clean up //
options(".abcd" = NULL)

##------------------------------------------------------------------------------
## Not hidden //
##------------------------------------------------------------------------------

options("optionr" = NULL)
res <- ensureOptionContainer(hide = FALSE)
"optionr" %in% names(options())
identical(getOption("optionr"), res)

## Clean up //
options("optionr" = NULL)

##------------------------------------------------------------------------------
## Check for existing options of same name/ID //
##------------------------------------------------------------------------------

options(".optionr" = NULL)
res <- ensureOptionContainer()
try(ensureOptionContainer(check = TRUE))

## Clean up //
options(".optionr" = NULL)

##------------------------------------------------------------------------------
## As interface //
##------------------------------------------------------------------------------
  
id <- structure(list(id = "test"), class = "OptionContext.Test")
options("test" = NULL)

res <- ensureOptionContainer(id, hide = FALSE)
"test" %in% names(options())
identical(getOption(id$id), res)  

## Clean up //
options("test" = NULL)


## End(Not run)

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