-
Notifications
You must be signed in to change notification settings - Fork 54
/
utils.R
303 lines (276 loc) · 6.41 KB
/
utils.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
#' @importFrom magrittr %>%
#' @export
magrittr::`%>%`
#' @importFrom rlang is_na
#' @export
rlang::is_na
#' @importFrom rlang are_na
#' @export
rlang::are_na
#' @importFrom visdat vis_miss
#' @export
visdat::vis_miss
#' Group By Helper
#'
#' This is a wrapper to facilitate the `grouped_df` S3 method.
#'
#' @param data data.frame, which will be grouped
#' @param .fun a function to apply
#' @param ... additional arguments to be passed to map
#'
#' @return a dataframe with the function applied to each group
#' @keywords internal
#' @noRd
#' @examples
#' \dontrun{
#' miss_case_table.grouped_df <- function(data) {
#' group_by_fun(data, .fun = miss_case_table)
#' }
#' airquality %>%
#' group_by(Month) %>%
#' miss_case_table()
#' }
#'
group_by_fun <- function(data, .fun, ...) {
tidyr::nest(data) %>%
dplyr::mutate(data = purrr::map(data, .fun, ...)) %>%
tidyr::unnest(cols = c(data))
}
#' Test if the input is NULL
#'
#' @param x object
#'
#' @return an error if input (x) is NULL
#'
#' @examples
#' \dontrun{
#' # success
#' test_if_null(airquality)
#' # fail
#' my_test <- NULL
#' test_if_null(my_test)
#' }
#' @keywords internal
#' @noRd
test_if_null <- function(x,
call = rlang::caller_env()) {
# test for null
if (is.null(x)) {
cli::cli_abort(
message = c(
"Input must not be NULL",
"Input is {.cls {class(x)}}"
),
call = call
)
}
}
#' Test if the input is Missing
#'
#' @param x object
#'
#' @return an error if input (x) is not specified
#'
#' @examples
#' \dontrun{
#' # success
#' my_test <- x
#' test_if_null(my_test)
#' # fail
#' test_if_missing()
#' }
#' @keywords internal
#' @noRd
test_if_missing <- function(x, msg = NULL) {
# test for null
if (missing(x)) {
cli::cli_abort(
c(
"argument must be specified",
"{msg}"
)
)
}
}
#' @keywords internal
#' @noRd
test_if_dots_missing <- function(dots_empty,
msg = NULL,
call = rlang::caller_env()) {
# test for null
if (dots_empty) {
cli::cli_abort(
c(
"argument must be specified",
"{msg}"
),
call = call
)
}
}
#' Test if input is a data.frame
#'
#' @param x object
#'
#' @return an error if input (x) is a data.frame
#'
#' @examples
#' \dontrun{
#' # success
#' test_if_dataframe(airquality)
#' # fail
#' my_test <- matrix(10)
#' test_if_dataframe(my_test)
#' }
#'
#' @keywords internal
#' @noRd
test_if_dataframe <- function(x,
arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
# test for dataframe
if (!inherits(x, "data.frame")) {
cli::cli_abort(
message = c(
"Input must inherit from {.cls data.frame}",
"We see class: {.cls {class(x)}}"
),
call = call
)
}
}
test_if_any_shade <- function(x,
call = rlang::caller_env()) {
# test for dataframe
test_if_dataframe(x)
if (!any_shade(x)) {
cli::cli_abort(
message = c(
"Input must contain a shade column.",
"See {.code ?shade}, {.code ?shade}, and {.code ?bind_shadow}"
),
call = call
)
}
}
#' Helper function to determine whether there are any missings
#'
#' @param x a vector
#'
#' @return logical vector TRUE = missing FALSE = complete
#'
any_row_miss <- function(x) {
apply(data.frame(x), MARGIN = 1, FUN = function(x) anyNA(x))
}
#' Add a counter variable for a span of dataframe
#'
#' Adds a variable, `span_counter` to a dataframe. Used internally to facilitate
#' counting of missing values over a given span.
#'
#' @param data data.frame
#' @param span_size integer
#'
#' @return data.frame with extra variable "span_counter".
#'
#' @examples
#' \dontrun{
#' # add_span_counter(pedestrian, span_size = 100)
#' }
add_span_counter <- function(data, span_size) {
dplyr::mutate(data,
span_counter = rep(
x = 1:ceiling(nrow(data)),
each = span_size,
length.out = nrow(data)
)
)
}
#' check the levels of many things
#'
#' this function is used internally to check what the levels are of the dataframe.
#'
#' @param x data.frame, usually
#'
#' @return a list containing the levels of everything
#' @keywords internal
#' @noRd
what_levels <- function(x) purrr::map(x, levels)
quo_to_shade <- function(...) {
# Use ensyms() rather than quos() because the latter allows
# arbitrary expressions. These variables are forwarded to select(),
# so potential expressions are `starts_with()`, `one_of()`, etc.
# The naniar code generally assumes that only symbols are passed in
# dots. `ensyms()` is a way of ensuring the input types.
vars <- rlang::ensyms(...)
# Adding `_NA` suffix to user symbols
shadow_chr <- purrr::map(vars, as_string) %>% paste0("_NA")
# Casting back to symbols
shadow_vars <- rlang::syms(shadow_chr)
return(shadow_vars)
}
class_glue <- function(x) {
class(x) %>% glue::glue_collapse(sep = ", ", last = ", or ")
}
diag_na <- function(size = 5) {
dna <- diag(
x = NA,
nrow = size,
ncol = size
)
suppressMessages(
tibble::as_tibble(dna,
.name_repair = "unique"
)
) %>%
set_names(paste0("x", seq_len(ncol(.))))
}
coerce_fct_na_explicit <- function(x) {
if (is.factor(x) & anyNA(x)) {
forcats::fct_na_value_to_level(x, level = "NA")
} else {
x
}
}
# any_shade <- function(x) any(grepl("^NA|^NA_", x))
any_row_shade <- function(x) {
apply(data.frame(x), MARGIN = 1, FUN = function(x) any(grepl("^NA|^NA_", x)))
}
vecIsFALSE <- Vectorize(isFALSE)
are_any_false <- function(x, ...) any(vecIsFALSE(x), ...)
check_btn_0_1 <- function(prop,
call = rlang::caller_env()) {
if (prop < 0 || prop > 1) {
cli::cli_abort(
message = c(
"{.var prop} must be between 0 and 1",
"{.var prop} is {prop}"
),
call = call
)
}
}
check_is_integer <- function(x,
call = rlang::caller_env()) {
if (x < 0) {
cli::cli_abort(
message = c(
"{.var x} must be greater than 0",
"{.var x} is {.val {x}}"
),
call = call
)
}
vctrs::vec_cast(x, integer())
}
check_is_scalar <- function(x,
call = rlang::caller_env()) {
if (length(x) != 1) {
cli::cli_abort(
message = c(
"{.var x} must be length 1",
"{.var x} is {x}, and {.var x} has length: {length(x)}"
),
call = call
)
}
}