Debian Codegraph @

R/utils.R

raw
1 captures <- function(x, m) {
2 assert("`x` must be a character", is.character(x))
3 assert(
4 "`m` must be a match object from `regexpr()`",
5 inherits(m, "integer") &&
6 all(
7 c(
8 "match.length",
9 "capture.start",
10 "capture.length",
11 "capture.names"
12 ) %in%
13 names(attributes(m))
14 )
15 )
16
17 starts <- attr(m, "capture.start")
18 strings <- substring(x, starts, starts + attr(m, "capture.length") - 1L)
19 res <- data.frame(
20 matrix(strings, ncol = NCOL(starts)),
21 stringsAsFactors = FALSE
22 )
23 colnames(res) <- auto_name(attr(m, "capture.names"))
24 res[is.na(m) | m == -1, ] <- NA_character_
25 res
26 }
27
28 auto_name <- function(names) {
29 missing <- names == ""
30 if (all(!missing)) {
31 return(names)
32 }
33 names[missing] <- seq_along(names)[missing]
34 names
35 }
36
37 is_windows <- function() {
38 # mock for tests
39 if (isTRUE(Sys.getenv("FS_IS_WINDOWS", "FALSE") == "TRUE")) {
40 return(TRUE)
41 }
42
43 tolower(Sys.info()[["sysname"]]) == "windows"
44 }
45
46 # This is needed to avoid checking the class of fs_path objects in the
47 # tests.
48 # @export
49 compare.fs_path <- function(x, y, ...) {
50 if (identical(class(y), "character")) {
51 class(x) <- NULL
52 }
53 names(x) <- NULL
54 names(y) <- NULL
55 NextMethod("compare")
56 }
57
58 # @export
59 compare.fs_perms <- function(x, y, ...) {
60 if (!inherits(y, "fs_perms")) {
61 y <- as.character(as_fs_perms(y))
62 x <- as.character(x)
63 }
64 NextMethod("compare")
65 }
66
67 nchar <- function(x, type = "chars", allowNA = FALSE, keepNA = NA) {
68 # keepNA was introduced in R 3.2.1, previous behavior was equivalent to keepNA
69 # = FALSE
70 if (getRversion() < "3.2.1") {
71 if (!identical(keepNA, FALSE)) {
72 stop("`keepNA` must be `FALSE` for R < 3.2.1", call. = FALSE)
73 }
74 return(base::nchar(x, type, allowNA))
75 }
76 base::nchar(x, type, allowNA, keepNA)
77 }
78
79 `%||%` <- function(x, y) if (is.null(x)) y else x
80
81 # Only use deterministic entries if we are building documentation in pkgdown.
82 pkgdown_tmp <- function(path) {
83 if (identical(Sys.getenv("IN_PKGDOWN"), "true")) {
84 file_temp_push(path)
85 }
86 }
87
88 # This is adapted from glue::collapse
89 # https://github.com/tidyverse/glue/blob/cac874724d09d430036d1bdeba77982e953f29a2/R/glue.R#L140-L161
90 collapse <- function(x, sep = "", width = Inf, last = "") {
91 if (length(x) == 0) {
92 return(character())
93 }
94 if (any(is.na(x))) {
95 return(NA_character_)
96 }
97
98 if (nzchar(last) && length(x) > 1) {
99 res <- collapse(x[seq(1, length(x) - 1)], sep = sep, width = Inf)
100 return(collapse(paste0(res, last, x[length(x)]), width = width))
101 }
102 x <- paste0(x, collapse = sep)
103 if (width < Inf) {
104 x_width <- nchar(x, "width", keepNA = FALSE)
105 too_wide <- x_width > width
106 if (too_wide) {
107 x <- paste0(substr(x, 1, width - 3), "...")
108 }
109 }
110 x
111 }
112
113 assert_no_missing <- function(x) {
114 nme <- as.character(substitute(x))
115 idx <- which(is.na(x))
116 if (length(idx) > 0) {
117 number <- prettyNum(length(idx), big.mark = ",")
118 remaining_width <- getOption("width") - nchar(number, keepNA = FALSE) - 29
119 indexes <- collapse(
120 idx,
121 width = remaining_width,
122 sep = ", ",
123 last = " and "
124 )
125 msg <- sprintf(
126 "`%s` must not have missing values
127 * NAs found at %s locations: %s",
128 nme,
129 number,
130 indexes
131 )
132
133 stop(fs_error(msg))
134 }
135 }
136
137 assert <- function(msg, ..., class = "invalid_argument") {
138 tests <- unlist(list(...))
139
140 if (!all(tests)) {
141 stop(fs_error(msg, class = class))
142 }
143 }
144
145 fs_error <- function(msg, class = "invalid_argument") {
146 structure(
147 class = c(class, "fs_error", "error", "condition"),
148 list(message = msg)
149 )
150 }
151
152 lengths <- function(x) {
153 vapply(x, length, integer(1))
154 }
155
156 as_tibble <- function(x) {
157 if (getOption("fs.use_tibble", TRUE) && is_installed("tibble")) {
158 tibble::as_tibble(x)
159 } else {
160 x
161 }
162 }
163
164 is_installed <- function(pkg) {
165 isTRUE(requireNamespace(pkg, quietly = TRUE))
166 }
167
168 mkdirp <- function(x) {
169 dir.create(x, showWarnings = FALSE, recursive = TRUE)
170 }
171
172
173 # Calls a function via deduplication of the input rather than each individual element.
174 call_with_deduplication <- function(func, x, ...) {
175 unique_x <- unique(x)
176
177 func(unique_x, ...)[match(x, unique_x)]
178 }