| 1 |
|
| 2 |
# functions which mask internal / base R equivalents, usually to provide |
| 3 |
# backwards compatibility or guard against common errors |
| 4 |
|
| 5 |
numeric_version <- function(x, strict = TRUE) { |
| 6 |
base::numeric_version(as.character(x), strict = strict) |
| 7 |
} |
| 8 |
|
| 9 |
sprintf <- function(fmt, ...) { |
| 10 |
message <- if (nargs() == 1L) fmt else base::sprintf(fmt, ...) |
| 11 |
ansify(message) |
| 12 |
} |
| 13 |
|
| 14 |
substring <- function(text, first, last = .Machine$integer.max) { |
| 15 |
|
| 16 |
n <- length(text) |
| 17 |
if (n == 0L) |
| 18 |
return(text) |
| 19 |
|
| 20 |
m <- max(n, length(first), length(last)) |
| 21 |
text <- rep_len(as.character(text), length.out = m) |
| 22 |
substr(text, first, last) |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
unique <- function(x) { |
| 27 |
base::unique(x) |
| 28 |
} |
| 29 |
|
| 30 |
# a wrapper for 'utils::untar()' that throws an error if untar fails |
| 31 |
untar <- function(tarfile, |
| 32 |
files = NULL, |
| 33 |
list = FALSE, |
| 34 |
exdir = ".", |
| 35 |
tar = Sys.getenv("TAR")) |
| 36 |
{ |
| 37 |
# delegate to utils::untar() |
| 38 |
result <- utils::untar( |
| 39 |
tarfile = tarfile, |
| 40 |
files = files, |
| 41 |
list = list, |
| 42 |
exdir = exdir, |
| 43 |
tar = tar |
| 44 |
) |
| 45 |
|
| 46 |
# check for errors (tar returns a status code) |
| 47 |
if (is.integer(result) && result != 0L) { |
| 48 |
call <- stringify(sys.call()) |
| 49 |
stopf("'%s' returned status code %i", call, result) |
| 50 |
} |
| 51 |
|
| 52 |
# return other results as-is |
| 53 |
result |
| 54 |
} |
| 55 |
|
| 56 |
# prefer writing files as UTF-8 |
| 57 |
writeLines <- function(text, con = stdout(), sep = "\n", useBytes = FALSE) { |
| 58 |
if (is.character(con) && missing(useBytes)) |
| 59 |
base::writeLines(enc2utf8(text), con = con, sep = sep, useBytes = TRUE) |
| 60 |
else |
| 61 |
base::writeLines(text, con, sep, useBytes) |
| 62 |
} |