Skip to content

Commit 0b601ee

Browse files
committed
Add breaks_cached() implementation
1 parent 93ff456 commit 0b601ee

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Collate:
109109
'backports.R'
110110
'bench.R'
111111
'bin.R'
112+
'breaks_cached.R'
112113
'coord-.R'
113114
'coord-cartesian-.R'
114115
'coord-fixed.R'

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ S3method(element_grob,element_blank)
2222
S3method(element_grob,element_line)
2323
S3method(element_grob,element_rect)
2424
S3method(element_grob,element_text)
25+
S3method(format,ggplot2_cached_breaks)
2526
S3method(format,ggproto)
2627
S3method(format,ggproto_method)
2728
S3method(fortify,"NULL")
@@ -106,6 +107,7 @@ S3method(predictdf,loess)
106107
S3method(print,element)
107108
S3method(print,ggplot)
108109
S3method(print,ggplot2_bins)
110+
S3method(print,ggplot2_cached_breaks)
109111
S3method(print,ggproto)
110112
S3method(print,ggproto_method)
111113
S3method(print,rel)
@@ -298,6 +300,7 @@ export(autoplot)
298300
export(benchplot)
299301
export(binned_scale)
300302
export(borders)
303+
export(breaks_cached)
301304
export(calc_element)
302305
export(check_device)
303306
export(combine_vars)

R/breaks_cached.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#' Caching scale breaks
2+
#'
3+
#' This helper caches the output of another breaks function the first time it is
4+
#' evaluated. All subsequent calls will return the same breaks vector
5+
#' regardless of the provided limits. In general this is not what you want
6+
#' since the breaks should change when the limits change. It is helpful in the
7+
#' specific case that you are using `follow.scale` on `stat_bin()` and related
8+
#' binning stats, because it ensures that the breaks are not recomputed after
9+
#' they are used to define the bin edges.
10+
#'
11+
#' @param breaks A function that takes the limits as input and returns breaks
12+
#' as output. See `ggplot2::continuous_scale` for details.
13+
#'
14+
#' @return A wrapped breaks function suitable for use with ggplot scales.
15+
#' @export
16+
breaks_cached <- function(breaks) {
17+
if (! rlang::is_function(breaks)) {
18+
cli::cli_abort("{.arg breaks} must be a function")
19+
}
20+
21+
cached <- ggplot2::ggproto(
22+
"BreaksCached", NULL,
23+
fn = breaks,
24+
cached = NULL,
25+
get_breaks = function(self, limits) {
26+
if (is.null(self$cached)) self$cached <- self$fn(limits)
27+
self$cached
28+
}
29+
)$get_breaks
30+
31+
class(cached) <- c("ggplot2_cached_breaks", class(cached))
32+
cached
33+
}
34+
35+
#' @export
36+
format.ggplot2_cached_breaks <- function(x, ...) {
37+
bc <- environment(x)$self
38+
inner <- environment(bc$fn)$f
39+
40+
paste0(
41+
"<cached breaks function>\n",
42+
ifelse(
43+
is.null(bc$cached),
44+
paste0(" ", format(inner), collapse = "\n"),
45+
paste0(" [", class(bc$cached), "] ", paste0(format(bc$cached), collapse = " "))
46+
)
47+
)
48+
}
49+
50+
#' @export
51+
print.ggplot2_cached_breaks <- function(x, ...) {
52+
cat(format(x), sep = "")
53+
}

man/breaks_cached.Rd

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)