/
choices_labeled.R
398 lines (370 loc) · 12.3 KB
/
choices_labeled.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#' Set "`<choice>:<label>`" type of names
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' This is often useful for [choices_selected()] as it marks up the drop-down boxes
#' for [shiny::selectInput()].
#'
#' @details
#' If either `choices` or `labels` are factors, they are coerced to character.
#' Duplicated elements from `choices` get removed.
#'
#' @param choices (`character` or `factor` or `numeric` or `logical`) vector.
#' @param labels (`character`) vector containing labels to be applied to `choices`.
#' If `NA` then "Label Missing" will be used.
#' @param subset (`character` or `factor` or `numeric` or `logical`) vector that
#' is a subset of `choices`.
#' This is useful if only a few variables need to be named.
#' If this argument is used, the returned vector will match its order.
#' @param types (`character`) vector containing the types of the columns to be used for applying the appropriate
#' icons to the [choices_selected] drop down box (e.g. "numeric").
#'
#' @return Named `character` vector.
#'
#' @examples
#' library(shiny)
#' library(teal.data)
#'
#' ADSL <- rADSL
#' ADTTE <- rADTTE
#'
#' choices1 <- choices_labeled(names(ADSL), col_labels(ADSL, fill = FALSE))
#' choices2 <- choices_labeled(ADTTE$PARAMCD, ADTTE$PARAM)
#'
#' # if only a subset of variables are needed, use subset argument
#' choices3 <- choices_labeled(
#' names(ADSL),
#' col_labels(ADSL, fill = FALSE),
#' subset = c("ARMCD", "ARM")
#' )
#'
#' ui <- fluidPage(
#' selectInput("c1",
#' label = "Choices from ADSL",
#' choices = choices1,
#' selected = choices1[1]
#' ),
#' selectInput("c2",
#' label = "Choices from ADTTE",
#' choices = choices2,
#' selected = choices2[1]
#' ),
#' selectInput("c3",
#' label = "Arm choices from ADSL",
#' choices = choices3,
#' selected = choices3[1]
#' )
#' )
#' server <- function(input, output) {}
#'
#' if (interactive()) {
#' shinyApp(ui, server)
#' }
#' @export
#'
choices_labeled <- function(choices, labels, subset = NULL, types = NULL) {
if (is.factor(choices)) {
choices <- as.character(choices)
}
checkmate::assert_atomic(choices, min.len = 1, any.missing = FALSE)
if (is.factor(labels)) {
labels <- as.character(labels)
}
checkmate::assert_character(labels[!is.na(labels)], any.missing = FALSE)
if (length(choices) != length(labels)) {
stop("length of choices must be the same as labels")
}
checkmate::assert_subset(subset, choices, empty.ok = TRUE)
checkmate::assert_character(types, len = length(choices), null.ok = TRUE)
if (!is.null(subset)) {
if (!all(subset %in% choices)) {
stop("all of subset variables must be in choices")
}
labels <- labels[choices %in% subset]
types <- types[choices %in% subset]
choices <- choices[choices %in% subset]
}
is_dupl <- duplicated(choices)
choices <- choices[!is_dupl]
labels <- labels[!is_dupl]
types <- types[!is_dupl]
labels[is.na(labels)] <- "Label Missing"
raw_labels <- labels
combined_labels <- if (length(choices) > 0) {
paste0(choices, ": ", labels)
} else {
character(0)
}
if (!is.null(subset)) {
ord <- match(subset, choices)
choices <- choices[ord]
raw_labels <- raw_labels[ord]
combined_labels <- combined_labels[ord]
types <- types[ord]
}
structure(
choices,
names = combined_labels,
raw_labels = raw_labels,
combined_labels = combined_labels,
class = c("choices_labeled", "character"),
types = types
)
}
#' Variable label extraction and custom selection from data
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' Wrapper on [choices_labeled] to label variables basing on existing labels in data.
#'
#' @rdname variable_choices
#'
#' @param data (`data.frame` or `character`)
#' If `data.frame`, then data to extract labels from.
#' If `character`, then name of the dataset to extract data from once available.
#' @param subset (`character` or `function`)
#' If `character`, then a vector of column names.
#' If `function`, then this function is used to determine the possible columns (e.g. all factor columns).
#' In this case, the function must take only single argument "data" and return a character vector.
#'
#' See examples for more details.
#' @param key (`character`) vector with names of the variables, which are part of the primary key
#' of the `data` argument.
#'
#' This is an optional argument, which allows to identify variables associated
#' with the primary key and display the appropriate icon for them in the
#' [teal.widgets::optionalSelectInput()] widget.
#' @param fill (`logical(1)`) if `TRUE`, the function will return variable names
#' for columns with non-existent labels; otherwise will return `NA` for them.
#'
#' @return Named `character` vector with additional attributes or `delayed_data` object.
#'
#' @examples
#' library(teal.data)
#'
#' ADRS <- rADRS
#' variable_choices(ADRS)
#' variable_choices(ADRS, subset = c("PARAM", "PARAMCD"))
#' variable_choices(ADRS, subset = c("", "PARAM", "PARAMCD"))
#' variable_choices(
#' ADRS,
#' subset = c("", "PARAM", "PARAMCD"),
#' key = default_cdisc_join_keys["ADRS", "ADRS"]
#' )
#'
#' # delayed version
#' variable_choices("ADRS", subset = c("USUBJID", "STUDYID"))
#'
#' # functional subset (with delayed data) - return only factor variables
#' variable_choices("ADRS", subset = function(data) {
#' idx <- vapply(data, is.factor, logical(1))
#' names(data)[idx]
#' })
#' @export
#'
variable_choices <- function(data, subset = NULL, fill = FALSE, key = NULL) {
checkmate::assert(
checkmate::check_character(subset, null.ok = TRUE, any.missing = FALSE),
checkmate::check_function(subset)
)
checkmate::assert_flag(fill)
checkmate::assert_character(key, null.ok = TRUE, any.missing = FALSE)
UseMethod("variable_choices")
}
#' @rdname variable_choices
#' @export
variable_choices.character <- function(data, subset = NULL, fill = FALSE, key = NULL) {
structure(list(data = data, subset = subset, key = key),
class = c("delayed_variable_choices", "delayed_data", "choices_labeled")
)
}
#' @rdname variable_choices
#' @export
variable_choices.data.frame <- function(data, subset = NULL, fill = TRUE, key = NULL) {
checkmate::assert(
checkmate::check_character(subset, null.ok = TRUE),
checkmate::check_function(subset, null.ok = TRUE)
)
if (is.function(subset)) {
subset <- resolve_delayed_expr(subset, ds = data, is_value_choices = FALSE)
}
checkmate::assert_subset(subset, c("", names(data)), empty.ok = TRUE)
if (length(subset) == 0) {
subset <- names(data)
}
key <- intersect(subset, key)
var_types <- vapply(data, function(x) class(x)[[1]], character(1))
if (length(key) != 0) {
var_types[key] <- "primary_key"
}
if (any(duplicated(subset))) {
warning(
"removed duplicated entries in subset:",
paste(unique(subset[duplicated(subset)]), collapse = ", ")
)
subset <- unique(subset)
}
if ("" %in% subset) {
choices_labeled(
choices = c("", names(data)),
labels = c("", unname(teal.data::col_labels(data, fill = fill))),
subset = subset,
types = c("", var_types)
)
} else {
choices_labeled(
choices = names(data),
labels = unname(teal.data::col_labels(data, fill = fill)),
subset = subset,
types = var_types
)
}
}
#' Value labeling and filtering based on variable relationship
#'
#' @description
#' `r lifecycle::badge("stable")`
#'
#' Wrapper on [choices_labeled] to label variable values basing on other variable values.
#'
#' @rdname value_choices
#'
#' @param data (`data.frame`, `character`)
#' If `data.frame`, then data to extract labels from.
#' If `character`, then name of the dataset to extract data from once available.
#' @param var_choices (`character` or `NULL`) vector with choices column names.
#' @param var_label (`character`) vector with labels column names.
#' @param subset (`character` or `function`)
#' If `character`, vector with values to subset.
#' If `function`, then this function is used to determine the possible columns (e.g. all factor columns).
#' In this case, the function must take only single argument "data" and return a character vector.
#'
#' See examples for more details.
#' @param sep (`character`) separator used in case of multiple column names.
#'
#' @return named character vector or `delayed_data` object.
#'
#' @examples
#' ADRS <- rADRS
#' value_choices(ADRS, "PARAMCD", "PARAM", subset = c("BESRSPI", "INVET"))
#' value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM"))
#' value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM"),
#' subset = c("BESRSPI - ARM A", "INVET - ARM A", "OVRINV - ARM A")
#' )
#' value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM"), sep = " --- ")
#'
#' # delayed version
#' value_choices("ADRS", c("PARAMCD", "ARMCD"), c("PARAM", "ARM"))
#'
#' # functional subset
#' value_choices(ADRS, "PARAMCD", "PARAM", subset = function(data) {
#' levels(data$PARAMCD)[1:2]
#' })
#' @export
#'
value_choices <- function(data,
var_choices,
var_label = NULL,
subset = NULL,
sep = " - ") {
checkmate::assert_character(var_choices, any.missing = FALSE)
checkmate::assert_character(var_label, len = length(var_choices), null.ok = TRUE, any.missing = FALSE)
checkmate::assert(
checkmate::check_vector(subset, null.ok = TRUE),
checkmate::check_function(subset)
)
checkmate::assert_string(sep)
UseMethod("value_choices")
}
#' @rdname value_choices
#' @export
value_choices.character <- function(data,
var_choices,
var_label = NULL,
subset = NULL,
sep = " - ") {
structure(
list(
data = data,
var_choices = var_choices,
var_label = var_label,
subset = subset,
sep = sep
),
class = c("delayed_value_choices", "delayed_data", "choices_labeled")
)
}
#' @rdname value_choices
#' @export
value_choices.data.frame <- function(data,
var_choices,
var_label = NULL,
subset = NULL,
sep = " - ") {
checkmate::assert_subset(var_choices, names(data))
checkmate::assert_subset(var_label, names(data), empty.ok = TRUE)
df_choices <- data[var_choices]
df_label <- data[var_label]
for (i in seq_along(var_choices)) {
if ("NA" %in% c(df_choices[[i]], levels(df_choices[[i]])) && any(is.na(df_choices[[i]]))) {
warning(paste0(
"Missing values and the string value of 'NA' both exist in the column of ", var_choices[i],
" either as value(s) or level(s). ",
"This will cause the missing values to be grouped with the actual string 'NA' values in the UI widget."
))
}
}
choices <- if (
length(var_choices) > 1 ||
is.character(df_choices[[1]]) ||
is.factor(df_choices[[1]]) ||
inherits(df_choices[[1]], c("Date", "POSIXct", "POSIXlt", "POSIXt"))
) {
df_choices <- dplyr::mutate_if(
df_choices,
.predicate = function(col) inherits(col, c("POSIXct", "POSIXlt", "POSIXt")),
.funs = function(col) {
if (is.null(attr(col, "tzone")) || all(attr(col, "tzone") == "")) {
format(trunc(col), "%Y-%m-%d %H:%M:%S")
} else {
format(trunc(col), "%Y-%m-%d %H:%M:%S %Z")
}
}
)
apply(df_choices, 1, paste, collapse = sep)
} else {
df_choices[[var_choices]]
}
labels <- apply(df_label, 1, paste, collapse = sep)
df <- unique(data.frame(choices, labels, stringsAsFactors = FALSE)) # unique combo of choices x labels
if (is.function(subset)) {
subset <- resolve_delayed_expr(subset, ds = data, is_value_choices = TRUE)
}
res <- choices_labeled(
choices = df$choices,
labels = df$labels,
subset = subset
)
attr(res, "sep") <- sep
attr(res, "var_choices") <- var_choices
attr(res, "var_label") <- var_label
res
}
#' @describeIn choices_labeled Print choices_labeled object
#'
#' @param x an object used to select a method.
#' @param ... further arguments passed to or from other methods.
#'
#' @export
#'
print.choices_labeled <- function(x, ...) {
cat(
sprintf("number of choices: %s \n", length(x)),
names(x),
"",
sep = "\n"
)
invisible(x)
}