forked from yihui/xfun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
300 lines (278 loc) · 9.44 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
stop2 = function(...) stop(..., call. = FALSE)
warning2 = function(...) warning(..., call. = FALSE)
#' Obtain an attribute of an object without partial matching
#'
#' An abbreviation of \code{base::\link[base]{attr}(exact = TRUE)}.
#' @param ... Passed to \code{base::\link[base]{attr}()} (without the
#' \code{exact} argument).
#' @export
#' @examples
#' z = structure(list(a = 1), foo = 2)
#' base::attr(z, 'f') # 2
#' xfun::attr(z, 'f') # NULL
#' xfun::attr(z, 'foo') # 2
attr = function(...) base::attr(..., exact = TRUE)
#' Set environment variables
#'
#' Set environment variables from a named character vector, and return the old
#' values of the variables, so they could be restored later.
#'
#' The motivation of this function is that \code{\link{Sys.setenv}()} does not
#' return the old values of the environment variables, so it is not
#' straightforward to restore the variables later.
#' @param vars A named character vector of the form \code{c(VARIABLE = VALUE)}.
#' If any value is \code{NA}, this function will try to unset the variable.
#' @return Old values of the variables (if not set, \code{NA}).
#' @export
#' @examples
#' vars = xfun::set_envvar(c(FOO = '1234'))
#' Sys.getenv('FOO')
#' xfun::set_envvar(vars)
#' Sys.getenv('FOO')
set_envvar = function(vars) {
if (is.null(nms <- names(vars)) || any(nms == '')) stop(
"The 'vars' argument must take a named character vector."
)
vals = Sys.getenv(nms, NA, names = TRUE)
i = is.na(vars)
suppressWarnings(Sys.unsetenv(nms[i]))
if (length(vars <- vars[!i])) do.call(Sys.setenv, as.list(vars))
invisible(vals)
}
#' Call \code{on.exit()} in a parent function
#'
#' The function \code{\link{on.exit}()} is often used to perform tasks when the
#' current function exits. This \code{exit_call()} function allows calling a
#' function when a parent function exits (thinking of it as inserting an
#' \code{on.exit()} call into the parent function).
#' @param fun A function to be called when the parent function exits.
#' @param n The parent frame number. For \code{n = 1}, \code{exit_call(fun)} is
#' the same as \code{on.exit(fun())}; \code{n = 2} means adding
#' \code{on.exit(fun())} in the parent function; \code{n = 3} means the
#' grandparent, etc.
#' @param ... Other arguments to be passed to \code{on.exit()}.
#' @references This function was inspired by Kevin Ushey:
#' \url{https://yihui.org/en/2017/12/on-exit-parent/}
#' @export
#' @examples
#' f = function(x) {
#' print(x)
#' xfun::exit_call(function() print('The parent function is exiting!'))
#' }
#' g = function(y) {
#' f(y)
#' print('f() has been called!')
#' }
#' g('An argument of g()!')
exit_call = function(fun, n = 2, ...) {
do.call(
on.exit, list(substitute(fun(), list(fun = fun)), add = TRUE, ...),
envir = parent.frame(n)
)
}
#' Set the global option \code{\link{options}(stringsAsFactors = FALSE)} inside
#' a parent function and restore the option after the parent function exits
#'
#' This is a shorthand of \code{opts = options(stringsAsFactors = FALSE);
#' on.exit(options(opts), add = TRUE)}; \code{strings_please()} is an alias of
#' \code{stringsAsStrings()}.
#' @export
#' @examples
#' f = function() {
#' xfun::strings_please()
#' data.frame(x = letters[1:4], y = factor(letters[1:4]))
#' }
#' str(f()) # the first column should be character
stringsAsStrings = function() {
# TODO: remove this function in the future since stringsAsFactors starts to
# default to FALSE since R 4.0.0
if (isFALSE(getOption('stringsAsFactors'))) return(invisible())
opts = options(stringsAsFactors = FALSE)
exit_call(function() options(opts))
}
#' @rdname stringsAsStrings
#' @export
strings_please = stringsAsStrings
#' Evaluate an expression under a specified working directory
#'
#' Change the working directory, evaluate the expression, and restore the
#' working directory.
#' @param dir Path to a directory.
#' @param expr An R expression.
#' @export
#' @examples
#' library(xfun)
#' in_dir(tempdir(), {print(getwd()); list.files()})
in_dir = function(dir, expr) {
owd = setwd(dir); on.exit(setwd(owd))
expr
}
#' Test if an object is identical to \code{FALSE}
#' *NO LONGER* exported: base::isFALSE() is faster and better,
#' when FALSE has an attribute: base::isFALSE(c(a = FALSE)) is TRUE
#' A simple abbreviation of \code{identical(x, FALSE)}.
#' @param x An R object.
#' @examples
#' library(xfun)
#' isFALSE(TRUE) # false
#' isFALSE(FALSE) # true
#' isFALSE(c(FALSE, FALSE)) # false
isFALSE = function(x) identical(x, FALSE)
#' Parse R code and do not keep the source
#'
#' An abbreviation of \code{parse(keep.source = FALSE)}.
#' @param code A character vector of the R source code.
#' @export
#' @return R \code{\link{expression}}s.
#' @examples library(xfun)
#' parse_only('1+1'); parse_only(c('y~x', '1:5 # a comment'))
#' parse_only(character(0))
parse_only = function(code) {
if (length(code) == 0) return(expression())
parse(text = code, keep.source = FALSE)
}
#' Try to evaluate an expression silently
#'
#' An abbreviation of \code{try(silent = TRUE)}.
#' @param expr An R expression.
#' @export
#' @examples library(xfun)
#' z = try_silent(stop('Wrong!'))
#' inherits(z, 'try-error')
try_silent = function(expr) try(expr, silent = TRUE)
#' Try an expression and see if it throws an error
#'
#' Use \code{\link{tryCatch}()} to check if an expression throws an error.
#' @inheritParams try_silent
#' @return \code{TRUE} (error) or \code{FALSE} (success).
#' @export
#' @examples
#' xfun::try_error(stop('foo')) # TRUE
#' xfun::try_error(1:10) # FALSE
try_error = function(expr) {
err = FALSE
tryCatch(expr, error = function(e) err <<- TRUE)
err
}
#' Retry calling a function for a number of times
#'
#' If the function returns an error, retry it for the specified number of
#' times, with a pause between attempts.
#'
#' One application of this function is to download a web resource. Since the
#' download might fail sometimes, you may want to retry it for a few more times.
#' @param fun A function.
#' @param ... Arguments to be passed to the function.
#' @param .times The number of times.
#' @param .pause The number of seconds to wait before the next attempt.
#' @export
#' @examplesIf interactive()
#' # read the Github releases info of the repo yihui/xfun
#' xfun::retry(xfun::github_releases, 'yihui/xfun')
retry = function(fun, ..., .times = 3, .pause = 5) {
for (i in seq_len(.times)) {
if (!inherits(res <- tryCatch(fun(...), error = identity), 'error'))
return(res)
Sys.sleep(.pause)
}
stop(res$message, call. = FALSE)
}
gsubi = function(...) gsub(..., ignore.case = TRUE)
#' Turn the output of \code{\link{str}()} into a tree diagram
#'
#' The super useful function \code{str()} uses \verb{..} to indicate the level
#' of sub-elements of an object, which may be difficult to read. This function
#' uses vertical pipes to connect all sub-elements on the same level, so it is
#' clearer which elements belong to the same parent element in an object with a
#' nested structure (such as a nested list).
#' @param ... Arguments to be passed to \code{\link{str}()} (note that the
#' \code{comp.str} is hardcoded inside this function, and it is the only
#' argument that you cannot customize).
#' @return A character string as a \code{\link{raw_string}()}.
#' @export
#' @examples fit = lsfit(1:9, 1:9)
#' str(fit)
#' xfun::tree(fit)
#'
#' fit = lm(dist ~ speed, data = cars)
#' str(fit)
#' xfun::tree(fit)
#'
#' # some trivial examples
#' xfun::tree(1:10)
#' xfun::tree(iris)
tree = function(...) {
x = capture.output(str(..., comp.str = '$ '))
r = '^([^$-]+[$-] )(.*)$'
x1 = gsub(r, '\\1', x)
x2 = gsub(r, '\\2', x)
x1 = gsub('[.][.]', ' ', x1)
x1 = gsub('[$] $', '|-', x1)
x1 = connect_pipes(x1)
x3 = paste(x1, x2, sep = '')
i = !grepl(r, x)
x3[i] = x[i]
raw_string(x3)
}
# for a tree diagram, connect the pipes on the same level, e.g., change
# |- ..
# |- ..
#
# |- ..
# to
# |- ..
# |- ..
# |
# |- ..
# this task is not complicated, but just boring nested for-loops
connect_pipes = function(x) {
ns = nchar(x); n = max(ns); m = length(x)
if (n < 2 || m < 3) return(x)
A = matrix('', nrow = m, ncol = n)
x = strsplit(x, '')
for (i in seq_len(m)) {
A[i, seq_len(ns[i])] = x[[i]]
}
k = NULL
for (j in seq_len(n - 1)) {
for (i in seq_len(m - 2)) {
if (!all(A[i, j + 0:1] == c('|', '-'))) next
for (l in (i + 1):m) {
cells = A[l, j + 0:1]
if (all(cells == ' ')) {
if (l == m) {
k = NULL; break
} else k = c(k, l)
} else if (all(cells == c('|', '-'))) {
break
} else {
k = NULL; break
}
}
if (length(k) > 0) A[k, j] = '|'
k = NULL
}
}
apply(A, 1, paste, collapse = '')
}
pkg_file = function(...) system.file(..., package = 'xfun', mustWork = TRUE)
#' Format numbers of bytes using a specified unit
#'
#' Call the S3 method \code{format.object_size()} to format numbers of bytes.
#' @param x A numeric vector (each element represents a number of bytes).
#' @param units,... Passed to \code{\link[=format.object_size]{format}()}.
#' @return A character vector.
#' @export
#' @examples
#' xfun::format_bytes(c(1, 1024, 2000, 1e6, 2e8))
#' xfun::format_bytes(c(1, 1024, 2000, 1e6, 2e8), units = 'KB')
format_bytes = function(x, units = 'auto', ...) {
vapply(x, function(b) {
format(structure(b, class = 'object_size'), units = units, ...)
}, character(1))
}
# get the function name of the parent call
func_name = function(which = 1) {
x = sys.call(which)[[1]]
deparse(x)[1]
}