no native code · R: 19 definitions, 0 typed strict, 12 gradual, prelude debt 6 · fuzzing: 0 definitions, 0 unsound, 0 accept calls outside their domain · tyr dc7f1a7 · Rsem 5c142d7 · NativeSem 770a1eb · rstt 2b2696f unpinned
t — a name the prelude does not declarename: t
color_diff_cie76 <- function(color, refs) {
d <- t(refs) - c(color)
sqrt(colSums(d * d))
}col2rgb — a name the prelude does not declarename: col2rgb
pretty_color <- function(color) {
stopifnot(length(color) == 1)
if (is.na(color)) {
structure(NA_character_, alt = NA_character_)
} else {
if (is.factor(color)) color <- as.character(color)
stopifnot(is.character(color))
color_rgb <- col2rgb(color)
color_lab <- convertColor(t(color_rgb), from = "sRGB", to = "Lab", scale.in = 256)
dist <- color_diff_cie76(
color_lab,
as.matrix(color_reference[, c("L", "a", "b")])
)
ret <- color_reference$name[dist == min(dist)][1]
attr(ret, "alt") <- color_reference$name_alt[dist == min(dist)][[1]]
ret
}
}pretty_color — one of the package’s own names, itself untypedname: pretty_color
pretty_colour <- pretty_colorNULLformat_num — one of the package’s own names, itself untypedname: format_num
pretty_num <- format_num$pretty_numformat_num — one of the package’s own names, itself untypedname: format_num
compute_num <- format_num$compute_nummatch — a name the prelude does not declarename: match
format_num <- local({
pretty_num <- function(number, style = c("default", "nopad", "6")) {
style <- switch(
match.arg(style),
"default" = pretty_num_default,
"nopad" = pretty_num_nopad,
"6" = pretty_num_6
)
style(number)
}
compute_num <- function(number, smallest_prefix = "y") {
prefixes0 <- c("y","z","a","f","p","n","u","m","", "k", "M", "G", "T", "P", "E", "Z", "Y")
zeroshif0 <- 9L
stopifnot(
is.numeric(number),
is.character(smallest_prefix),
length(smallest_prefix) == 1,
!is.na(smallest_prefix),
smallest_prefix %in% prefixes0
)
limits <- c( 999950 * 1000 ^ (seq_len(length(prefixes0) ) - (zeroshif0+1L)))
nrow <- length(limits)
low <- match(smallest_prefix, prefixes0)
zeroshift <- zeroshif0 +1L - low
prefixes <- prefixes0[low:length(prefixes0)]
limits <- limits[low:nrow]
nrow <- nrow - low + 1
neg <- number < 0 & !is.na(number)
number <- abs(number)
mat <- matrix(
rep(number, each = nrow),
nrow = nrow,
ncol = length(number)
)
mat2 <- matrix(mat < limits, nrow = nrow, ncol = length(number))
exponent <- nrow - colSums(mat2) - (zeroshift -1L)
in_range <- function(exponent) {
max(min(exponent,nrow-zeroshift, na.rm = FALSE),1L-zeroshift, na.rm = TRUE)
}
if (length(exponent)) {
exponent <- sapply(exponent, in_range)
}
res <- number / 1000 ^ exponent
prefix <- prefixes[exponent + zeroshift]
## Zero number
res[number == 0] <- 0
prefix[number == 0] <- prefixes[zeroshift]
## NA and NaN number
res[is.na(number)] <- NA_real_
res[is.nan(number)] <- NaN
prefix[is.na(number)] <- "" # prefixes0[low] is meaningless # Includes NaN as well
data.frame(
stringsAsFactors = FALSE,
amount = res,
prefix = prefix,
negative = neg
)
}
pretty_num_default <- function(number) {
szs <- compute_num(number)
amt <- szs$amount
sep <- " "
## String. For fractions we always show two fraction digits
res <- character(length(amt))
int <- is.na(amt) | abs(amt - as.integer(amt)) <= .Machine$double.eps
res[int] <- format(
ifelse(szs$negative[int], -1, 1) * amt[int],
scientific = FALSE
)
res[!int] <- sprintf("%.2f", ifelse(szs$negative[!int], -1, 1) * amt[!int])
format(paste(res, szs$prefix,sep = sep), justify = "right")
}
pretty_num_nopad <- function(number) {
sub("^\\s+", "", pretty_num_default(number))
}
pretty_num_6 <- function(number) {
szs <- compute_num(number, smallest_prefix = "y")
amt <- round(szs$amount,2)
sep <- " "
na <- is.na(amt)
nan <- is.nan(amt)
neg <- !na & !nan & szs$negative
l10p <- !na & !nan & !neg & amt < 10
l100p <- !na & !nan & !neg & amt >= 10 & amt < 100
b100p <- !na & !nan & !neg & amt >= 100
l10n <- !na & !nan & neg & amt < 10
l100n <- !na & !nan & neg & amt >= 10 & amt < 100
b100n <- !na & !nan & neg & amt >= 100
famt <- character(length(amt))
famt[na] <- " NA"
famt[nan] <- " NaN"
famt[l10p] <- sprintf("%.2f", amt[l10p])
famt[l100p] <- sprintf("%.1f", amt[l100p])
famt[b100p] <- sprintf(" %.0f", amt[b100p])
famt[l10n] <- sprintf("-%.1f", amt[l10n])
famt[l100n] <- sprintf(" -%.0f", amt[l100n])
famt[b100n] <- sprintf("-%.0f", amt[b100n])
sub(" $"," ",paste0(famt, sep, szs$prefix))
}
structure(
list(
.internal = environment(),
pretty_num = pretty_num,
compute_num = compute_num
),
class = c("standalone_num", "standalone")
)
})argument: @((CHR | LGL) | v('I841), lgl<>)
failing call: base::[](NA, as.raw(0))pretty_round <- function(x, digits = 0, sci_range = Inf, sci_sep = "e") {
if (length(digits) == 1) {
mask_na <- is.na(x)
mask_aschar <- is.nan(x) | is.infinite(x)
mask_manip <- !(mask_na | mask_aschar)
ret <- rep(NA, length(x))
## Put in the special values
if (any(mask_na)) {
ret[mask_na] <- "NA"
}
if (any(mask_aschar)) {
ret[mask_aschar] <- as.character(x[mask_aschar])
}
if (any(mask_manip)) {
xtmp <- round(x[mask_manip], digits)
mask_sci <-
xtmp != 0 &
abs(log10(abs(xtmp))) >= sci_range
mask_no_sci <- !mask_sci
if (any(mask_sci)) {
logval <- floor(log10(abs(xtmp[mask_sci])))
ret[mask_manip][mask_sci] <-
paste0(
formatC(xtmp[mask_sci] / 10^logval, format = "f", digits = digits + logval),
sci_sep,
formatC(logval, format = "d")
)
}
if (any(mask_no_sci)) {
if (digits < 0) {
ret[mask_manip][mask_no_sci] <-
formatC(xtmp[mask_no_sci], format = "f", digits = 0)
} else {
ret[mask_manip][mask_no_sci] <-
formatC(xtmp[mask_no_sci], format = "f", digits = digits)
}
}
}
ret
} else if (length(x) == length(digits)) {
mapply(pretty_round, x, digits = digits, sci_range = sci_range, sci_sep = sci_sep)
} else {
stop("digits must either be a scalar or the same length as x")
}
}argument: @((dbl & v('I1063))<>, digits: (DBL | INT | LGL) | v('I1104))
failing call: base::round(as.raw(0), digits = NA)pretty_signif <- function(x, digits = 6, sci_range = 6, sci_sep = "e") {
mask_na <- is.na(x)
mask_aschar <- is.nan(x) | is.infinite(x)
mask_manip <- !(mask_na | mask_aschar)
ret <- rep(NA, length(x))
## Put in the special values
if (any(mask_na)) {
ret[mask_na] <- "NA"
}
if (any(mask_aschar)) {
ret[mask_aschar] <- as.character(x[mask_aschar])
}
if (any(mask_manip)) {
xtmp <- x[mask_manip]
toplog <- bottomlog <- rep(NA, length(xtmp))
## When 0 give the digits as the output
bottomlog[xtmp %in% 0] <- digits
## Otherwise set it to digits orders of magnitude lower than the
## current value
toplog <- log10(abs(xtmp))
## When the order of magnitude is an exact log 10, move up one so
## that the math works for determing the lower log.
mask.exact.log <- (toplog %% 1) %in% 0
toplog[mask.exact.log] <- toplog[mask.exact.log] + 1
toplog <- ceiling(toplog)
bottomlog[is.na(bottomlog)] <- digits - toplog[is.na(bottomlog)]
## Find times when rounding increases the toplog and shift up the
## bottomlog to a corresponding degree. e.g. x=0.9999 and digits=2
## should be 1.0 not 1.00.
newtoplog <- log10(abs(round(xtmp, digits = bottomlog)))
mask.exact.log <- (newtoplog %% 1) %in% 0
newtoplog[mask.exact.log] <- newtoplog[mask.exact.log] + 1
newtoplog <- ceiling(newtoplog)
mask.move.up <- toplog < newtoplog
bottomlog[mask.move.up] <- bottomlog[mask.move.up] - 1
## Do the rounding
ret[mask_manip] <- pretty_round(xtmp,
digits = bottomlog,
sci_range = sci_range, sci_sep = sci_sep
)
}
ret
}pretty_p_value <- function(x, minval = 0.0001) {
stopifnot(is.numeric(minval) & !is.factor(minval) & !is.na(minval))
stopifnot(minval < 1 & minval > 0)
ret <- rep(NA_character_, length(x))
if (!all(is.na(x))) {
# The input check on x class and value is here to allow for inputs of all NA
# values to be of any class.
stopifnot(is.numeric(x) & !is.factor(x))
stopifnot(is.na(x) | (x <= 1 & x >= 0))
ndigits <- -floor(log10(minval))
mask_min <- !is.na(x) & x < minval
mask_over <- !is.na(x) & x >= minval
ret[mask_min] <- paste0("<", pretty_round(minval, digits = ndigits))
ret[mask_over] <- pretty_round(x[mask_over], digits = ndigits)
}
ret
}"_PACKAGE"format_bytes — one of the package’s own names, itself untypedname: format_bytes
pretty_bytes <- format_bytes$pretty_bytesformat_bytes — one of the package’s own names, itself untypedname: format_bytes
compute_bytes <- format_bytes$compute_bytesmatch — a name the prelude does not declarename: match
format_bytes <- local({
pretty_bytes <- function(bytes, style = c("default", "nopad", "6")) {
style <- switch(
match.arg(style),
"default" = pretty_bytes_default,
"nopad" = pretty_bytes_nopad,
"6" = pretty_bytes_6
)
style(bytes)
}
compute_bytes <- function(bytes, smallest_unit = "B") {
units0 <- c("B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
stopifnot(
is.numeric(bytes),
is.character(smallest_unit),
length(smallest_unit) == 1,
!is.na(smallest_unit),
smallest_unit %in% units0
)
limits <- c(1000, 999950 * 1000 ^ (seq_len(length(units0) - 2) - 1))
low <- match(smallest_unit, units0)
units <- units0[low:length(units0)]
limits <- limits[low:length(limits)]
neg <- bytes < 0 & !is.na(bytes)
bytes <- abs(bytes)
mat <- matrix(
rep(bytes, each = length(limits)),
nrow = length(limits),
ncol = length(bytes)
)
mat2 <- matrix(mat < limits, nrow = length(limits), ncol = length(bytes))
exponent <- length(limits) - colSums(mat2) + low - 1L
res <- bytes / 1000 ^ exponent
unit <- units[exponent - low + 2L]
## Zero bytes
res[bytes == 0] <- 0
unit[bytes == 0] <- units[1]
## NA and NaN bytes
res[is.na(bytes)] <- NA_real_
res[is.nan(bytes)] <- NaN
unit[is.na(bytes)] <- units0[low] # Includes NaN as well
data.frame(
stringsAsFactors = FALSE,
amount = res,
unit = unit,
negative = neg
)
}
pretty_bytes_default <- function(bytes) {
szs <- compute_bytes(bytes)
amt <- szs$amount
## String. For fractions we always show two fraction digits
res <- character(length(amt))
int <- is.na(amt) | amt == as.integer(amt)
res[int] <- format(
ifelse(szs$negative[int], -1, 1) * amt[int],
scientific = FALSE
)
res[!int] <- sprintf("%.2f", ifelse(szs$negative[!int], -1, 1) * amt[!int])
format(paste(res, szs$unit), justify = "right")
}
pretty_bytes_nopad <- function(bytes) {
sub("^\\s+", "", pretty_bytes_default(bytes))
}
pretty_bytes_6 <- function(bytes) {
szs <- compute_bytes(bytes, smallest_unit = "kB")
amt <- szs$amount
na <- is.na(amt)
nan <- is.nan(amt)
neg <- !na & !nan & szs$negative
l10 <- !na & !nan & !neg & amt < 10
l100 <- !na & !nan & !neg & amt >= 10 & amt < 100
b100 <- !na & !nan & !neg & amt >= 100
szs$unit[neg] <- "kB"
famt <- character(length(amt))
famt[na] <- " NA"
famt[nan] <- "NaN"
famt[neg] <- "< 0"
famt[l10] <- sprintf("%.1f", amt[l10])
famt[l100] <- sprintf(" %.0f", amt[l100])
famt[b100] <- sprintf("%.0f", amt[b100])
paste0(famt, " ", szs$unit)
}
structure(
list(
.internal = environment(),
pretty_bytes = pretty_bytes,
compute_bytes = compute_bytes
),
class = c("standalone_bytes", "standalone")
)
})format_time_ago — one of the package’s own names, itself untypedname: format_time_ago
time_ago <- format_time_ago$time_agoformat_time_ago — one of the package’s own names, itself untypedname: format_time_ago
vague_dt <- format_time_ago$vague_dtexpression — a name the prelude does not declarename: expression
format_time_ago <- local({
e <- expression
`%s%` <- function(lhs, rhs) {
assert_string(lhs)
do.call(
sprintf,
c(list(lhs), as.list(rhs))
)
}
assert_string <- function(x) {
stopifnot(is.character(x), length(x) == 1L)
}
assert_diff_time <- function(x) {
stopifnot(inherits(x, "difftime"))
}
vague_dt_default <- list(
list(c = e(seconds < 10), s = "moments ago"),
list(c = e(seconds < 45), s = "less than a minute ago"),
list(c = e(seconds < 90), s = "about a minute ago"),
list(c = e(minutes < 45), s = e("%d minutes ago" %s% round(minutes))),
list(c = e(minutes < 90), s = "about an hour ago"),
list(c = e(hours < 24), s = e("%d hours ago" %s% round(hours))),
list(c = e(hours < 42), s = "a day ago"),
list(c = e(days < 30), s = e("%d days ago" %s% round(days))),
list(c = e(days < 45), s = "about a month ago"),
list(c = e(days < 335), s = e("%d months ago" %s% round(days / 30))),
list(c = e(years < 1.5), s = "about a year ago"),
list(c = TRUE, s = e("%d years ago" %s% round(years)))
)
vague_dt_short <- list(
list(c = e(seconds < 50), s = "<1 min"),
list(c = e(minutes < 50), s = e("%d min" %s% round(minutes))),
list(c = e(hours < 1.5), s = "1 hour"),
list(c = e(hours < 18), s = e("%d hours" %s% round(hours))),
list(c = e(hours < 42), s = "1 day"),
list(c = e(days < 30), s = e("%d day" %s% round(days))),
list(c = e(days < 45), s = "1 mon"),
list(c = e(days < 335), s = e("%d mon" %s% round(days / 30))),
list(c = e(years < 1.5), s = "1 year"),
list(c = TRUE, s = e("%d years" %s% round(years)))
)
vague_dt_terse <- list(
list(c = e(seconds < 50), s = e("%2ds" %s% round(seconds))),
list(c = e(minutes < 50), s = e("%2dm" %s% round(minutes))),
list(c = e(hours < 18), s = e("%2dh" %s% round(hours))),
list(c = e(days < 30), s = e("%2dd" %s% round(days))),
list(c = e(days < 335), s = e("%2dM" %s% round(days / 30))),
list(c = TRUE, s = e("%2dy" %s% round(years)))
)
vague_dt_formats <- list(
"default" = vague_dt_default,
"short" = vague_dt_short,
"terse" = vague_dt_terse
)
time_ago <- function(date, format = c("default", "short", "terse")) {
date <- as.POSIXct(date)
if (length(date) > 1) return(sapply(date, time_ago, format = format))
seconds <- difftime(Sys.time(), date, units = "secs")
vague_dt(seconds, format = format)
}
vague_dt <- function(dt, format = c("default", "short", "terse")) {
assert_diff_time(dt)
units(dt) <- "secs"
seconds <- as.vector(dt)
## Simplest to quit here for empty input
if (!length(seconds)) return(character())
pieces <- list(
minutes = seconds / 60,
hours = seconds / 60 / 60,
days = seconds / 60 / 60 / 24,
years = seconds / 60 / 60 / 24 / 365.25
)
format <- match.arg(format)
for (p in vague_dt_formats[[format]]) {
if (eval(p$c, pieces)) return(eval(p$s, pieces))
}
}
structure(
list(
.internal = environment(),
time_ago = time_ago,
vague_dt = vague_dt
),
class = c("standalone_time_ago", "standalone")
)
})format_time — one of the package’s own names, itself untypedname: format_time
pretty_ms <- format_time$pretty_msformat_time — one of the package’s own names, itself untypedname: format_time
pretty_sec <- format_time$pretty_secformat_time — one of the package’s own names, itself untypedname: format_time
pretty_dt <- format_time$pretty_dtdata.frame — a name the prelude does not declarename: data.frame
format_time <- local({
assert_diff_time <- function(x) {
stopifnot(inherits(x, "difftime"))
}
parse_ms <- function(ms) {
stopifnot(is.numeric(ms))
data.frame(
days = floor(ms / 86400000),
hours = floor((ms / 3600000) %% 24),
minutes = floor((ms / 60000) %% 60),
seconds = round((ms / 1000) %% 60, 1)
)
}
first_positive <- function(x) which(x > 0)[1]
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
pretty_ms <- function(ms, compact = FALSE) {
stopifnot(is.numeric(ms))
parsed <- t(parse_ms(ms))
if (compact) {
units <- c("d", "h", "m", "s")
parsed2 <- parsed
parsed2[] <- paste0(parsed, units)
idx <- cbind(
apply(parsed, 2, first_positive),
seq_len(length(ms))
)
tmp <- paste0("~", parsed2[idx])
# handle NAs
tmp[is.na(parsed2[idx])] <- NA_character_
tmp
} else {
## Exact for small ones
exact <- paste0(ceiling(ms), "ms")
exact[is.na(ms)] <- NA_character_
## Approximate for others, in seconds
merge_pieces <- function(pieces) {
## handle NAs
if (all(is.na(pieces))) {
return(NA_character_)
}
## handle non-NAs
paste0(
if (pieces[1]) paste0(pieces[1], "d "),
if (pieces[2]) paste0(pieces[2], "h "),
if (pieces[3]) paste0(pieces[3], "m "),
if (pieces[4]) paste0(pieces[4], "s ")
)
}
approx <- trim(apply(parsed, 2, merge_pieces))
ifelse(ms < 1000, exact, approx)
}
}
pretty_sec <- function(sec, compact = FALSE) {
pretty_ms(sec * 1000, compact = compact)
}
pretty_dt <- function(dt, compact = FALSE) {
assert_diff_time(dt)
units(dt) <- "secs"
pretty_sec(as.vector(dt), compact = compact)
}
structure(
list(
.internal = environment(),
pretty_ms = pretty_ms,
pretty_sec = pretty_sec,
pretty_dt = pretty_dt
),
class = c("standalone_time", "standalone")
)
})