Skip to content

Commit

Permalink
Reformat with air (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan authored Jan 16, 2025
1 parent 9d3ad55 commit a96f013
Show file tree
Hide file tree
Showing 20 changed files with 405 additions and 156 deletions.
36 changes: 24 additions & 12 deletions R/autoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
#' }
#' }
# Lazily registered in `.onLoad()`
autoplot.bench_mark <- function(object,
type = c("beeswarm", "jitter", "ridge", "boxplot", "violin"),...) {

autoplot.bench_mark <- function(
object,
type = c("beeswarm", "jitter", "ridge", "boxplot", "violin"),
...
) {
rlang::check_installed(c("ggplot2", "tidyr"), "for `autoplot()`.")

type <- match.arg(type)
Expand All @@ -69,8 +71,8 @@ autoplot.bench_mark <- function(object,
}
p <- ggplot2::ggplot(res)


switch(type,
switch(
type,
beeswarm = p <- p +
ggplot2::aes(.data$expression, .data$time, color = .data$gc) +
ggbeeswarm::geom_quasirandom(...) +
Expand All @@ -93,21 +95,26 @@ autoplot.bench_mark <- function(object,
violin = p <- p +
ggplot2::aes(.data$expression, .data$time) +
ggplot2::geom_violin(...) +
ggplot2::coord_flip())
ggplot2::coord_flip()
)

parameters <- setdiff(
colnames(object),
c("expression", summary_cols, data_cols, c("level0", "level1", "level2")))
c("expression", summary_cols, data_cols, c("level0", "level1", "level2"))
)

if (length(parameters) == 0) {
return(p)
}

if (length(parameters) == 2) {
return(p +
ggplot2::facet_grid(
paste0(parameters[[1]], "~", parameters[[2]]),
labeller = ggplot2::label_both))
return(
p +
ggplot2::facet_grid(
paste0(parameters[[1]], "~", parameters[[2]]),
labeller = ggplot2::label_both
)
)
}

p + ggplot2::facet_wrap(parameters, labeller = ggplot2::label_both)
Expand All @@ -117,7 +124,12 @@ autoplot.bench_mark <- function(object,
#' @param x A `bench_mark` object.
#' @param y Ignored, required for compatibility with the `plot()` generic.
#' @export
plot.bench_mark <- function(x, ..., type = c("beeswarm", "jitter", "ridge", "boxplot", "violin"), y) {
plot.bench_mark <- function(
x,
...,
type = c("beeswarm", "jitter", "ridge", "boxplot", "violin"),
y
) {
type <- match.arg(type)
ggplot2::autoplot(x, type = type, ...)
}
8 changes: 6 additions & 2 deletions R/bench_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
bench_time <- function(expr) {
stats::setNames(
as_bench_time(.Call(system_time_, substitute(expr), parent.frame())),
c("process", "real"))
c("process", "real")
)
}

#' @export
Expand Down Expand Up @@ -46,5 +47,8 @@ bench_memory <- function(expr) {

memory <- parse_allocations(f)

tibble::tibble(mem_alloc = bench_bytes(sum(memory$bytes, na.rm = TRUE)), memory = list(memory))
tibble::tibble(
mem_alloc = bench_bytes(sum(memory$bytes, na.rm = TRUE)),
memory = list(memory)
)
}
62 changes: 48 additions & 14 deletions R/bytes.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# This is mostly a copy of https://github.com/r-lib/fs/blob/0f5b6191935fe4c862d2e5003655e6c1669f4afd/R/fs_bytes.R
# If I end up needing this in a third package it should probably live in a package somewhere, maybe prettyunits?

byte_units <- c('B' = 1, 'K' = 1024, 'M' = 1024 ^ 2, 'G' = 1024 ^ 3, 'T' = 1024 ^ 4, 'P' = 1024 ^ 5, 'E' = 1024 ^ 6, 'Z' = 1024 ^ 7, 'Y' = 1024 ^ 8)
byte_units <- c(
'B' = 1,
'K' = 1024,
'M' = 1024^2,
'G' = 1024^3,
'T' = 1024^4,
'P' = 1024^5,
'E' = 1024^6,
'Z' = 1024^7,
'Y' = 1024^8
)

#' Human readable memory sizes
#'
Expand Down Expand Up @@ -42,7 +52,14 @@ setOldClass(c("bench_bytes", "numeric"), numeric())
#' @export
as_bench_bytes.default <- function(x) {
x <- as.character(x)
m <- captures(x, regexpr("^(?<size>[[:digit:].]+)\\s*(?<unit>[KMGTPEZY]?)i?[Bb]?$", x, perl = TRUE))
m <- captures(
x,
regexpr(
"^(?<size>[[:digit:].]+)\\s*(?<unit>[KMGTPEZY]?)i?[Bb]?$",
x,
perl = TRUE
)
)
m$unit[m$unit == ""] <- "B"
new_bench_bytes(unname(as.numeric(m$size) * byte_units[m$unit]))
}
Expand All @@ -59,7 +76,13 @@ as_bench_bytes.numeric <- function(x) {

# Adapted from https://github.com/gaborcsardi/prettyunits
#' @export
format.bench_bytes <- function(x, scientific = FALSE, digits = 3, drop0trailing = TRUE, ...) {
format.bench_bytes <- function(
x,
scientific = FALSE,
digits = 3,
drop0trailing = TRUE,
...
) {
nms <- names(x)

bytes <- unclass(x)
Expand All @@ -74,13 +97,19 @@ format.bench_bytes <- function(x, scientific = FALSE, digits = 3, drop0trailing
## NA and NaN bytes
res[is.na(bytes)] <- NA_real_
res[is.nan(bytes)] <- NaN
unit[is.na(bytes)] <- "" # Includes NaN as well
unit[is.na(bytes)] <- "" # Includes NaN as well

# Append an extra B to each unit
large_units <- unit %in% names(byte_units)[-1]
unit[large_units] <- paste0(unit[large_units], "B")

res <- format(res, scientific = scientific, digits = digits, drop0trailing = drop0trailing, ...)
res <- format(
res,
scientific = scientific,
digits = digits,
drop0trailing = drop0trailing,
...
)

stats::setNames(paste0(res, unit), nms)
}
Expand Down Expand Up @@ -120,13 +149,16 @@ max.bench_bytes <- function(x, ...) {

#' @export
# Adapted from Ops.numeric_version
Ops.bench_bytes <- function (e1, e2) {
Ops.bench_bytes <- function(e1, e2) {
if (nargs() == 1L) {
stop(sprintf("unary '%s' not defined for \"bench_bytes\" objects", .Generic),
call. = FALSE)
stop(
sprintf("unary '%s' not defined for \"bench_bytes\" objects", .Generic),
call. = FALSE
)
}

boolean <- switch(.Generic,
boolean <- switch(
.Generic,
`+` = TRUE,
`-` = TRUE,
`*` = TRUE,
Expand All @@ -138,10 +170,13 @@ Ops.bench_bytes <- function (e1, e2) {
`!=` = TRUE,
`<=` = TRUE,
`>=` = TRUE,
FALSE)
FALSE
)
if (!boolean) {
stop(sprintf("'%s' not defined for \"bench_bytes\" objects", .Generic),
call. = FALSE)
stop(
sprintf("'%s' not defined for \"bench_bytes\" objects", .Generic),
call. = FALSE
)
}
e1 <- as_bench_bytes(e1)
e2 <- as_bench_bytes(e2)
Expand All @@ -158,7 +193,6 @@ type_sum.bench_bytes <- function(x) {
"bch:byt"
}


#' Benchmark time transformation
#'
#' This both log transforms the times and formats the labels as a `bench_time`
Expand All @@ -179,7 +213,7 @@ bench_bytes_trans <- function(base = 2) {
)
}
trans <- function(x) log(as.numeric(x), base)
inv <- function(x) as_bench_bytes(base ^ as.numeric(x))
inv <- function(x) as_bench_bytes(base^as.numeric(x))

scales::trans_new(
name = paste0("bch:byt-", format(base)),
Expand Down
6 changes: 5 additions & 1 deletion R/expression.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ scale_y_bench_expr <- function(...) {
#' @rdname scale_bench_expr
#' @keywords internal
#' @export
scale_colour_bench_expr <- function(palette = scales::hue_pal(...), ..., aesthetics = "colour") {
scale_colour_bench_expr <- function(
palette = scales::hue_pal(...),
...,
aesthetics = "colour"
) {
sc <- ggplot2::discrete_scale(aesthetics, "bench_expr", palette, ...)
sc$transform <- as.character
sc
Expand Down
32 changes: 19 additions & 13 deletions R/import-standalone-s3-register.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,26 @@ s3_register <- function(generic, class, method = NULL) {
method_fn <- get_method(method)
stopifnot(is.function(method_fn))


# Only register if generic can be accessed
if (exists(generic, envir)) {
registerS3method(generic, class, method_fn, envir = envir)
} else if (identical(Sys.getenv("NOT_CRAN"), "true")) {
warn <- .rlang_s3_register_compat("warn")

warn(c(
sprintf(
"Can't find generic `%s` in package %s to register S3 method.",
generic,
package
),
"i" = "This message is only shown to developers using devtools.",
"i" = sprintf("Do you need to update %s to the latest version?", package)
))
warn(
c(
sprintf(
"Can't find generic `%s` in package %s to register S3 method.",
generic,
package
),
"i" = "This message is only shown to developers using devtools.",
"i" = sprintf(
"Do you need to update %s to the latest version?",
package
)
)
)
}
}

Expand Down Expand Up @@ -150,9 +154,11 @@ s3_register <- function(generic, class, method = NULL) {
)

# Only use rlang if it is fully loaded (#1482)
if (try_rlang &&
requireNamespace("rlang", quietly = TRUE) &&
environmentIsLocked(asNamespace("rlang"))) {
if (
try_rlang &&
requireNamespace("rlang", quietly = TRUE) &&
environmentIsLocked(asNamespace("rlang"))
) {
switch(
fn,
is_interactive = return(rlang::is_interactive)
Expand Down
1 change: 0 additions & 1 deletion R/load.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Get system load averages
#'
#' Uses OS system APIs to return the load average for the past 1, 5 and 15 minutes.
Expand Down
Loading

0 comments on commit a96f013

Please sign in to comment.