Description Usage Arguments Value Author(s) References See Also Examples
See generic: setAnywhereOption
1 2 3 4 5 6 | ## S4 method for signature 'character,ANY,character'
setAnywhereOption(id, value,
where = tryCatch(devtools::as.package(".")$package, error = function(cond) {
stop("Invalid default value for `where`") }), fail_value = NULL,
force = FALSE, gap = TRUE, must_exist = FALSE, reactive = FALSE,
return_status = TRUE, strict = c(0, 1, 2), typed = FALSE, ...)
|
id |
|
value |
|
where |
|
fail_value |
|
force |
|
gap |
|
must_exist |
|
reactive |
|
return_status |
|
strict |
|
typed |
|
logical
. TRUE
.
Janko Thyson janko.thyson@gmail.com
http://github.com/Rappster/optionr
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | ## Not run:
##------------------------------------------------------------------------------
## Basics //
##------------------------------------------------------------------------------
container <- initializeOptionContainer(overwrite = TRUE)
## Simple name/ID //
setAnywhereOption(id = "test", value = TRUE)
getAnywhereOption(id = "test")
## Path-like name/ID //
setAnywhereOption(id = "test", value = new.env())
## --> note that `test` is overwritten and thus transformed from a "leaf"
## to a "branch" component (i.e. an environment)
getAnywhereOption(id = "test")
ls(getAnywhereOption(id = "test"))
setAnywhereOption(id = "test/a", value = TRUE)
ls(getAnywhereOption(id = "test"))
getAnywhereOption(id = "test/a")
## Must exist //
setAnywhereOption(id = "test/b", value = TRUE, must_exist = TRUE)
try(setAnywhereOption(id = "test/b", value = TRUE, must_exist = TRUE, strict = 2))
## Typed //
setAnywhereOption(id = "test/c", value = "hello world!", typed = TRUE)
setAnywhereOption(id = "test/c", value = 1:3)
## --> wrong class, but `strict = 0` --> disregarded without warning or error
getAnywhereOption(id = "test/c")
## --> still `hello world!` because `value = 1:3` had wrong class
setAnywhereOption(id = "test/c", value = "hello world!", typed = TRUE, strict = 1)
try(setAnywhereOption(id = "test/c", value = 1:3))
## --> warning and no assignment
getAnywhereOption(id = "test/c")
## --> still `hello world!`
setAnywhereOption(id = "test/c", value = "hello world!", typed = TRUE, strict = 2)
try(setAnywhereOption(id = "test/c", value = 1:3))
## --> error
getAnywhereOption(id = "test/c")
## --> still `hello world!`
setAnywhereOption(id = "test/a", value = "something else")
## --> correct class --> value changed
getAnywhereOption(id = "test/a")
##------------------------------------------------------------------------------
## Numerical names/IDs //
##------------------------------------------------------------------------------
container <- initializeOptionContainer(overwrite = TRUE)
setAnywhereOption(id = "20140101", value = TRUE)
ls(container, all.names = TRUE)
getAnywhereOption(id = "20140101")
##------------------------------------------------------------------------------
## Branch gaps //
##------------------------------------------------------------------------------
container <- initializeOptionContainer(overwrite = TRUE)
setAnywhereOption(id = "a/b/c/d", value = TRUE)
try(setAnywhereOption(id = "a/b/c/d", value = TRUE, strict = 2))
## --> branch gap: branches a, b and c do not exist yet
## Closing the gap //
setAnywhereOption(id = "a/b/c/d", value = TRUE)
## Inspect //
ls(container)
getAnywhereOption(id = "a")
getAnywhereOption(id = "a/b")
getAnywhereOption(id = "a/b/c")
getAnywhereOption(id = "a/b/c/d")
##------------------------------------------------------------------------------
## Forcing leafs to branches //
##------------------------------------------------------------------------------
container <- initializeOptionContainer(overwrite = TRUE)
setAnywhereOption(id = "a", value = "hello world!")
setAnywhereOption(id = "a/b", value = 10)
try(setAnywhereOption(id = "a/b", value = 10, strict = 2))
## --> starting branch `a` is not an environment
getAnywhereOption(id = "a")
## Forcing leaf into a branch //
setAnywhereOption(id = "a/b", value = 10, force = TRUE)
getAnywhereOption(id = "a")
getAnywhereOption(id = "a/b")
##------------------------------------------------------------------------------
## Different `where` //
##------------------------------------------------------------------------------
where <- "test"
container <- initializeOptionContainer(id = where, overwrite = TRUE)
setAnywhereOption(id = "a/b/c", value = 10, where = where)
getAnywhereOption(id = "a/b/c", where = where)
identical(getOptionContainer(where), container)
exists("a", container)
where <- structure(list(id = "test"), class = "OptionContext.Test")
container <- initializeOptionContainer(id = where, overwrite = TRUE)
setAnywhereOption(id = "a/b/c", value = 10, where = where)
getAnywhereOption(id = "a/b/c", where = where)
identical(getOptionContainer(where), container)
exists("a", container)
##------------------------------------------------------------------------------
## Reactive options: simple name/ID //
##------------------------------------------------------------------------------
container <- initializeOptionContainer(overwrite = TRUE)
setAnywhereOption(id = "x_1", value = TRUE, reactive = TRUE)
setAnywhereOption(
id = "x_2",
value = reactiveOption(!getAnywhereOption(id = "x_1"))
)
## `x_2` should always be the opposite of `x_1`
## Note that you can ommit `reactive = TRUE` when `value = reactiveOption(...)`
getAnywhereOption(id = "x_1")
getAnywhereOption(id = "x_2")
## Changing via options //
setAnywhereOption(id = "x_1", value = FALSE)
getAnywhereOption(id = "x_1")
getAnywhereOption(id = "x_2")
## When changed manually //
container <- getOptionContainer()
container$x_1 <- TRUE
container$x_1
container$x_2
## Trying to change bound variable //
setAnywhereOption(id = "x_2", value = TRUE)
getAnywhereOption(id = "x_2")
## --> has no effect; warning and error behavior can be specified via `strict`
##------------------------------------------------------------------------------
## Reactive options: path-like name/ID //
##------------------------------------------------------------------------------
container <- initializeOptionContainer(overwrite = TRUE)
setAnywhereOption(id = "a/test", value = TRUE, reactive = TRUE)
setAnywhereOption(id = "b/test",
value = reactiveOption(!getAnywhereOption(id = "a/test"))
)
getAnywhereOption(id = "a/test")
getAnywhereOption(id = "b/test")
setAnywhereOption(id = "a/test", value = FALSE)
getAnywhereOption(id = "a/test")
getAnywhereOption(id = "b/test")
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.