setTyped: Set Typed Object Value (S3)

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

Description

Creates an implicitly typed object.

Usage

1
2
3
setTyped(id, value = NULL, where = parent.frame(), numint = TRUE,
  from_null = TRUE, to_null = TRUE, inherit = FALSE, return_invis = c(0,
  1, 2), strict = c(2, 1, 0), ...)

Arguments

id

character. Name/ID of the typed object to create.

value

ANY. Object value.

where

environment. Environment in which to create the object.

numint

logical. TRUE: do not distinguish between types (classes) numeric and integer; FALSE: threat them as two different types (classes). Default: TRUE as this seems to make most sense in practical applications.

from_null

logical. TRUE: any type (class) is valid to overwrite an initial NULL value. FALSE: this would be regarded as a type (class) mismatch. Default: TRUE as this seems to make most sense in practical applications.

to_null

logical. TRUE: value of NULL is always valid type (class) regardless of the type (class) that was stored at the initial assignment operation. FALSE: this would be regarded as a type (class) mismatch. Default: TRUE as this seems to make most sense in practical applications.

inherit

logical. TRUE: visible typed value inherits from informal S3 class TypedObject; FALSE: class table not altered. Default: FALSE to provide maximum amount of compatibality with S3 method dispatch.

return_invis

numeric. Controls the actual return value

  • 0: return visible object part only

  • 1: return invisible object as return value of setTyped

  • 2: return invisible object for each subsequent assignment or get operation

strict

numeric. Relevant if assigning an explicit value to an object with reactive dependency on other objects. reactive relationship.

  • 0: ignore without warning

  • 1: ignore with Warning

  • 2: stop with error

...

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.

Details

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.

Author(s)

Janko Thyson janko.thyson@rappster.de

References

http://github.com/Rappster/typr

See Also

makeActiveBinding

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
 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)

rappster/typr documentation built on May 27, 2019, 2:02 a.m.