diff --git a/NAMESPACE b/NAMESPACE index 542a24d..b0c6a4a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,9 +18,16 @@ S3method(as_bench_bytes,numeric) S3method(as_bench_time,bench_time) S3method(as_bench_time,default) S3method(as_bench_time,numeric) +S3method(dplyr::filter,bench_mark) +S3method(dplyr::group_by,bench_mark) S3method(format,bench_bytes) S3method(format,bench_expr) S3method(format,bench_time) +S3method(ggplot2::autoplot,bench_mark) +S3method(ggplot2::scale_type,bench_bytes) +S3method(ggplot2::scale_type,bench_expr) +S3method(ggplot2::scale_type,bench_time) +S3method(knitr::knit_print,bench_mark) S3method(max,bench_bytes) S3method(max,bench_time) S3method(mean,bench_time) @@ -37,9 +44,12 @@ S3method(rbind,bench_mark) S3method(sum,bench_bytes) S3method(sum,bench_time) S3method(summary,bench_mark) +S3method(tidyr::unnest,bench_mark) S3method(type_sum,bench_bytes) S3method(type_sum,bench_expr) S3method(type_sum,bench_time) +S3method(vctrs::vec_proxy,bench_expr) +S3method(vctrs::vec_restore,bench_expr) export(as_bench_bytes) export(as_bench_mark) export(as_bench_time) diff --git a/R/autoplot.R b/R/autoplot.R index a0290cf..339e318 100644 --- a/R/autoplot.R +++ b/R/autoplot.R @@ -45,6 +45,7 @@ #' autoplot("violin") #' } #' } +#' @exportS3Method ggplot2::autoplot autoplot.bench_mark <- function(object, type = c("beeswarm", "jitter", "ridge", "boxplot", "violin"),...) { diff --git a/R/bytes.R b/R/bytes.R index 9e59dc2..45fad4e 100644 --- a/R/bytes.R +++ b/R/bytes.R @@ -181,6 +181,7 @@ bench_bytes_trans <- function(base = 2) { scales::log_breaks(base = base), domain = c(1e-100, Inf)) } +#' @exportS3Method ggplot2::scale_type scale_type.bench_bytes <- function(x) "bench_bytes" #' Position scales for bench_time data diff --git a/R/expression.R b/R/expression.R index 8ea0832..57baab1 100644 --- a/R/expression.R +++ b/R/expression.R @@ -34,7 +34,7 @@ type_sum.bench_expr <- function(x) { new_bench_expr(new_x) } -# @export +#' @exportS3Method vctrs::vec_proxy vec_proxy.bench_expr <- function(x, ...) { desc <- attr(x, "description") attributes(x) <- NULL @@ -42,7 +42,7 @@ vec_proxy.bench_expr <- function(x, ...) { vctrs::new_data_frame(out, n = length(x)) } -# @export +#' @exportS3Method vctrs::vec_restore vec_restore.bench_expr <- function(x, to, ...) { new_bench_expr(x$x, x$desc) } @@ -55,6 +55,7 @@ pillar_shaft.bench_expr <- function(x, ...) { pillar_shaft(as.character(x), ...) } +#' @exportS3Method ggplot2::scale_type scale_type.bench_expr <- function(x) { "bench_expr" } diff --git a/R/import-standalone-s3-register.R b/R/import-standalone-s3-register.R deleted file mode 100644 index d165e18..0000000 --- a/R/import-standalone-s3-register.R +++ /dev/null @@ -1,187 +0,0 @@ -# Standalone file: do not edit by hand -# Source: -# ---------------------------------------------------------------------- -# -# --- -# repo: r-lib/rlang -# file: standalone-s3-register.R -# last-updated: 2022-08-29 -# license: https://unlicense.org -# --- -# -# nocov start - -#' Register a method for a suggested dependency -#' -#' Generally, the recommended way to register an S3 method is to use the -#' `S3Method()` namespace directive (often generated automatically by the -#' `@export` roxygen2 tag). However, this technique requires that the generic -#' be in an imported package, and sometimes you want to suggest a package, -#' and only provide a method when that package is loaded. `s3_register()` -#' can be called from your package's `.onLoad()` to dynamically register -#' a method only if the generic's package is loaded. -#' -#' For R 3.5.0 and later, `s3_register()` is also useful when demonstrating -#' class creation in a vignette, since method lookup no longer always involves -#' the lexical scope. For R 3.6.0 and later, you can achieve a similar effect -#' by using "delayed method registration", i.e. placing the following in your -#' `NAMESPACE` file: -#' -#' ``` -#' if (getRversion() >= "3.6.0") { -#' S3method(package::generic, class) -#' } -#' ``` -#' -#' @section Usage in other packages: -#' To avoid taking a dependency on vctrs, you copy the source of -#' [`s3_register()`](https://github.com/r-lib/rlang/blob/main/R/standalone-s3-register.R) -#' into your own package. It is licensed under the permissive -#' [unlicense](https://choosealicense.com/licenses/unlicense/) to make it -#' crystal clear that we're happy for you to do this. There's no need to include -#' the license or even credit us when using this function. -#' -#' @param generic Name of the generic in the form `"pkg::generic"`. -#' @param class Name of the class -#' @param method Optionally, the implementation of the method. By default, -#' this will be found by looking for a function called `generic.class` -#' in the package environment. -#' @examples -#' # A typical use case is to dynamically register tibble/pillar methods -#' # for your class. That way you avoid creating a hard dependency on packages -#' # that are not essential, while still providing finer control over -#' # printing when they are used. -#' -#' .onLoad <- function(...) { -#' s3_register("pillar::pillar_shaft", "vctrs_vctr") -#' s3_register("tibble::type_sum", "vctrs_vctr") -#' } -#' @keywords internal -#' @noRd -s3_register <- function(generic, class, method = NULL) { - stopifnot(is.character(generic), length(generic) == 1) - stopifnot(is.character(class), length(class) == 1) - - pieces <- strsplit(generic, "::")[[1]] - stopifnot(length(pieces) == 2) - package <- pieces[[1]] - generic <- pieces[[2]] - - caller <- parent.frame() - - get_method_env <- function() { - top <- topenv(caller) - if (isNamespace(top)) { - asNamespace(environmentName(top)) - } else { - caller - } - } - get_method <- function(method) { - if (is.null(method)) { - get(paste0(generic, ".", class), envir = get_method_env()) - } else { - method - } - } - - register <- function(...) { - envir <- asNamespace(package) - - # Refresh the method each time, it might have been updated by - # `devtools::load_all()` - 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) - )) - } - } - - # Always register hook in case package is later unloaded & reloaded - setHook(packageEvent(package, "onLoad"), function(...) { - register() - }) - - # For compatibility with R < 4.1.0 where base isn't locked - is_sealed <- function(pkg) { - identical(pkg, "base") || environmentIsLocked(asNamespace(pkg)) - } - - # Avoid registration failures during loading (pkgload or regular). - # Check that environment is locked because the registering package - # might be a dependency of the package that exports the generic. In - # that case, the exports (and the generic) might not be populated - # yet (#1225). - if (isNamespaceLoaded(package) && is_sealed(package)) { - register() - } - - invisible() -} - -.rlang_s3_register_compat <- function(fn, try_rlang = TRUE) { - # Compats that behave the same independently of rlang's presence - out <- switch( - fn, - is_installed = return(function(pkg) requireNamespace(pkg, quietly = TRUE)) - ) - - # Only use rlang if it is fully loaded (#1482) - if (try_rlang && - requireNamespace("rlang", quietly = TRUE) && - environmentIsLocked(asNamespace("rlang"))) { - switch( - fn, - is_interactive = return(rlang::is_interactive) - ) - - # Make sure rlang knows about "x" and "i" bullets - if (utils::packageVersion("rlang") >= "0.4.2") { - switch( - fn, - abort = return(rlang::abort), - warn = return((rlang::warn)), - inform = return(rlang::inform) - ) - } - } - - # Fall back to base compats - - is_interactive_compat <- function() { - opt <- getOption("rlang_interactive") - if (!is.null(opt)) { - opt - } else { - interactive() - } - } - - format_msg <- function(x) paste(x, collapse = "\n") - switch( - fn, - is_interactive = return(is_interactive_compat), - abort = return(function(msg) stop(format_msg(msg), call. = FALSE)), - warn = return(function(msg) warning(format_msg(msg), call. = FALSE)), - inform = return(function(msg) message(format_msg(msg))) - ) - - stop(sprintf("Internal error in rlang shims: Unknown function `%s()`.", fn)) -} - -# nocov end diff --git a/R/mark.R b/R/mark.R index 896cb81..0b03223 100644 --- a/R/mark.R +++ b/R/mark.R @@ -322,6 +322,7 @@ parse_allocations <- function(filename) { #' #' @param options A list of knitr chunk options set in the currently evaluated #' chunk. +#' @exportS3Method knitr::knit_print knit_print.bench_mark <- function(x, ..., options) { if (!isTRUE(options$bench.all_columns)) { x <- x[!colnames(x) %in% data_cols] @@ -339,6 +340,7 @@ parse_gc <- function(x) { utils::globalVariables(c("time", "gc")) +#' @exportS3Method tidyr::unnest unnest.bench_mark <- function(data, ...) { if (inherits(data[["expression"]], "bench_expr")) { data[["expression"]] <- as.character(data[["expression"]]) @@ -376,12 +378,14 @@ rbind.bench_mark <- function(..., deparse.level = 1) { res } +#' @exportS3Method dplyr::filter filter.bench_mark <- function(.data, ...) { dots <- rlang::quos(...) idx <- Reduce(`&`, lapply(dots, rlang::eval_tidy, data = .data)) .data[idx, ] } +#' @exportS3Method dplyr::group_by group_by.bench_mark <- function(.data, ...) { bench_mark(NextMethod()) } diff --git a/R/time.R b/R/time.R index 9159edb..4230204 100644 --- a/R/time.R +++ b/R/time.R @@ -222,6 +222,7 @@ bench_time_trans <- function(base = 10) { scales::log_breaks(base = base), domain = c(1e-100, Inf)) } +#' @exportS3Method ggplot2::scale_type scale_type.bench_time <- function(x) "bench_time" #' Position scales for bench_time data diff --git a/R/zzz.R b/R/zzz.R deleted file mode 100644 index d6041e7..0000000 --- a/R/zzz.R +++ /dev/null @@ -1,17 +0,0 @@ -#nocov start -.onLoad <- function(...) { - s3_register("tidyr::unnest", "bench_mark") - s3_register("dplyr::filter", "bench_mark") - s3_register("dplyr::group_by", "bench_mark") - s3_register("ggplot2::autoplot", "bench_mark") - - s3_register("ggplot2::scale_type", "bench_expr") - s3_register("ggplot2::scale_type", "bench_time") - s3_register("ggplot2::scale_type", "bench_bytes") - - s3_register("knitr::knit_print", "bench_mark") - - s3_register("vctrs::vec_proxy", "bench_expr") - s3_register("vctrs::vec_restore", "bench_expr") -} -#nocov end diff --git a/man/autoplot.bench_mark.Rd b/man/autoplot.bench_mark.Rd index 0f2be54..bd148af 100644 --- a/man/autoplot.bench_mark.Rd +++ b/man/autoplot.bench_mark.Rd @@ -5,7 +5,7 @@ \alias{plot.bench_mark} \title{Autoplot method for bench_mark objects} \usage{ -autoplot.bench_mark( +\method{autoplot}{bench_mark}( object, type = c("beeswarm", "jitter", "ridge", "boxplot", "violin"), ... diff --git a/man/knit_print.bench_mark.Rd b/man/knit_print.bench_mark.Rd index 45584b0..4f36446 100644 --- a/man/knit_print.bench_mark.Rd +++ b/man/knit_print.bench_mark.Rd @@ -4,7 +4,7 @@ \alias{knit_print.bench_mark} \title{Custom printing function for \code{bench_mark} objects in knitr documents} \usage{ -knit_print.bench_mark(x, ..., options) +\method{knit_print}{bench_mark}(x, ..., options) } \arguments{ \item{x}{An R object to be printed}