| 1 |
|
| 2 |
`%>%` <- function(...) { |
| 3 |
|
| 4 |
dots <- eval(substitute(alist(...))) |
| 5 |
if (length(dots) != 2L) |
| 6 |
stopf("`%>%` called with invalid number of arguments") |
| 7 |
|
| 8 |
lhs <- dots[[1L]]; rhs <- dots[[2L]] |
| 9 |
if (!is.call(rhs)) |
| 10 |
stopf("right-hand side of rhs is not a call") |
| 11 |
|
| 12 |
data <- c(rhs[[1L]], lhs, as.list(rhs[-1L])) |
| 13 |
call <- as.call(data) |
| 14 |
|
| 15 |
nm <- names(rhs) |
| 16 |
if (length(nm)) |
| 17 |
names(call) <- c("", "", nm[-1L]) |
| 18 |
|
| 19 |
eval(call, envir = parent.frame()) |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
`%NA%` <- function(x, y) { |
| 24 |
if (length(x) && is.na(x)) y else x |
| 25 |
} |
| 26 |
|
| 27 |
`%&&%` <- function(x, y) { |
| 28 |
if (length(x)) y |
| 29 |
} |
| 30 |
|
| 31 |
lines <- function(...) { |
| 32 |
paste(..., sep = "\n") |
| 33 |
} |
| 34 |
|
| 35 |
is_named <- function(x) { |
| 36 |
nm <- names(x) |
| 37 |
!is.null(nm) && all(nzchar(nm)) |
| 38 |
} |
| 39 |
|
| 40 |
named <- function(object, names = object) { |
| 41 |
names(object) <- names |
| 42 |
object |
| 43 |
} |
| 44 |
|
| 45 |
empty <- function(x) { |
| 46 |
length(x) == 0L |
| 47 |
} |
| 48 |
|
| 49 |
zlength <- function(x) { |
| 50 |
length(x) != 0L |
| 51 |
} |
| 52 |
|
| 53 |
dQuote <- function(x) { |
| 54 |
encodeString(x, quote = "\"", justify = "none") |
| 55 |
} |
| 56 |
|
| 57 |
trim <- function(x) { |
| 58 |
gsub("^\\s+|\\s+$", "", x, perl = TRUE) |
| 59 |
} |
| 60 |
|
| 61 |
trimws <- function(x) { |
| 62 |
gsub("^\\s+|\\s+$", "", x, perl = TRUE) |
| 63 |
} |
| 64 |
|
| 65 |
case <- function(...) { |
| 66 |
|
| 67 |
dots <- eval(substitute(alist(...))) |
| 68 |
for (i in seq_along(dots)) { |
| 69 |
|
| 70 |
if (identical(dots[[i]], quote(expr = ))) |
| 71 |
next |
| 72 |
|
| 73 |
dot <- eval(dots[[i]], envir = parent.frame()) |
| 74 |
if (!inherits(dot, "formula")) |
| 75 |
return(dot) |
| 76 |
|
| 77 |
# Silence R CMD check note |
| 78 |
expr <- NULL |
| 79 |
cond <- NULL |
| 80 |
|
| 81 |
# use delayed assignments below so we can allow return statements to |
| 82 |
# be handled in the lexical scope where they were defined |
| 83 |
if (length(dot) == 2L) { |
| 84 |
do.call(delayedAssign, list("expr", dot[[2L]], eval.env = environment(dot))) |
| 85 |
return(expr) |
| 86 |
} |
| 87 |
|
| 88 |
do.call(delayedAssign, list("cond", dot[[2L]], eval.env = environment(dot))) |
| 89 |
do.call(delayedAssign, list("expr", dot[[3L]], eval.env = environment(dot))) |
| 90 |
if (cond) return(expr) |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
compose <- function(wrapper, callback) { |
| 97 |
function(...) wrapper(callback(...)) |
| 98 |
} |
| 99 |
|
| 100 |
catch <- function(expr) { |
| 101 |
|
| 102 |
# evaluate in a child environment so that defer() / on.exit() |
| 103 |
# cleanups scope to this eval frame rather than leaking to the |
| 104 |
# caller via R's promise evaluation semantics |
| 105 |
envir <- new.env(parent = parent.frame()) |
| 106 |
|
| 107 |
result <- tryCatch( |
| 108 |
withCallingHandlers( |
| 109 |
eval(substitute(expr), envir = envir), |
| 110 |
error = renv_error_capture |
| 111 |
), |
| 112 |
error = renv_error_tag |
| 113 |
) |
| 114 |
|
| 115 |
# copy bindings back so that <- assignments are visible to the caller |
| 116 |
renv_catch_copy(envir, parent.frame()) |
| 117 |
|
| 118 |
result |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
catchall <- function(expr) { |
| 123 |
|
| 124 |
envir <- new.env(parent = parent.frame()) |
| 125 |
|
| 126 |
result <- tryCatch( |
| 127 |
withCallingHandlers( |
| 128 |
eval(substitute(expr), envir = envir), |
| 129 |
error = renv_error_capture, |
| 130 |
warning = renv_error_capture |
| 131 |
), |
| 132 |
error = renv_error_tag, |
| 133 |
warning = renv_error_tag |
| 134 |
) |
| 135 |
|
| 136 |
renv_catch_copy(envir, parent.frame()) |
| 137 |
|
| 138 |
result |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
renv_catch_copy <- function(envir, parent) { |
| 143 |
for (name in ls(envir = envir, all.names = TRUE)) |
| 144 |
assign(name, envir[[name]], envir = parent) |
| 145 |
} |
| 146 |
|
| 147 |
# nocov start |
| 148 |
|
| 149 |
ask <- function(question, default = FALSE) { |
| 150 |
|
| 151 |
if (renv_tests_running()) |
| 152 |
return(TRUE) |
| 153 |
|
| 154 |
enabled <- getOption("renv.prompt.enabled", default = TRUE) |
| 155 |
if (!enabled) |
| 156 |
return(default) |
| 157 |
|
| 158 |
if (!interactive()) |
| 159 |
return(default) |
| 160 |
|
| 161 |
# can't prompt for input when autoloading; code run from `.Rprofile` should |
| 162 |
# not attempt to interact with the user |
| 163 |
# from `?Startup`: |
| 164 |
# "It is not intended that there be interaction with the user during startup |
| 165 |
# code. Attempting to do so can crash the R process." |
| 166 |
# https://github.com/rstudio/renv/issues/1879 |
| 167 |
if (autoloading()) |
| 168 |
return(default) |
| 169 |
|
| 170 |
# be verbose in this scope, as we're asking the user for input |
| 171 |
renv_scope_options(renv.verbose = TRUE) |
| 172 |
|
| 173 |
repeat { |
| 174 |
|
| 175 |
# solicit user's answer |
| 176 |
selection <- if (default) "[Y/n]" else "[y/N]" |
| 177 |
prompt <- sprintf("%s %s: ", question, selection) |
| 178 |
response <- tryCatch( |
| 179 |
tolower(trimws(readline(prompt))), |
| 180 |
interrupt = identity |
| 181 |
) |
| 182 |
|
| 183 |
# check for interrupts; treat as abort request |
| 184 |
cancel_if(inherits(response, "interrupt")) |
| 185 |
|
| 186 |
# use default when no response |
| 187 |
if (!nzchar(response)) |
| 188 |
return(default) |
| 189 |
|
| 190 |
# check for 'yes' responses |
| 191 |
if (response %in% c("y", "yes")) { |
| 192 |
writef("") |
| 193 |
return(TRUE) |
| 194 |
} |
| 195 |
|
| 196 |
# check for 'no' responses |
| 197 |
if (response %in% c("n", "no")) { |
| 198 |
writef("") |
| 199 |
return(FALSE) |
| 200 |
} |
| 201 |
|
| 202 |
# ask the user again |
| 203 |
writef("- Unrecognized response: please enter 'y' or 'n', or type Ctrl + C to cancel.") |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
proceed <- function(default = TRUE) { |
| 210 |
ask("Do you want to proceed?", default = default) |
| 211 |
} |
| 212 |
|
| 213 |
menu <- function(choices, title, default = 1L) { |
| 214 |
|
| 215 |
testing <- getOption("renv.menu.choice", integer()) |
| 216 |
selected <- if (length(testing)) { |
| 217 |
options(renv.menu.choice = testing[-1L]) |
| 218 |
testing[[1L]] |
| 219 |
} else if (testing()) { |
| 220 |
default |
| 221 |
} |
| 222 |
|
| 223 |
if (!is.null(selected)) { |
| 224 |
title <- paste(title, collapse = "\n") |
| 225 |
body <- paste(sprintf("%i: %s", seq_along(choices), choices), collapse = "\n") |
| 226 |
footer <- sprintf("Selection: %s\n", selected) |
| 227 |
writef(paste(c(title, body, footer), collapse = "\n\n")) |
| 228 |
return(names(choices)[selected]) |
| 229 |
} |
| 230 |
|
| 231 |
if (!interactive()) { |
| 232 |
value <- if (is.numeric(default)) names(choices)[default] else default |
| 233 |
return(value) |
| 234 |
} |
| 235 |
|
| 236 |
idx <- tryCatch( |
| 237 |
utils::menu(choices, paste(title, collapse = "\n"), graphics = FALSE), |
| 238 |
interrupt = function(cnd) 0L |
| 239 |
) |
| 240 |
|
| 241 |
if (idx == 0L) |
| 242 |
return("cancel") |
| 243 |
|
| 244 |
names(choices)[idx] |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
# nocov end |
| 249 |
|
| 250 |
insert <- function(contents, |
| 251 |
pattern, |
| 252 |
replacement, |
| 253 |
anchor = NULL, |
| 254 |
fixed = FALSE) |
| 255 |
{ |
| 256 |
# first, check to see if the pattern matches a line |
| 257 |
index <- grep(pattern, contents, perl = !fixed, fixed = fixed) |
| 258 |
if (length(index)) { |
| 259 |
contents[index] <- replacement |
| 260 |
return(contents) |
| 261 |
} |
| 262 |
|
| 263 |
# otherwise, check for the anchor, and insert after |
| 264 |
index <- if (!is.null(anchor)) |
| 265 |
grep(anchor, contents, perl = !fixed, fixed = fixed) |
| 266 |
|
| 267 |
if (!length(index)) |
| 268 |
return(c(contents, replacement)) |
| 269 |
|
| 270 |
c( |
| 271 |
head(contents, n = index), |
| 272 |
replacement, |
| 273 |
tail(contents, n = -index) |
| 274 |
) |
| 275 |
} |
| 276 |
|
| 277 |
deparsed <- function(value, width = 60L) { |
| 278 |
paste(deparse(value, width.cutoff = width), collapse = "\n") |
| 279 |
} |
| 280 |
|
| 281 |
read <- function(file) { |
| 282 |
renv_scope_options(warn = -1L) |
| 283 |
contents <- readLines(file, warn = FALSE) |
| 284 |
paste(contents, collapse = "\n") |
| 285 |
} |
| 286 |
|
| 287 |
plural <- function(word, n) { |
| 288 |
suffixes <- c("", "s") |
| 289 |
indices <- as.integer(n != 1L) + 1L |
| 290 |
paste0(word, suffixes[indices]) |
| 291 |
} |
| 292 |
|
| 293 |
nplural <- function(word, n) { |
| 294 |
paste(n, plural(word, n)) |
| 295 |
} |
| 296 |
|
| 297 |
trunc <- function(text, n = 78) { |
| 298 |
long <- nchar(text) > n |
| 299 |
text[long] <- sprintf("%s <...>", substring(text[long], 1, n - 6)) |
| 300 |
text |
| 301 |
} |
| 302 |
|
| 303 |
# like tools::file_ext, but includes leading '.', and preserves |
| 304 |
# '.tar.gz', '.tar.bz' and so on |
| 305 |
fileext <- function(path, default = "") { |
| 306 |
indices <- regexpr("[.]((?:tar[.])?[[:alnum:]]+)$", path, perl = TRUE) |
| 307 |
ifelse(indices > -1L, substring(path, indices), default) |
| 308 |
} |
| 309 |
|
| 310 |
visited <- function(name, envir) { |
| 311 |
value <- envir[[name]] %||% FALSE |
| 312 |
envir[[name]] <- TRUE |
| 313 |
value |
| 314 |
} |
| 315 |
|
| 316 |
zmap <- function(x, f) { |
| 317 |
callback <- function(x) do.call(f, x) |
| 318 |
lapply(x, callback) |
| 319 |
} |
| 320 |
|
| 321 |
rowapply <- function(X, FUN, ...) { |
| 322 |
lapply(seq_len(NROW(X)), function(I) { |
| 323 |
FUN(X[I, , drop = FALSE], ...) |
| 324 |
}) |
| 325 |
} |
| 326 |
|
| 327 |
comspec <- function() { |
| 328 |
Sys.getenv("COMSPEC", unset = Sys.which("cmd.exe")) |
| 329 |
} |
| 330 |
|
| 331 |
nullfile <- function() { |
| 332 |
if (renv_platform_windows()) "NUL" else "/dev/null" |
| 333 |
} |
| 334 |
|
| 335 |
quietly <- function(expr, sink = TRUE) { |
| 336 |
|
| 337 |
if (sink) { |
| 338 |
sink(file = nullfile()) |
| 339 |
defer(sink(NULL)) |
| 340 |
} |
| 341 |
|
| 342 |
withCallingHandlers( |
| 343 |
expr, |
| 344 |
warning = function(c) invokeRestart("muffleWarning"), |
| 345 |
message = function(c) invokeRestart("muffleMessage"), |
| 346 |
packageStartupMessage = function(c) invokeRestart("muffleMessage") |
| 347 |
) |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
# NOTE: This function can be used in preference to `as.*()` if you'd like |
| 352 |
# to preserve attributes on the incoming object 'x'. |
| 353 |
convert <- function(x, type) { |
| 354 |
storage.mode(x) <- type |
| 355 |
x |
| 356 |
} |
| 357 |
|
| 358 |
remap <- function(x, map) { |
| 359 |
|
| 360 |
# TODO: use match? |
| 361 |
remapped <- x |
| 362 |
enumerate(map, function(key, val) { |
| 363 |
remapped[remapped == key] <<- val |
| 364 |
}) |
| 365 |
remapped |
| 366 |
|
| 367 |
} |
| 368 |
|
| 369 |
keep <- function(x, keys) { |
| 370 |
x[intersect(keys, names(x))] |
| 371 |
} |
| 372 |
|
| 373 |
keep_if <- function(x, f) { |
| 374 |
x[f(x)] |
| 375 |
} |
| 376 |
|
| 377 |
omit <- function(x, keys) { |
| 378 |
x[setdiff(names(x), keys)] |
| 379 |
} |
| 380 |
|
| 381 |
omit_if <- function(x, f) { |
| 382 |
x[!f(x)] |
| 383 |
} |
| 384 |
|
| 385 |
invoke <- function(callback, ...) { |
| 386 |
callback(...) |
| 387 |
} |
| 388 |
|
| 389 |
resolve <- function(object) { |
| 390 |
|
| 391 |
while (is.function(object)) |
| 392 |
object <- object() |
| 393 |
|
| 394 |
object |
| 395 |
|
| 396 |
} |
| 397 |
|
| 398 |
dequote <- function(strings) { |
| 399 |
|
| 400 |
for (quote in c("'", '"')) { |
| 401 |
|
| 402 |
# find strings matching pattern |
| 403 |
pattern <- paste0(quote, "(.*)", quote) |
| 404 |
matches <- grep(pattern, strings, perl = TRUE) |
| 405 |
if (empty(matches)) |
| 406 |
next |
| 407 |
|
| 408 |
# remove outer quotes |
| 409 |
strings[matches] <- gsub(pattern, "\\1", strings[matches], perl = TRUE) |
| 410 |
|
| 411 |
# un-escape inner quotes |
| 412 |
pattern <- paste0("\\", quote) |
| 413 |
strings[matches] <- gsub(pattern, quote, strings[matches], fixed = TRUE) |
| 414 |
} |
| 415 |
|
| 416 |
strings |
| 417 |
|
| 418 |
} |
| 419 |
|
| 420 |
nth <- function(x, i) { |
| 421 |
x[[i]] |
| 422 |
} |
| 423 |
|
| 424 |
find <- function(x, f, ...) { |
| 425 |
for (i in seq_along(x)) |
| 426 |
if (!is.null(value <- f(x[[i]], ...))) |
| 427 |
return(value) |
| 428 |
} |
| 429 |
|
| 430 |
recursing <- function() { |
| 431 |
|
| 432 |
nf <- sys.nframe() |
| 433 |
if (nf < 2L) |
| 434 |
return(FALSE) |
| 435 |
|
| 436 |
np <- sys.parent() |
| 437 |
fn <- sys.function(np) |
| 438 |
for (i in seq_len(np - 1L)) |
| 439 |
if (identical(fn, sys.function(i))) |
| 440 |
return(TRUE) |
| 441 |
|
| 442 |
FALSE |
| 443 |
|
| 444 |
} |
| 445 |
|
| 446 |
csort <- function(x, decreasing = FALSE, ...) { |
| 447 |
renv_scope_locale("LC_COLLATE", "C") |
| 448 |
sort(x, decreasing, ...) |
| 449 |
} |
| 450 |
|
| 451 |
fsub <- function(pattern, replacement, x, ignore.case = FALSE, useBytes = FALSE) { |
| 452 |
sub(pattern, replacement, x, ignore.case = ignore.case, useBytes = useBytes, fixed = TRUE) |
| 453 |
} |
| 454 |
|
| 455 |
rows <- function(data, indices) { |
| 456 |
|
| 457 |
if (is.null(data)) |
| 458 |
return(data_frame()) |
| 459 |
|
| 460 |
# convert logical values |
| 461 |
if (is.logical(indices)) { |
| 462 |
if (length(indices) < nrow(data)) |
| 463 |
indices <- rep(indices, length.out = nrow(data)) |
| 464 |
indices <- which(indices, useNames = FALSE) |
| 465 |
} |
| 466 |
|
| 467 |
# build output list |
| 468 |
output <- vector("list", length(data)) |
| 469 |
for (i in seq_along(data)) |
| 470 |
output[[i]] <- .subset2(data, i)[indices] |
| 471 |
|
| 472 |
# copy relevant attributes |
| 473 |
attrs <- attributes(data) |
| 474 |
attrs[["row.names"]] <- .set_row_names(length(indices)) |
| 475 |
attributes(output) <- attrs |
| 476 |
|
| 477 |
# return new data.frame |
| 478 |
output |
| 479 |
|
| 480 |
} |
| 481 |
|
| 482 |
cols <- function(data, indices) { |
| 483 |
|
| 484 |
# perform subset |
| 485 |
output <- .subset(data, indices) |
| 486 |
|
| 487 |
# copy relevant attributes |
| 488 |
attrs <- attributes(data) |
| 489 |
attrs[["names"]] <- attr(output, "names", exact = TRUE) |
| 490 |
attributes(output) <- attrs |
| 491 |
|
| 492 |
# return output |
| 493 |
output |
| 494 |
|
| 495 |
} |
| 496 |
|
| 497 |
stringify <- function(object, collapse = " ") { |
| 498 |
|
| 499 |
if (is.symbol(object)) |
| 500 |
return(as.character(object)) |
| 501 |
|
| 502 |
paste( |
| 503 |
deparse(object, width.cutoff = 500L), |
| 504 |
collapse = collapse |
| 505 |
) |
| 506 |
|
| 507 |
} |
| 508 |
|
| 509 |
env <- function(...) { |
| 510 |
list2env(list(...), envir = new.env(parent = emptyenv())) |
| 511 |
} |
| 512 |
|
| 513 |
env2list <- function(env) { |
| 514 |
as.list.environment(env, all.names = TRUE) |
| 515 |
} |
| 516 |
|
| 517 |
chop <- function(x, split = "\n", fixed = TRUE, perl = FALSE, useBytes = FALSE) { |
| 518 |
strsplit(x, split, !perl, perl, useBytes)[[1L]] |
| 519 |
} |
| 520 |
|
| 521 |
prof <- function(expr, ...) { |
| 522 |
|
| 523 |
profile <- tempfile("renv-profile-", fileext = ".Rprof") |
| 524 |
|
| 525 |
Rprof(profile, ...) |
| 526 |
result <- expr |
| 527 |
Rprof(NULL) |
| 528 |
print(summaryRprof(profile)) |
| 529 |
|
| 530 |
invisible(result) |
| 531 |
|
| 532 |
} |
| 533 |
|
| 534 |
recycle <- function(data) { |
| 535 |
|
| 536 |
# compute number of columns |
| 537 |
n <- lengths(data, use.names = FALSE) |
| 538 |
nrow <- max(n) |
| 539 |
|
| 540 |
# start recycling |
| 541 |
for (i in seq_along(data)) { |
| 542 |
if (n[[i]] == 0L) { |
| 543 |
length(data[[i]]) <- nrow |
| 544 |
} else if (n[[i]] != nrow) { |
| 545 |
data[[i]] <- rep.int(data[[i]], nrow / n[[i]]) |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
data |
| 550 |
|
| 551 |
} |
| 552 |
|
| 553 |
take <- function(data, index = NULL) { |
| 554 |
if (is.null(index)) data else .subset2(data, index) |
| 555 |
} |
| 556 |
|
| 557 |
cancel <- function(verbose = TRUE) { |
| 558 |
|
| 559 |
renv_snapshot_auto_suppress_next() |
| 560 |
if (testing()) |
| 561 |
stop("Operation canceled", call. = FALSE) |
| 562 |
|
| 563 |
if (verbose) |
| 564 |
message("- Operation canceled.") |
| 565 |
invokeRestart("abort") |
| 566 |
|
| 567 |
} |
| 568 |
|
| 569 |
cancel_if <- function(cnd) { |
| 570 |
if (cnd) cancel() |
| 571 |
} |
| 572 |
|
| 573 |
rep_named <- function(names, x) { |
| 574 |
values <- rep_len(x, length(names)) |
| 575 |
names(values) <- names |
| 576 |
values |
| 577 |
} |
| 578 |
|
| 579 |
wait_until <- function(callback, ...) { |
| 580 |
repeat if (callback(...)) return(TRUE) |
| 581 |
} |
| 582 |
|
| 583 |
summon <- function() { |
| 584 |
envir <- do.call(attach, list(what = NULL, name = "renv")) |
| 585 |
renv <- renv_envir_self() |
| 586 |
list2env(as.list(renv), envir = envir) |
| 587 |
} |
| 588 |
|
| 589 |
overlay <- function(lhs, rhs) { |
| 590 |
modifyList(as.list(lhs), as.list(rhs)) |
| 591 |
} |
| 592 |
|
| 593 |
# the 'top' renv function in the call stack |
| 594 |
topfun <- function() { |
| 595 |
|
| 596 |
self <- renv_envir_self() |
| 597 |
frames <- sys.frames() |
| 598 |
|
| 599 |
for (i in seq_along(frames)) |
| 600 |
if (identical(self, parent.env(frames[[i]]))) |
| 601 |
return(sys.function(i)) |
| 602 |
|
| 603 |
} |
| 604 |
|
| 605 |
warnify <- function(cnd) { |
| 606 |
|
| 607 |
# re-signal condition as warning |
| 608 |
class(cnd) <- c("warning", "condition") |
| 609 |
warning(cnd) |
| 610 |
|
| 611 |
# return the condition invisibly |
| 612 |
invisible(cnd) |
| 613 |
|
| 614 |
} |
| 615 |
|
| 616 |
when <- function(condition, consequence, alternative) { |
| 617 |
if (condition) consequence else alternative |
| 618 |
} |
| 619 |
|
| 620 |
# note: also handles stringy values like 'True' |
| 621 |
not <- function(value) { |
| 622 |
if (value) FALSE else TRUE |
| 623 |
} |
| 624 |
|
| 625 |
wait <- function(predicate, ...) { |
| 626 |
while (TRUE) |
| 627 |
if (predicate(...)) |
| 628 |
break |
| 629 |
} |
| 630 |
|
| 631 |
exclude <- function(value, exclude) { |
| 632 |
value[match(value, exclude, 0L) == 0L] |
| 633 |
} |
| 634 |
|