forked from tidyverse/purrr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.R
309 lines (260 loc) · 7.19 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
304
305
306
307
308
309
#' Pipe operator
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
maybe_as_data_frame <- function(out, x) {
if (is.data.frame(x)) {
check_tibble()
tibble::as_tibble(out)
} else {
out
}
}
check_tibble <- function() {
if (!is_installed("tibble")) {
abort("The tibble package must be installed")
}
}
check_tidyselect <- function(){
if (!is_installed("tidyselect")) {
abort("Using tidyselect in `map_at()` requires tidyselect")
}
}
at_selection <- function(nm, .at){
if (is_quosures(.at)){
check_tidyselect()
.at <- tidyselect::vars_select(.vars = nm, !!!.at)
}
.at
}
recycle_args <- function(args) {
lengths <- map_int(args, length)
n <- max(lengths)
stopifnot(all(lengths == 1L | lengths == n))
to_recycle <- lengths == 1L
args[to_recycle] <- lapply(args[to_recycle], function(x) rep.int(x, n))
args
}
names2 <- function(x) {
names(x) %||% rep("", length(x))
}
#' Infix attribute accessor
#'
#' @description
#'
#' \Sexpr[results=rd, stage=render]{purrr:::lifecycle("soft-deprecated")}
#'
#' Please use the `%@%` operator exported in rlang. It has an
#' interface more consistent with `@`: uses NSE, supports S4 fields,
#' and has an assignment variant.
#'
#' @param x Object
#' @param name Attribute name
#' @export
#' @name get-attr
#' @keywords internal
#' @examples
#' factor(1:3) %@% "levels"
#' mtcars %@% "class"
`%@%` <- function(x, name) {
signal_soft_deprecated(paste_line(
"`%@%` is soft-deprecated as of purrr 0.3.0.",
"Please use the operator provided in rlang instead."
))
attr(x, name, exact = TRUE)
}
#' Generate random sample from a Bernoulli distribution
#'
#' @param n Number of samples
#' @param p Probability of getting `TRUE`
#' @return A logical vector
#' @export
#' @examples
#' rbernoulli(10)
#' rbernoulli(100, 0.1)
rbernoulli <- function(n, p = 0.5) {
stats::runif(n) > (1 - p)
}
#' Generate random sample from a discrete uniform distribution
#'
#' @param n Number of samples to draw.
#' @param a,b Range of the distribution (inclusive).
#' @export
#' @examples
#' table(rdunif(1e3, 10))
#' table(rdunif(1e3, 10, -5))
rdunif <- function(n, b, a = 1) {
stopifnot(is.numeric(a), length(a) == 1)
stopifnot(is.numeric(b), length(b) == 1)
a1 <- min(a, b)
b1 <- max(a, b)
sample(b1 - a1 + 1, n, replace = TRUE) + a1 - 1
}
# magrittr placeholder
globalVariables(".")
has_names <- function(x) {
nms <- names(x)
if (is.null(nms)) {
rep_along(x, FALSE)
} else {
!(is.na(nms) | nms == "")
}
}
ndots <- function(...) nargs()
is_names <- function(nms) {
is_character(nms) && !any(is.na(nms) | nms == "")
}
paste_line <- function(...) {
paste(chr(...), collapse = "\n")
}
cat_line <- function(...) {
cat(paste0(paste_line(...), "\n"))
}
# From rlang
friendly_type_of <- function(x, length = FALSE) {
if (is.object(x)) {
return(sprintf("a `%s` object", paste_classes(x)))
}
friendly <- as_friendly_type(typeof(x))
if (length && is_vector(x)) {
friendly <- paste0(friendly, sprintf(" of length %s", length(x)))
}
friendly
}
as_friendly_type <- function(type) {
switch(type,
logical = "a logical vector",
integer = "an integer vector",
numeric = ,
double = "a double vector",
complex = "a complex vector",
character = "a character vector",
raw = "a raw vector",
string = "a string",
list = "a list",
NULL = "NULL",
environment = "an environment",
externalptr = "a pointer",
weakref = "a weak reference",
S4 = "an S4 object",
name = ,
symbol = "a symbol",
language = "a call",
pairlist = "a pairlist node",
expression = "an expression vector",
quosure = "a quosure",
formula = "a formula",
char = "an internal string",
promise = "an internal promise",
... = "an internal dots object",
any = "an internal `any` object",
bytecode = "an internal bytecode object",
primitive = ,
builtin = ,
special = "a primitive function",
closure = "a function",
type
)
}
paste_classes <- function(x) {
paste(class(x), collapse = "/")
}
is_bool <- function(x) {
is_logical(x, n = 1) && !is.na(x)
}
is_number <- function(x) {
is_integerish(x, n = 1, finite = TRUE)
}
is_quantity <- function(x) {
typeof(x) %in% c("integer", "double") && length(x) == 1 && !is.na(x)
}
friendly_type_of_element <- function(x) {
if (is.object(x)) {
classes <- paste0("`", paste_classes(x), "`")
if (single) {
friendly <- sprintf("a single %s element", classes)
} else {
friendly <- sprintf("a %s element", classes)
}
return(friendly)
}
switch(typeof(x),
logical = "a single logical",
integer = "a single integer",
double = "a single double",
complex = "a single complex number",
character = "a single string",
raw = "a single raw value",
list = "a list of one element",
abort("Expected a base vector type")
)
}
has_crayon <- function() is_installed("crayon") && crayon::has_color()
red <- function(x) if (has_crayon()) crayon::red(x) else x
blue <- function(x) if (has_crayon()) crayon::blue(x) else x
green <- function(x) if (has_crayon()) crayon::green(x) else x
yellow <- function(x) if (has_crayon()) crayon::yellow(x) else x
magenta <- function(x) if (has_crayon()) crayon::magenta(x) else x
cyan <- function(x) if (has_crayon()) crayon::cyan(x) else x
blurred <- function(x) if (has_crayon()) crayon::blurred(x) else x
silver <- function(x) if (has_crayon()) crayon::silver(x) else x
bold <- function(x) if (has_crayon()) crayon::bold(x) else x
italic <- function(x) if (has_crayon()) crayon::italic(x) else x
underline <- function(x) if (has_crayon()) crayon::underline(x) else x
bullet <- function(...) paste0(bold(silver(" * ")), sprintf(...))
quo_invert <- function(call) {
if (!is_call(call)) {
abort("Internal error: Expected call in `quo_invert()`")
}
call <- duplicate(call, shallow = TRUE)
if (is_quosure(call)) {
rest <- quo_get_expr(call)
} else {
rest <- call
}
first_quo <- NULL
# Find first quosured argument. We unwrap constant quosures which
# add no scoping information.
while (!is_null(rest)) {
elt <- node_car(rest)
if (is_quosure(elt)) {
if (quo_is_constant(elt)) {
# Unwrap constant quosures
node_poke_car(rest, quo_get_expr(elt))
} else if (is_null(first_quo)) {
# Record first quosured argument
first_quo <- elt
first_node <- rest
}
}
rest <- node_cdr(rest)
}
if (is_null(first_quo)) {
return(call)
}
# Take the wrapping quosure env as reference if there is one.
# Otherwise, take the first quosure detected in arguments.
if (is_quosure(call)) {
env <- quo_get_env(call)
call <- quo_get_expr(call)
} else {
env <- quo_get_env(first_quo)
}
rest <- first_node
while (!is_null(rest)) {
cur <- node_car(rest)
if (is_quosure(cur) && is_reference(quo_get_env(cur), env)) {
node_poke_car(rest, quo_get_expr(cur))
}
rest <- node_cdr(rest)
}
new_quosure(call, env)
}
quo_is_constant <- function(quo) {
is_reference(quo_get_env(quo), empty_env())
}