Description Usage Arguments Details Author(s) References See Also Examples
Creates an implicitly typed object.
1 2 3 |
id |
|
value |
|
where |
|
numint |
|
from_null |
|
to_null |
|
inherit |
|
return_invis |
|
strict |
|
... |
Further arguments to be passed to subsequent functions/methods. In particular, all environments of references that you are referring to in the body of the binding function. See section Referenced environments. |
The type (class) of value
is remembered and stored in an invisible
object part curtesy of makeActiveBinding
. For any
subsequent assignment operations on the object, the type (class) of
value
is checked against the originally stored type (class).
Type (class) mismatches trigger an error, a warning or are silently
ignored depending on the value of strict
.
Janko Thyson janko.thyson@rappster.de
http://github.com/Rappster/typr
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 163 164 165 166 167 168 169 170 171 | ## Not run:
##------------------------------------------------------------------------------
## Levels of strictness //
##------------------------------------------------------------------------------
## Strict = 2 //
(setTyped(id = "x_1", value = 10))
x_1
try(x_1 <- "hello world!")
## --> ignored with error
x_1
## Strict = 1 //
setTyped(id = "x_1", value = 10, strict = 1)
try(x_1 <- "hello world!")
## --> ignored with warning
x_1
## Strict = 0 //
setTyped(id = "x_1", value = 10, strict = 0)
x_1 <- "hello world!"
x_1
## --> simply ignored
##------------------------------------------------------------------------------
## NULL //
##------------------------------------------------------------------------------
## Change from NULL //
(setTyped(id = "x_1"))
x_1
x_1 <- "hello world!"
x_1
## --> overwritten
setTyped(id = "x_1", from_null = FALSE)
try(x_1 <- "hello world!")
## --> ignored with error
setTyped(id = "x_1", from_null = FALSE, strict = 1)
try(x_1 <- "hello world!")
## --> ignored with warning
setTyped(id = "x_1", from_null = FALSE, strict = 0)
x_1 <- "hello world!"
x_1
## --> simply ignored
## Change to NULL //
setTyped(id = "x_1", value = 10)
x_1 <- NULL
x_1
## --> overwritten
setTyped(id = "x_1", value = 10, to_null = FALSE)
try(x_1 <- NULL)
## --> ignored with error
setTyped(id = "x_1", value = 10, to_null = FALSE, strict = 1)
try(x_1 <- NULL)
## --> ignored with warning
setTyped(id = "x_1", value = 10, to_null = FALSE, strict = 0)
x_1 <- NULL
x_1
## --> simply ignored
##------------------------------------------------------------------------------
## Numerical values //
##------------------------------------------------------------------------------
## Change from `numeric` to `integer` //
setTyped(id = "x_1", 10)
class(x_1)
## --> numeric
(x_1 <- as.integer(20))
class(x_1)
## --> overwritten
setTyped(id = "x_1", 10, numint = FALSE)
try(x_1 <- as.integer(20))
## --> ignored with error
setTyped(id = "x_1", 10, numint = FALSE, strict = 1)
try(x_1 <- as.integer(20))
## --> ignored with warning
setTyped(id = "x_1", 10, numint = FALSE, strict = 0)
x_1 <- as.integer(20)
x_1
class(x_1)
## --> simply ignored
## Change from `integer` to `numeric` //
(setTyped(id = "x_1", as.integer(10)))
class(x_1)
## --> integer
x_1 <- 20
x_1
class(x_1)
## --> overwritten
setTyped("x_1", as.integer(10), numint = FALSE)
try(x_1 <- 20)
## --> ignored with error
setTyped("x_1", as.integer(10), numint = FALSE)
try(x_1 <- 20)
## --> ignored with warning
setTyped("x_1", as.integer(10), numint = FALSE, strict = 0)
x_1 <- 20
x_1
class(x_1)
## --> simply ignored
##------------------------------------------------------------------------------
## Inheritance //
##------------------------------------------------------------------------------
value <- structure(10, class = c("MyNumeric", "numeric"))
setTyped(id = "x_1", value)
x_1
inherits(x_1, "MyNumeric")
inherits(x_1, "numeric")
x_1 <- as.integer(20)
x_1
## --> overwritten
setTyped(id = "x_1", value, numint = FALSE)
try(x_1 <- as.integer(10))
## --> ignored with error
## --> note the information that any of {`MyNumeric`, `numeric`} would be
## a valid type (class)
x_1
##------------------------------------------------------------------------------
## Return invisible //
##------------------------------------------------------------------------------
## Return once only //
(res <- setTyped(id = "x_1", 10, return_invis = 1))
## --> return value is `InvisibleObject`
x_1
## --> but `x_1` has value `10`
res$.value
ls(res, all.names = TRUE)
exists(".id", envir = res, inherits = FALSE)
exists(".uid", envir = res, inherits = FALSE)
exists(".class", envir = res, inherits = FALSE)
exists(".where", envir = res, inherits = FALSE)
exists(".validateType", envir = res, inherits = FALSE)
## Return always //
(res <- setTyped(id = "x_1", 10, return_invis = 2))
identical(x_1, res)
## --> invisible object is now also assigned to `x_1`
exists(".id", envir = x_1, inherits = FALSE)
exists(".uid", envir = x_1, inherits = FALSE)
exists(".class", envir = x_1, inherits = FALSE)
exists(".where", envir = x_1, inherits = FALSE)
exists(".validateType", envir = x_1, inherits = FALSE)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.