|
7 | 7 | #' @param edition An edition. Possible values currently include `"2026"` only. |
8 | 8 | #' Can be `NULL` (default) to unset an edition. |
9 | 9 | #' |
10 | | -#' @returns The previous `edition` value. This function is called for the side |
11 | | -#' effect of setting the edition though. |
| 10 | +#' @returns For `set_ggplot2_edition()`, the previous `edition` value and for |
| 11 | +#' `local_ggplot2_edition()`: `NULL`. Thesef unction are called for the side |
| 12 | +#' effect of setting the edition though. For `with_ggplot2_edition`, the |
| 13 | +#' result of the evaluation. For `get_ggplot2_edition()`, the currently |
| 14 | +#' active edition. |
| 15 | +#' |
12 | 16 | #' @export |
13 | 17 | #' @keywords internal |
14 | 18 | #' |
15 | 19 | #' @examples |
16 | 20 | #' # Setting an edition |
17 | 21 | #' set_ggplot2_edition(2026) |
18 | 22 | #' |
| 23 | +#' # Getting the edition |
| 24 | +#' get_ggplot2_edition() |
| 25 | +#' |
| 26 | +#' |
19 | 27 | #' # Unsetting an edition |
20 | 28 | #' set_ggplot2_edition() |
| 29 | +#' |
| 30 | +#' # Using withr-like scoping |
| 31 | +#' with_ggplot2_edition(2026, get_ggplot2_edition()) |
21 | 32 | set_ggplot2_edition <- function(edition = NULL) { |
22 | 33 | old <- ggplot_global$edition |
23 | 34 | ggplot_global$edition <- validate_edition(edition) |
24 | 35 | invisible(old) |
25 | 36 | } |
26 | 37 |
|
27 | | -local_ggplot2_edition <- function(edition, env = parent.frame()) { |
| 38 | +#' @export |
| 39 | +#' @param env An `environment` to use for scoping. |
| 40 | +#' @rdname set_ggplot2_edition |
| 41 | +local_ggplot2_edition <- function(edition = NULL, env = parent.frame()) { |
28 | 42 | old <- set_ggplot2_edition(edition) |
29 | 43 | withr::defer(set_ggplot2_edition(old), envir = env) |
30 | 44 | } |
31 | 45 |
|
32 | | -with_ggplot2_edition <- function(edition, code) { |
| 46 | +#' @export |
| 47 | +#' @param code Code to execute in the temporary environment. |
| 48 | +#' @rdname set_ggplot2_edition |
| 49 | +with_ggplot2_edition <- function(edition = NULL, code) { |
33 | 50 | local_ggplot2_edition(edition) |
34 | 51 | code |
35 | 52 | } |
36 | 53 |
|
| 54 | +#' @export |
| 55 | +#' @rdname set_ggplot2_edition |
37 | 56 | get_ggplot2_edition <- function() { |
38 | 57 | ggplot_global$edition[[1]] |
39 | 58 | } |
|
0 commit comments