From 2c8b098cb1b22a90b7191ed248df6224d9781aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Tue, 24 Dec 2024 12:17:22 +0100 Subject: [PATCH] docs: Define `type_sum.accel()` only in help page to avoid silent errors when loading (#721) --- R/type.R | 6 +++++- R/zzz.R | 12 ------------ man/format_type_sum.Rd | 6 +++++- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/R/type.R b/R/type.R index 0bb50dabf..6bff8a4a6 100644 --- a/R/type.R +++ b/R/type.R @@ -76,7 +76,7 @@ format_full_pillar_type <- function(x) { #' @export #' @examples #' # Default method: show the type with angle brackets -#' format_type_sum(1, NULL) +#' writeLines(format_type_sum("dbl", width = NULL)) #' pillar(1) #' #' # AsIs method: show the type without angle brackets @@ -84,6 +84,10 @@ format_full_pillar_type <- function(x) { #' I("kg m/s^2") #' } #' +#' # Typically done through NAMESPACE +#' # (perhaps with an @export directive in roxygen2) +#' registerS3method("type_sum", "accel", type_sum.accel) +#' #' accel <- structure(9.81, class = "accel") #' pillar(accel) format_type_sum <- function(x, width, ...) { diff --git a/R/zzz.R b/R/zzz.R index 6dc6257c1..01b08d9ee 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -36,13 +36,6 @@ #' @importFrom cli symbol ansi_strwrap NULL -# https://github.com/r-lib/pkgdown/issues/1540 -if (Sys.getenv("IN_PKGDOWN") != "") { - type_sum.accel <- function(x) { - I("kg m/s^2") - } -} - # nolint start .onLoad <- function(libname, pkgname) { # nolint end @@ -67,11 +60,6 @@ if (Sys.getenv("IN_PKGDOWN") != "") { debug_info() } - # https://github.com/r-lib/pkgdown/issues/1540 - if (Sys.getenv("IN_PKGDOWN") != "") { - s3_register("pillar::type_sum", "accel") - } - invisible() } diff --git a/man/format_type_sum.Rd b/man/format_type_sum.Rd index 2864f31ee..e66c9e2a6 100644 --- a/man/format_type_sum.Rd +++ b/man/format_type_sum.Rd @@ -33,7 +33,7 @@ For even more control over the formatting, implement your own method. } \examples{ # Default method: show the type with angle brackets -format_type_sum(1, NULL) +writeLines(format_type_sum("dbl", width = NULL)) pillar(1) # AsIs method: show the type without angle brackets @@ -41,6 +41,10 @@ type_sum.accel <- function(x) { I("kg m/s^2") } +# Typically done through NAMESPACE +# (perhaps with an @export directive in roxygen2) +registerS3method("type_sum", "accel", type_sum.accel) + accel <- structure(9.81, class = "accel") pillar(accel) }