-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget_solutionSettings_server.R
319 lines (313 loc) · 9.32 KB
/
widget_solutionSettings_server.R
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#' @include internal.R class_SolutionSettings.R
NULL
#' Update solution settings widget
#'
#' Change the settings for solution settings widget on the client.
#' Specifically, change the settings for a theme or weight depicted
#' in a solution settings widget.
#'
#' @param session The `session` object passed to function given to
#' `shinyServer` Default is [shiny::getDefaultReactiveDomain()].
#'
#' @param inputId `character` The identifier of the input object.
#'
#' @param value `list` Object containing the new settings (see Details).
#'
#' @details
#' ## Overview
#'
#' The argument to `value` should be `list` object containing
#' the information necessary to update a theme or weight.
#' Broadly speaking, it should contain the following elements:
#'
#' \describe{
#' \item{id}{`character` value with the identifier for the theme or weight.}
#' \item{setting}{`character` value with the name of the setting to update.}
#' \item{value}{`ANY` new value for the setting.}
#' \item{type}{`character` value indicating if the setting is a `theme`
#' or `weight`.}
#' }
#'
#' Note that the `value` element in the `list` object should have a
#' class (i.e. `numeric`, `logical, or `character`) that is relevant
#' to the setting that should be updated. For example, if the
#' `setting` element is equal to `name`, then the `value` element
#' should contain a `character` value.
#' For reference, we provide examples detailing all the various settings that
#' can be updated below.
#'
#' ## Themes
#'
#' Here we detail all possible settings that can be updated for
#' themes.
#'
#' **Update the name.**
#' This controls the bold text shown in the header of the theme.
#'
#' \describe{
#' \item{id}{`"THEMEID"`}
#' \item{setting}{`"name"`}
#' \item{value}{`"SPECIES"`}
#' \item{type}{`"theme"`}
#' }
#'
#' **Update the status.**
#' This controls the large switch shown in the header of theme.
#' Note that the `value` element must be a `logical` (`TRUE` or `FALSE`) value.
#'
#' \describe{
#' \item{id}{`"THEMEID"`}
#' \item{setting}{`"status"`}
#' \item{value}{`TRUE`}
#' \item{type}{`"theme"`}
#' }
#'
#' **Update the view.**
#' This controls whether the "group" or "single" tab is active for themes with
#' multiple features.
#' It does not have any effect for themes with a single feature.
#' Note that the `value` element must be a `character` value equal to
#' `"group"` or `"single"`.
#'
#' \describe{
#' \item{id}{`"THEMEID"`}
#' \item{setting}{`"view"`}
#' \item{value}{`"group"`}
#' \item{type}{`"theme"`}
#' }
#'
#' **Update the group goal.**
#' This controls the slider present under the "group" tab.
#' It does not have any effect for themes with a single features.
#'
#' \describe{
#' \item{id}{`"THEMEID"`}
#' \item{setting}{`"group_goal"`}
#' \item{value}{`0.7`}
#' \item{type}{`"theme"`}
#' }
#'
#' **Update the feature status.**
#' This controls the switches present under the "single" tab.
#' To ensure compatibility between themes with a single feature
#' and themes with multiple features, this will update the status
#' for a theme with a single feature.
#' Note that the `value` element must have a value for each feature
#' within the theme (the example below assumes the theme has three features).
#'
#' \describe{
#' \item{id}{`"THEMEID"`}
#' \item{setting}{`"feature_status"`}
#' \item{value}{`c(TRUE, FALSE, TRUE)`}
#' \item{type}{`"theme"`}
#' }
#'
#' **Update the feature current values.**
#' This controls the bars indicating the amount currently held for each
#' feature under both the "single" and "group" tabs.
#' Note that the `value` element must have a value for each feature
#' within the theme (the example below assumes the theme has three features).
#'
#' \describe{
#' \item{id}{`"THEMEID"`}
#' \item{setting}{`"feature_current"`}
#' \item{value}{`c(0.3, 0.1, 0.7)`}
#' \item{type}{`"theme"`}
#' }
#'
#' **Update the feature goals.**
#' This controls the sliders present under the "single" tab.
#' To ensure compatibility between themes with a single feature
#' and themes with multiple features, this will update the status
#' for a theme with a single feature.
#' Note that the `value` element must have a value for each feature
#' within the theme (the example below assumes the theme has three features).
#'
#' \describe{
#' \item{id}{`"THEMEID"`}
#' \item{setting}{`"feature_goal"`}
#' \item{value}{`c(0.3, 0.1, 0.7)`}
#' \item{type}{`"theme"`}
#' }
#'
#'
#' ## Weights
#'
#' Here we detail all possible settings that can be updated for
#' weights.
#'
#' **Update the name.**
#' This controls the bold text shown in the header of the weight.
#'
#' \describe{
#' \item{id}{`"WEIGHTID"`}
#' \item{setting}{`"name"`}
#' \item{value}{`"SPECIES"`}
#' \item{type}{`"weight"`}
#' }
#'
#' **Update the status.**
#' This controls the large switch shown in the header of weight.
#' Note that the `value` element must be a `logical` (`TRUE` or `FALSE`) value.
#'
#' \describe{
#' \item{id}{`"WEIGHTID"`}
#' \item{setting}{`"status"`}
#' \item{value}{`TRUE`}
#' \item{type}{`"weight"`}
#' }
#'
#' **Update the factor.**
#' This controls the slider shown for the weight.
#'
#' \describe{
#' \item{id}{`"WEIGHTID"`}
#' \item{setting}{`"factor"`}
#' \item{value}{`0.1`}
#' \item{type}{`"weight"`}
#' }
#'
#' ## Includes
#' Here we detail all possible settings that can be updated for
#' includes.
#'
#' **Update the name.**
#' This controls the bold text shown in the header of the include.
#'
#' \describe{
#' \item{id}{`"INCLUDEID"`}
#' \item{setting}{`"name"`}
#' \item{value}{`"PA"`}
#' \item{type}{`"include"`}
#' }
#'
#' **Update the status.**
#' This controls the large switch shown in the header of include.
#' Note that the `value` element must be a `logical` (`TRUE` or `FALSE`) value.
#'
#' \describe{
#' \item{id}{`"INCLUDEID"`}
#' \item{setting}{`"status"`}
#' \item{value}{`TRUE`}
#' \item{type}{`"include"`}
#' }
#'
#' ## Excludes
#' Here we detail all possible settings that can be updated for
#' excludes.
#'
#' **Update the name.**
#' This controls the bold text shown in the header of the exclude.
#'
#' \describe{
#' \item{id}{`"EXCLUDEID"`}
#' \item{setting}{`"name"`}
#' \item{value}{`"XX"`}
#' \item{type}{`"exclude"`}
#' }
#'
#' **Update the status.**
#' This controls the large switch shown in the header of exclude.
#' Note that the `value` element must be a `logical` (`TRUE` or `FALSE`) value.
#'
#' \describe{
#' \item{id}{`"EXCLUDEID"`}
#' \item{setting}{`"status"`}
#' \item{value}{`TRUE`}
#' \item{type}{`"exclude"`}
#' }
#'
#' @seealso [solutionSettings()].
#'
#' @export
updateSolutionSettings <- function(session = shiny::getDefaultReactiveDomain(),
inputId, value) {
# assert valid arguments
assertthat::assert_that(
assertthat::is.string(inputId),
assertthat::noNA(inputId),
is.list(value)
)
assertthat::assert_that(
assertthat::has_name(value, "id"),
assertthat::is.string(value$id),
assertthat::noNA(value$id),
assertthat::has_name(value, "setting"),
assertthat::is.string(value$setting),
assertthat::noNA(value$setting),
assertthat::has_name(value, "value"),
assertthat::has_name(value, "type"),
assertthat::is.string(value$type),
assertthat::noNA(value$type)
)
# assert value contains valid settings
## define valid setting names and value classes
assertthat::assert_that(
value$type %in% c("weight", "theme", "include", "exclude", "parameter")
)
if (identical(value$type, "theme")) {
param_names <- c(
"name", "status", "view",
"group_goal", "feature_goal", "feature_status", "feature_current"
)
param_classes <- c(
"character", "logical", "character",
"numeric", "numeric", "logical", "numeric"
)
} else if (identical(value$type, "weight")) {
param_names <- c("name", "status", "factor")
param_classes <- c("character", "logical", "numeric")
} else if (identical(value$type, "include")) {
param_names <- c("name", "status", "overlap")
param_classes <- c("character", "logical", "character")
} else if (identical(value$type, "exclude")) {
param_names <- c("name", "status", "overlap")
param_classes <- c("character", "logical", "character")
} else if (identical(value$type, "parameter")) {
param_names <- c("name", "status", "value")
param_classes <- c("character", "logical", "numeric")
}
## sanity check
assertthat::assert_that(
length(param_names) == length(param_classes),
msg = "internal validation failed"
)
## coerce integer values to double values
if (is.integer(value$value)) {
value$value <- as.double(value$value)
}
## run checks
assertthat::assert_that(
value$setting %in% param_names,
msg = paste0(
value$type,
"s must have a `setting` equal to one of the following: ",
paste(paste0("\"", param_names, "\""), collapse = ", ")
)
)
assertthat::assert_that(
inherits(
value$value,
param_classes[[which(param_names == value$setting)]]
),
msg = paste0(
"the \"", value$setting,
"\" setting must have a ",
param_classes[[which(param_names == value$setting)]],
" `value`"
)
)
if (identical(value$setting, "view")) {
assertthat::assert_that(
value$value %in% c("single", "group"),
msg = paste0(
"the \"view\" setting must have a \"single\" ",
"or \"group\" `value`."
)
)
}
# pass data to widget
session$sendCustomMessage(
"solutionSettings:update", list(id = inputId, value = value)
)
}