-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils-sfmisc.R
812 lines (554 loc) · 13.9 KB
/
utils-sfmisc.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# sfmisc utils 1.0.0.9034
# utils -------------------------------------------------------------------
# nocov start
# commonly used utility functions included from the package sfmisc
#' Paste and Truncate
#'
#' @param x a vector
#' @param width (maximum) width of result
#' @param dots `character` scalar. String to use for ellipses
#' @inheritParams paste
#'
#' @return a `character` scalar
#' @noRd
#'
#' @examples
#' ptrunc(month.abb)
#' ptrunc(month.abb, month.name)
#'
ptrunc <- function(
...,
width = 40L,
sep = ", ",
collapse = ", ",
dots = " ..."
){
assert(width > 7L, "The minimum supported width is 8")
x <- paste(..., sep = sep, collapse = collapse)
sel <- vapply(x, nchar, integer(1), USE.NAMES = FALSE) > width
x[sel] <- strtrim(x[sel], width = width - 4L)
x[sel] <- paste0(gsub(",{0,1}\\s*$", "", x[sel]), dots)
x
}
fmt_class <- function(x, open = "<", close = ">"){
paste0(open, paste(x, collapse = "/"), close)
}
#' @param x any \R object
#' @param ignore subclasses to ignore
#' @noRd
class_fmt <- function(x, ignore = NULL){
fmt_class(setdiff(class(x), ignore))
}
compact <- function(x){
x[!vapply(x, is.null, FALSE)]
}
walk <- function(.x, .f, ...){
for (i in seq_along(.x)){
.f(.x[[i]], ...)
}
invisible(.x)
}
# assertions --------------------------------------------------------------
#' Assert a condition
#'
#' A simpler and more efficient for [base::stopifnot()] that has an easy
#' mechanism for supplying custom error messages. As opposed to `stopifnot()`,
#' `assert()` only works with a single (scalar) assertions.
#'
#' @param cond `TRUE` or `FALSE` (without any attributes). `FALSE` will throw
#' an exception with an automatically constructed error message (if `...`
#' was not supplied). Anything else will throw an exception stating that
#' `cond` was not valid.
#' @param ... passed on to [stop()]
#' @param call. passed on to [stop()]
#' @param domain passed on to [stop()]
#'
#' @noRd
#'
#' @return TRUE on success
#'
#' @examples
#'
#' \dontrun{
#' assert(1 == 1)
#' assert(1 == 2)
#' }
#'
#'
assert <- function(
cond,
...,
call. = FALSE,
domain = NULL
){
if (identical(cond, TRUE)){
return(TRUE)
} else if (identical(cond, FALSE)){
if (identical(length(list(...)), 0L)){
msg <- paste0("`", deparse(match.call()[[2]]), "`", " is not 'TRUE'")
stop(msg, call. = call., domain = domain)
} else {
suppressWarnings( stop(..., call. = call., domain = domain) )
}
} else {
stop("Assertion must be either 'TRUE' or 'FALSE'")
}
}
assert_namespace <- function(...){
pkgs <- c(...)
res <- vapply(pkgs, requireNamespace, logical(1), quietly = TRUE)
if (all(res)){
return(invisible(TRUE))
} else {
miss <- pkgs[!res]
if (identical(length(miss), 1L)){
msg <- sprintf(paste(
"This function requires the package '%s'. You can install it with",
'`install.packages("%s")`.'), miss, miss
)
} else {
msg <- sprintf(
paste(
"This function requires the packages %s. You can install them with",
"`install.packages(%s)`."
),
paste(miss, collapse = ", "),
paste0("c(", paste(paste0('\"', miss, '\"'), collapse = ", "), ")")
)
}
}
stop(msg, call. = FALSE)
}
# predicates --------------------------------------------------------------
is_error <- function(x){
inherits(x, "error")
}
is_try_error <- function(x){
inherits(x, "try-error")
}
is_scalar <- function(x){
identical(length(x), 1L)
}
is_POSIXct <- function(x){
inherits(x, "POSIXct")
}
is_scalar_POSIXct <- function(x){
is_POSIXct(x) && is_scalar(x)
}
is_POSIXlt <- function(x){
inherits(x, "POSIXlt")
}
is_scalar_POSIXlt <- function(x){
is_POSIXlt(x) && is_scalar(x)
}
is_POSIXt <- function(x){
inherits(x, "POSIXt")
}
is_scalar_POSIXt <- function(x){
is_POSIXt(x) && is_scalar(x)
}
is_Date <- function(x){
inherits(x, "Date")
}
is_scalar_Date <- function(x){
is_Date(x) && is_scalar(x)
}
is_scalar_list <- function(x){
is_list(x) && is_scalar(x)
}
is_scalar_atomic <- function(x){
is.atomic(x) && is_scalar(x)
}
is_scalar_logical <- function(x){
is.logical(x) && is_scalar(x)
}
is_scalar_integer <- function(x){
is.integer(x) && is_scalar(x)
}
is_scalar_factor <- function(x){
is.factor(x) && is_scalar(x)
}
is_scalar_list <- function(x){
is.list(x) && is_scalar(x)
}
is_scalar_numeric <- function(x){
is.numeric(x) && is_scalar(x)
}
is_scalar_character <- function(x){
is.character(x) && is_scalar(x)
}
is_vector <- function(x){
is.atomic(x) || is.list(x)
}
is_bool <- function(x){
is.logical(x) && !anyNA(x)
}
#' Check if Object is a Boolean
#'
#' Check wheter an object is either `TRUE` or `FALSE`.
#'
#' @param x Any \R Object.
#' @return either `TRUE` or `FALSE`
#' @noRd
#'
is_scalar_bool <- function(x){
identical(x, TRUE) || identical(x, FALSE)
}
#' Check if Object is Integer-like
#'
#' Check wheter an object is either `TRUE` or `FALSE`.
#'
#' @param x Any \R Object.
#' @return either `TRUE` or `FALSE`
#' @noRd
#'
is_integerish <- function(x){
if (!is.numeric(x)){
FALSE
} else {
all(as.integer(x) == x)
}
}
is_scalar_integerish <- function(x){
is_scalar(x) && is_integerish(x)
}
is_n <- function(x){
is_scalar_integerish(x) && identical(x > 0, TRUE)
}
is_n0 <- function(x){
is_scalar_integerish(x) && identical(x >= 0, TRUE)
}
#' Check if Objects have the same length
#'
#' @param ... Any number of \R Objects.
#'
#' @return either `TRUE` or `FALSE`
#' @noRd
is_equal_length <- function(...){
lengths <- vapply(list(...), length, 1L)
identical(length(unique(lengths)), 1L)
}
#' Check if Object has length 0
#'
#' Check wheter an object is either `TRUE` or `FALSE`.
#'
#' @param x Any \R Object.
#' @return either `TRUE` or `FALSE`
#' @noRd
#'
is_empty <- function(x){
identical(length(x), 0L)
}
#' Check if a String is Blank
#'
#' Check wheter a character vector contains only of spaces
#'
#' @param x Any \R Object.
#' @return either `TRUE` or `FALSE`
#' @noRd
#'
is_blank <- function(x){
trimws(x) == ""
}
#' Test if a Vector or Combination of Vectors is a Candidate Key
#'
#' Checks if all elements of the atomic vector `x`, or the combination of
#' all elements of `x` if `x` is a `list`, are unique and neither `NA` or
#' `infinite`.
#'
#' @param x a atomic vector or a list of atomic vectors
#'
#' @return `TRUE/FALSE`
#' @noRd
#'
#' @examples
#'
#' is_candidate_key(c(1, 2, 3))
#' is_candidate_key(c(1, 2, NA))
#' is_candidate_key(c(1, 2, Inf))
#'
#' td <- data.frame(
#' x = 1:10,
#' y = 1:2,
#' z = 1:5
#' )
#'
#' is_candidate_key(list(td$x, td$z))
#' # a data.frame is just a special list
#' is_candidate_key(td[, c("y", "z")])
is_candidate_key <- function(x){
if (is.atomic(x)){
# !is.infinite instead of is.finite because x can be a character vector
length(x) > 1 &&
all(!is.infinite(x)) &&
!any(is.na(x)) &&
identical(length(unique(x)), length(x))
} else if (is.list(x)){
length(x) > 0 &&
length(x[[1]] > 0) &&
do.call(is_equal_length, x) &&
all(vapply(x, function(.x) all(!is.infinite(.x)), logical(1))) &&
all(vapply(x, function(.x) !any(is.na(.x)), logical(1))) &&
!any(duplicated(as.data.frame(x)))
}
}
# https://modern-sql.com/feature/is-distinct-from
is_not_distinct_from <- function(x, y){
((x == y) & !is.na(x) & !is.na(y)) | (is.na(x) & is.na(y))
}
is_distinct_from <- function(x, y){
((x != y) & !is.na(x) & !is.na(y)) | (is.na(x) != is.na(y))
}
is_windows_path <- function(x){
nchar(x) >= 2 & grepl("^[A-Za-z].*", x) & substr(x, 2, 2) == ":"
}
# equalish ----------------------------------------------------------------
#' Check for equality within a tolerance level
#'
#'
#'
#' @param x,y `numeric` vectors
#' @param tolerance `numeric` scalar. tolerance level (absolute value). Defaults
#' to `.Machine$double.eps^0.5` which is a sensible default for comparing
#' floating point numbers.
#'
#' @return `equalish()` returns TRUE if the absolute difference between `x` and
#' `y` is less than `tolerance`.
#' @noRd
#' @seealso [.Machine]
#'
#'
#' @examples
#' a <- 0.7
#' b <- 0.2
#' a - b == 0.5
#' equalish(a - b, 0.5)
#'
equalish <- function(x, y, tolerance = .Machine$double.eps ^ 0.5){
assert(is_scalar_numeric(tolerance) && tolerance >= 0)
abs(x - y) < tolerance
}
#' @return `equalish_frac()` returns `TRUE` if the relative difference between
#' `x` and `y` is smaller than `tolerance`. The relative difference is
#' defined as `abs(x - y) / pmax(abs(x), abs(y))`. If both `x` and `y` are
#' `0` the relative difference is not defined, but this function will still
#' return `TRUE`.
#'
#' @noRd
#' @examples
#'
#' equalish_frac(1000, 1010, tolerance = 0.01)
#' equalish_frac(1000, 1010, tolerance = 0.009)
#' equalish_frac(0, 0)
#'
equalish_frac <- function(x, y, tolerance = .Machine$double.eps ^ 0.5){
assert(is_scalar_numeric(tolerance) && tolerance >= 0)
res <- abs(x - y) / pmax(abs(x), abs(y)) < tolerance
res[x == 0 & y == 0] <- TRUE
res
}
# all_are -----------------------------------------------------------------
#' Convert vector if identical elements to scalar
#'
#' Returns `unique(x)` if all elements of `x` are identical, throws an error if
#' not.
#'
#' @inheritParams all_are_identical
#'
#' @return A scalar of the same type as `x`
#' @noRd
as_scalar <- function(x){
res <- unique(x)
if (is_scalar(res)){
return(res)
} else {
stop("Not all elements of x are identical")
}
}
#' Test if all elements of a vector are identical
#'
#' @param x any object that can be handled by [unique()] (usually a vector or
#' list)
#' @param empty_value Value to return if function is called on a vector of
#' length 0 (e.g. `NULL`, `numeric()`, ...)
#'
#' @noRd
#' @family special equality checks
#' @return `TRUE/FALSE`
#'
#' @examples
#'
#' all_are_identical(c(1,2,3))
#' all_are_identical(c(1,1,1))
#'
all_are_identical <- function(x, empty_value = FALSE) {
assert(length(empty_value) <= 1)
if (length(x) > 0L) {
return(identical(length(unique(x)), 1L))
} else {
if (is.null(x)){
warning("'x' is NULL")
} else {
warning("'x' is an empty vector")
}
return(empty_value)
}
}
#' Test if all elements of a vector are unique
#'
#' @inheritParams all_are_identical
#'
#' @return TRUE/FALSE
#'
#' @noRd
#' @family special equality checks
#'
#' @examples
#'
#' all_are_identical(c(1,2,3))
#' all_are_identical(c(1,1,1))
#'
all_are_distinct <- function(
x,
empty_value = FALSE
){
assert(length(empty_value) <= 1)
if (identical(length(x), 1L)) {
return(TRUE)
} else if (length(x) > 1L) {
return(identical(length(unique(x)), length(x)))
} else {
if (is.null(x)){
warning("'x' is NULL")
} else {
warning("'x' is an empty vector")
}
return(empty_value)
}
}
n_distinct <- function(x){
length(unique(x))
}
# misc --------------------------------------------------------------------
pad_left <- function(
x,
width = max(nchar(paste(x))),
pad = " "
){
diff <- pmax(width - nchar(paste(x)), 0L)
padding <-
vapply(diff, function(i) paste(rep.int(pad, i), collapse = ""), character(1))
paste0(padding, x)
}
pad_right <- function(
x,
width = max(nchar(paste(x))),
pad = " "
){
diff <- pmax(width - nchar(paste(x)), 0L)
padding <-
vapply(diff, function(i) paste(rep.int(pad, i), collapse = ""), character(1))
paste0(x, padding)
}
`%||%` <- function(x, y){
if (is.null(x))
y
else (x)
}
preview_object <- function(
x,
width = 32,
brackets = c("(", ")"),
quotes = c("`", "`"),
dots = ".."
){
if (is.function(x)){
fmls <- names(formals(x))
len_fmls <- length(fmls)
if (len_fmls > 4){
fmls <- fmls[1:4]
fmls_fmt <- paste(fmls, collapse = ", ")
fmls_fmt <- paste0(fmls_fmt, ", +", len_fmls - length(fmls), "")
} else {
fmls_fmt <- paste(fmls, collapse = ", ")
}
return(fmt_class(paste(
fmt_class(class(x), open = "", close = ""), "(", fmls_fmt, ")",
sep = ""
)))
}
if (!is.atomic(x))
return(class_fmt(x))
if (is.numeric(x))
x <- format(x, justify = "none", drop0trailing = TRUE, trim = TRUE)
res <- ptrunc(x, collapse = ", ", width = width, dots = dots)
if (length(x) > 1)
res <- paste0(brackets[[1]], res, brackets[[2]])
else
res <- paste0(quotes[[1]], res, quotes[[2]])
res
}
#' Collapse text vectors with a comma
#'
#' @param x `character` vector
#'
#' @return a `character` scalar
#' @noRd
comma <- function(..., collapse = ", "){
paste(unlist(c(...)), collapse = collapse)
}
#' Collapse text vectors with a comma (no duplicates)
#'
#' @param x `character` vector
#'
#' @return a `character` scalar
#' @noRd
commaset <- function(..., collapse = ", "){
paste(sort(unique(unlist(c(...)))), collapse = collapse)
}
#' Clean up paths to make them comparable, inspired by fs::path_tidy
#'
#' @param x `character` vector
#'
#' @return a `character` vector
#' @noRd
path_tidy <- function(x){
x <- gsub("\\\\", "/", x)
x <- gsub("(?!^)/+", "/", x, perl = TRUE)
sel <- x != "/"
x[sel] <- gsub("/$", "", x[sel])
sel <- is_windows_path(x)
if (any(sel)){
clean_win <- function(.x){
substr(.x, 1, 1) <- toupper(substr(.x, 1 ,1))
.sel <- nchar(.x) == 2
.x[.sel] <- paste0(.x[.sel], "/")
.x
}
x[sel] <- clean_win(x[sel])
}
x
}
#' Return (unique) duplicated elements of a vector or rows of a data.frame
#'
#' For every element/row of `x` that has at least one duplicate, return one
#' instance of that element.
#'
#' @param x an [atomic] vector or [data.frame]
#' @param ... passed on to [duplicated()]
#'
#' @noRd
#'
#' @examples
#' dupes(c(1, 1, 1, 2))
#' dupes(cars[c(1, 1, 1, 2), ])
dupes <- function(x, ...){
if (is.atomic(x)){
sort(unique(x[duplicated(x, ...)]))
} else if (is.data.frame(x)){
res <- unique(x[duplicated(x, ...), ])
row.names(res) <- NULL
res
}
}
# nocov end