-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreactive_references.Rmd
More file actions
219 lines (167 loc) · 4.94 KB
/
reactive_references.Rmd
File metadata and controls
219 lines (167 loc) · 4.94 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
---
title: "Reactive References"
author: "Janko Thyson"
date: "Saturday, October 11, 2014"
output:
pdf_document
---
```{r, echo=FALSE}
suppressMessages(require("reactr"))
```
You currently have three different options in order to specify the function that references other reactive objects and that defines the actual reactive relationship to them.
## Option 1: via YAML markup (recommended)
The easiest and most compact way of making references recognizable is by specifying them in either in special YAML markup string or wrapping the YAML markup inside a comment:
```{r, eval=FALSE, cache=FALSE}
## Via string //
"object-ref: {id: {id}, where: {where}, as {as}]"
## Via comment //
## object-ref: {id} in {where} as {ref-id}]
```
### Explanation of markup components
- `{id}`: name/ID that the visible value of the referenced object has been assigned to
- `{where}` (optional): environment that the referenced object has been assigned to
Default: `parent.frame()`
- '{as}' (optional): alternative name/ID that is used in the remainder of the binding function
### Note
When using comments, the leading `##` are optional in the sense that the entire line must simply be a valid comment in R scripts (e.g., could also be `#` or `###` etc.)
### Examples of generic structure
Here are the possible markup constellations
```{r, eval=FALSE}
object-ref: {id: x_1}
object-ref: {id: x_1, where: where_1}
object-ref: {id: x_1, where: where_1, as: ref_1}
object-ref: {id: x_1, as: ref_1}
```
### Actual examples
```{r, eval=TRUE}
setReactive(id = "x_1", value = 10)
## With curly brackets //
setReactive(id = "x_2", value = function() {
"object-ref: {id: x_1}"
x_1 * 2
}
)
## W/o curly brackets //
setReactive(id = "x_3", value = function()
## object-ref: {id: x_1}
x_1 * 2
)
## With '{as}' //
setReactive(id = "x_4", value = function() {
"object-ref: {id: x_1, as: ref_1}"
ref_1 * 2
})
## Multiple //
setReactive(id = "x_5", value = function() {
"object-ref: {id: x_1, where: where,as: ref_1}"
"object-ref: {id: x_2, as: ref_2}"
ref_1 + ref_2
})
```
Note that the order **does not** matter!
```{r, eval=TRUE}
setReactive(id = "x_6", value = function() {
"object-ref: {id: x_1, as: ref_1, where: where}"
"object-ref: {id: x_2, as: ref_2}"
ref_1 + ref_2
})
```
Clean up
```{r, message=FALSE}
rmReactive("x_1")
rmReactive("x_2")
rmReactive("x_3")
rmReactive("x_4")
```
## Option 2: via argument `refs`
You can also specify the references by using the special argument `refs`:
```{r, eval=FALSE}
refs = list(ref_1 = list(id = {id}, where = {where})))
```
### Examples
```{r, eval=TRUE}
resetRegistry()
where_1 <- new.env()
setReactive(id = "x_1", value = 10, where = where_1)
## With curly brackets //
setReactive(id = "x_2", value = function(
refs = list(ref_1 = list(id = "x_1", where = where_1))
) {
x_1 * 2
})
## W/o curly brackets //
setReactive(id = "x_3", value = function(
refs = list(ref_1 = list(id = "x_1", where = where_1))
) x_1 * 2
)
## Without explicit 'where' //
setReactive(id = "x_4", value = function(
refs = list(ref_1 = list(id = "x_1")))
x_1 * 2
)
```
Clean up
```{r, message=FALSE}
rmReactive("x_1")
rmReactive("x_2")
rmReactive("x_3")
rmReactive("x_4")
```
## Option 3: via explicit code
You can also specify the references by using lines that start with
```{r, eval=FALSE}
.ref_*
```
followd by `<-` and a call to `get()` of the form:
```{r, eval=FALSE}
get({id}, {where})
```
### Examples of generic structure
```{r, eval=FALSE}
.ref_1 <- get(x = "x_1", envir = where_1)
.ref_2 <- get("x_1", where_1)
```
#### NOTE
1. The recognition mechanism relies on names/IDs starting with `ref_` to properly identify references
2. To be absolutely sure you retrieve the correct object, it is recommended to use `inherits = FALSE`
3. All environment objects that are used inside the binding functions should be passed along as additional arguments to either `setReactive()` or `setShinyReactive()`
### Actual examples
```{r, eval=TRUE}
setReactive(id = "x_1", value = 10)
## With curly brackets //
setReactive(id = "x_2", value = function() {
.ref_1 <- get(x = "x_1", inherits = FALSE)
.ref_1 * 2
})
## W/o curly brackets //
setReactive(id = "x_3", value = function()
.ref_1 <- get(x = "x_1", inherits = FALSE)
## For '* 2' you would need to use curly brackets
## and a new line
)
## W/o argument names //
setReactive(id = "x_4", value = function()
.ref_1 <- get("x_1", inherits = FALSE)
## For '* 2' you would need to use curly brackets
## and a new line
)
## Explicit environments //
where_1 <- new.env()
setReactive(id = "x_1", value = 10, where = where_1)
setReactive(id = "x_2", value = function() {
.ref_1 <- get(x = "x_1", where_1, inherits = FALSE)
.ref_1 * 2
}, where_1 = where_1)
where_1$x_1
## --> `x_1` is in `where_1`
x_2
## --> `x_2` is in .GlobalEnv but references `x_1` from `where_1`
```
Clean up
```{r, message=FALSE}
rmReactive("x_1")
rmReactive("x_2")
rmReactive("x_3")
rmReactive("x_4")
rm(where_1)
```