-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaliases.R
More file actions
368 lines (319 loc) · 12.7 KB
/
aliases.R
File metadata and controls
368 lines (319 loc) · 12.7 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
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
#------------------------------------------------------------------------------#
# Author: Laurent R. Bergé
# Created: 2023-06-27
# ~: Alias generators
#------------------------------------------------------------------------------#
#' Create `string_magic` aliases with custom defaults
#'
#' Utility to easily create `string_magic` aliases with custom default
#'
#' @inheritParams string_magic
#'
#' @param .local_ops Named list or `NULL` (default). If provided, it must be a list
#' of the form `list(alias1 = ops1, alias2 = ops2)` where alias is the name of the newly defined
#' operator an `ops` is a character scalar representing the associated string_magic operations.
#' Ex: `list(add = "' + 'collapse")` creates the operation `add` which collapses the
#' string with pluses. All operations created here are only available to the
#' generated function.
#'
#' @details
#'
#' Use this function if you want to change `string_magic` default values. For example,
#' if you want the interpolation to be done with `"{{}}"` (instead of `{}`) or if you want the
#' default separation to be the space (instead of the empty string). See the example.
#'
#' @return
#' This function returns a function which will behave in the same way as [string_magic()]
#'
#'
#' @inheritSection string_magic_register_fun Writing a package using `string_magic`
#'
#' @author
#' Laurent Berge
#'
#' @family related to string_magic
#' @family tools with aliases
#'
#' @examples
#'
#' # we create the function sma2 with different defaults
#' sma2 = string_magic_alias(.delim = ".[ ]", .sep = " ", .class = "string_magic")
#'
#' person = "john doe"
#' sma2("Hello", ".[title ? person]")
#'
#' # you can use the arguments whose default has been changed
#' sma2("Hello", ".[title ? person]", .sep = ": ")
#'
#'
#'
string_magic_alias = function(.sep = "", .vectorize = FALSE,
.delim = c("{", "}"), .last = NULL,
.post = NULL, .default = NULL, .nest = FALSE,
.invisible = FALSE, .local_ops = NULL,
.collapse = NULL, .check = TRUE,
.class = NULL, .namespace = NULL){
# checks
check_character(.sep, scalar = TRUE)
check_logical(.vectorize, scalar = TRUE)
.delim = check_set_delimiters(.delim)
check_last(.last)
check_function(.post, null = TRUE)
check_character(.default, scalar = TRUE, null = TRUE)
check_logical(.nest, scalar = TRUE)
check_logical(.invisible, scalar = TRUE)
# .local_ops, see below
check_character(.collapse, scalar = TRUE, null = TRUE)
check_logical(.check, scalar = TRUE)
check_character(.class, no_na = TRUE, null = TRUE)
check_character(.namespace, scalar = TRUE, null = TRUE)
# .local_ops
if(!missnull(.local_ops)){
info = .sma("\nINFO: names of the list = alias. Content = string_magic operations.",
"\nEx: .local_ops = list(\"plus\" = \"' + 'collapse\")")
if(!is.list(.local_ops)){
stopi("The argument `.local_ops` must be a list. ",
"Currently it is of class {enum.bq?class(.local_ops)}.", info)
}
if(length(.local_ops) == 0){
stopi("The argument `.local_ops` must be a non-empty list. ",
"Currently it is empty.", info)
}
if(is.null(names(.local_ops))){
stopi("In the argument `.local_ops` require names.",
"\nPROBLEM: the list has no names.", info)
}
if(string_any(names(.local_ops), "[^[:lower:]]")){
stopi("In the argument `.local_ops`, the aliases must be composed of ",
"only lower case letters.",
"\nPROBLEM: this is not the case for ",
"{'[^[:lower:]]'get, enum.bq ? names(.local_ops)}.", info)
}
if(!all(sapply(.local_ops, is.character))){
i_pblm = which(!sapply(.local_ops, is.character))[1]
stopi("In the argument `.local_ops`, each element of the list must ",
"be a character scalar.",
"\nPROBLEM: the {nth?i_pblm} element is of class ",
"{enum.bq?class(.local_ops[i_pblm]}.", info)
}
if(any(lengths(.local_ops) != 1)){
i_pblm = which(lengths(.local_ops) != 1)[1]
stopi("In the argument `.local_ops`, each element of the list must ",
"be a character scalar.",
"\nPROBLEM: the {nth?i_pblm} element is of length {len?.local_ops[i_pblm]}.",
info)
}
if(is.null(.namespace)){
.namespace = tag_gen()
}
for(i in seq_along(.local_ops)){
alias = names(.local_ops)[[i]]
content = .local_ops[[i]]
string_magic_register_ops(content, alias, .namespace)
}
}
# forcing evaluations (INDISPENSABLE)
sep = .sep
vectorize = .vectorize
delim = .delim
last = .last
post = .post
default = .default
nest = .nest
invisible = .invisible
collapse = .collapse
check = .check
class = .class
namespace = .namespace
if(.nest && .vectorize){
stop("You cannot have `.nest` and `.vectorize` at the same time. One of the two must be set to `FALSE`.")
}
res = function(..., .envir = parent.frame(), .sep = sep, .vectorize = vectorize,
.delim = delim, .last = last, .post = post, .default = default,
.nest = nest, .invisible = invisible, .collapse = collapse,
.check = check, .class = class, .help = NULL,
.namespace = namespace){
string_magic(..., .envir = .envir, .sep = .sep, .vectorize = .vectorize,
.delim = .delim, .last = .last, .post = .post, .default = .default,
.nest = .nest, .invisible = .invisible, .collapse = .collapse,
.check = .check, .class = .class, .help = .help,
.namespace = .namespace)
}
res
}
#' @describeIn cat_magic Create an alias of `cat_magic` with custom defaults
cat_magic_alias = function(.sep = "", .end = "", .width = FALSE, .leader = "",
.delim = c("{", "}"), .last = NULL,
.trigger = TRUE, .check = TRUE,
.namespace = NULL){
# checks
check_character(.sep, scalar = TRUE)
check_character(.end, scalar = TRUE)
check_character(.leader, scalar = TRUE)
.delim = check_set_delimiters(.delim)
check_character(.last, scalar = TRUE, null = TRUE)
check_logical(.trigger, scalar = TRUE)
check_logical(.check, scalar = TRUE)
check_character(.namespace, scalar = TRUE, null = TRUE)
# width is special
is_call = isTRUE(try(is.call(.width), silent = TRUE))
if(!is_call){
.width = substitute(.width)
if(!".sw" %in% all.vars(.width)){
.width = eval(.width, parent.frame())
}
}
sep = .sep
end = .end
leader = .leader
last = .last
trigger = .trigger
check = .check
namespace = .namespace
width = .width
delim = .delim
res = function(..., .sep = sep, .end = end, .width = width, .leader = leader,
.envir = parent.frame(), delim = delim, .last = last,
.trigger = trigger, .check = check, .help = NULL,
.namespace = namespace){
cat_magic(..., .sep = .sep, .end = .end, .width = .width, .leader = .leader,
.envir = .envir, .delim = .delim, .last = .last,
.trigger = .trigger, .check = .check, .help = .help,
.namespace = .namespace)
}
res
}
#' @describeIn cat_magic Create an alias of `message_magic` with custom defaults
message_magic_alias = function(.sep = "", .end = "\n", .width = ~min(100, .sw), .leader = "",
.delim = c("{", "}"), .last = NULL, .trigger = TRUE,
.check = TRUE, .namespace = NULL){
# checks
check_character(.sep, scalar = TRUE)
check_character(.end, scalar = TRUE)
check_character(.leader, scalar = TRUE)
.delim = check_set_delimiters(.delim)
check_character(.last, scalar = TRUE, null = TRUE)
check_logical(.check, scalar = TRUE)
check_logical(.trigger, scalar = TRUE)
check_character(.namespace, scalar = TRUE, null = TRUE)
# width is special
is_call = isTRUE(try(is.call(.width), silent = TRUE))
if(!is_call){
.width = substitute(.width)
if(!".sw" %in% all.vars(.width)){
.width = eval(.width, parent.frame())
}
}
# forcing the eval
sep = .sep
end = .end
leader = .leader
last = .last
trigger = .trigger
check = .check
namespace = .namespace
width = .width
delim = .delim
res = function(..., .sep = sep, .end = end, .width = width, .leader = leader,
.envir = parent.frame(), .delim = delim, .last = last,
.trigger = trigger,
.check = check, .help = NULL,
.namespace = namespace){
message_magic(..., .sep = .sep, .end = .end, .width = .width, .leader = .leader,
.envir = .envir, .delim = .delim, .last = .last,
.trigger = .trigger, .check = .check, .help = .help,
.namespace = .namespace)
}
res
}
#' @describeIn string_ops `string_ops` alias with custom defaults
string_ops_alias = function(op = NULL, pre_unik = NULL, namespace = NULL){
#
check_character(op, scalar = TRUE, null = TRUE)
check_logical(pre_unik, null = TRUE, scalar = TRUE)
check_character(namespace, scalar = TRUE, null = TRUE)
# forcing evaluations
.op = op
.pre_unik = pre_unik
.namespace = namespace
res = function(x, op = .op, pre_unik = .pre_unik, namespace = .namespace){
string_ops(x, op = op, pre_unik = pre_unik, namespace = namespace)
}
res
}
#' @describeIn string_clean Create a `string_clean` alias with custom defaults
string_clean_alias = function(replacement = "", pipe = " => ", split = ",[ \n\t]+",
ignore.case = FALSE, fixed = FALSE, word = FALSE,
total = FALSE, single = FALSE,
namespace = NULL){
check_character(replacement, scalar = TRUE)
check_character(pipe, scalar = TRUE)
check_character(split, scalar = TRUE, null = TRUE)
check_logical(ignore.case, scalar = TRUE)
check_logical(fixed, scalar = TRUE)
check_logical(word, scalar = TRUE)
check_logical(total, scalar = TRUE)
check_logical(single, scalar = TRUE)
check_character(namespace, scalar = TRUE, null = TRUE)
# forcing the evaluations
.replacement = replacement
.pipe = pipe
.split = split
.ignore.case = ignore.case
.fixed = fixed
.word = word
.total = total
.single = single
.namespace = namespace
res = function(x, ..., replacement = .replacement, pipe = .pipe, split = .split,
ignore.case = .ignore.case, fixed = .fixed, word = .word,
total = .total, single = .single, envir = parent.frame(),
namespace = .namespace){
string_clean(x, ..., replacement = replacement, pipe = pipe, split = split,
ignore.case = ignore.case, fixed = fixed, word = word,
total = total, single = single, envir = envir,
namespace = namespace)
}
res
}
#' @describeIn string_vec Create `string_vec` aliases with custom defaults
string_vec_alias = function(.cmat = FALSE, .nmat = FALSE, .df = FALSE, .df.convert = TRUE,
.last = NULL, .delim = c("{", "}"), .split = TRUE,
.protect.vars = FALSE, .check = TRUE, .sep = NULL,
.collapse = NULL, .namespace = NULL){
.delim = check_set_delimiters(.delim)
check_character(.sep, scalar = TRUE, null = TRUE)
check_character(.collapse, scalar = TRUE, null = TRUE)
check_character(.last, scalar = TRUE, null = TRUE)
check_logical(.protect.vars, scalar = TRUE)
check_logical(.check, scalar = TRUE)
check_character(.namespace, scalar = TRUE, null = TRUE)
.split = check_set_split(.split)
check_set_mat(.cmat, .nmat, .df)
check_logical(.df.convert, scalar = TRUE)
# forcing evaluation
nmat = .nmat
cmat = .cmat
df = .df
df.convert = .df.convert
last = .last
delim = .delim
split = .split
protect.vars = .protect.vars
check = .check
sep = .sep
collapse = .collapse
namespace = .namespace
res = function(..., .cmat = cmat, .nmat = nmat, .df = df, .df.convert = df.convert,
.last = last, .delim = delim, .envir = parent.frame(),
.split = split, .protect.vars = protect.vars,
.check = check, .sep = sep,
.collapse = collapse, .namespace = namespace){
string_vec(..., .cmat = .cmat, .nmat = .nmat, .df = .df, .df.convert = .df.convert,
.last = last, .delim = .delim, .envir = .envir,
.split = .split, .protect.vars = .protect.vars,
.check = .check, .sep = .sep,
.collapse = .collapse, .namespace = .namespace)
}
res
}