From 6cf19b0204eee97175d6bf5066cadb79c92811b1 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Thu, 8 Dec 2022 22:05:05 +0100 Subject: [PATCH 1/6] Use cache to track original defaults --- R/geom-defaults.r | 61 +++++++++++++++++++++++++++++++++++------- man/update_defaults.Rd | 6 +++-- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/R/geom-defaults.r b/R/geom-defaults.r index 1fefa8c66e..edbd11a617 100644 --- a/R/geom-defaults.r +++ b/R/geom-defaults.r @@ -1,28 +1,69 @@ +geom_defaults_cache <- new.env(parent = emptyenv()) +stat_defaults_cache <- new.env(parent = emptyenv()) + #' Modify geom/stat aesthetic defaults for future plots #' #' @param stat,geom Name of geom/stat to modify (like `"point"` or #' `"bin"`), or a Geom/Stat object (like `GeomPoint` or #' `StatBin`). -#' @param new Named list of aesthetics. +#' @param new Named list of aesthetics. Alternatively, `NULL` to reset the +#' defaults. #' @keywords internal #' @export #' @examples #' update_geom_defaults("point", list(colour = "darkblue")) #' ggplot(mtcars, aes(mpg, wt)) + geom_point() -#' update_geom_defaults("point", list(colour = "black")) +#' # Reset defaults by using `new = NULL` +#' update_geom_defaults("point", NULL) #' @rdname update_defaults update_geom_defaults <- function(geom, new) { - g <- check_subclass(geom, "Geom", env = parent.frame()) - old <- g$default_aes - g$default_aes <- defaults(rename_aes(new), old) - invisible() + if (is.null(new)) { + + vec_assert(geom, character(), 1) + old <- geom_defaults_cache[[geom]] + if (!is.null(old)) { + new <- update_geom_defaults(geom, old) + env_unbind(geom_defaults_cache, geom) + } + invisible(new) + + } else { + + g <- check_subclass(geom, "Geom", env = parent.frame()) + old <- g$default_aes + # Only update cache the first time + if (!geom %in% ls(geom_defaults_cache)) { + geom_defaults_cache[[geom]] <- old + } + g$default_aes <- defaults(rename_aes(new), old) + invisible(old) + + } } #' @rdname update_defaults #' @export update_stat_defaults <- function(stat, new) { - g <- check_subclass(stat, "Stat", env = parent.frame()) - old <- g$default_aes - g$default_aes <- defaults(rename_aes(new), old) - invisible() + if (is.null(new)) { + + vec_assert(stat, character(), 1) + old <- stat_defaults_cache[[stat]] + if (!is.null(old)) { + new <- update_geom_defaults(stat, old) + env_unbind(geom_defaults_cache, stat) + } + invisible(new) + + } else { + + g <- check_subclass(stat, "Stat", env = parent.frame()) + old <- g$default_aes + # Only update cache the first time + if (!stat %in% ls(stat_defaults_cache)) { + stat_defaults_cache[[stat]] <- old + } + g$default_aes <- defaults(rename_aes(new), old) + invisible(old) + + } } diff --git a/man/update_defaults.Rd b/man/update_defaults.Rd index b3cc710eb7..b68cc05d32 100644 --- a/man/update_defaults.Rd +++ b/man/update_defaults.Rd @@ -10,7 +10,8 @@ update_geom_defaults(geom, new) update_stat_defaults(stat, new) } \arguments{ -\item{new}{Named list of aesthetics.} +\item{new}{Named list of aesthetics. Alternatively, \code{NULL} to reset the +defaults.} \item{stat, geom}{Name of geom/stat to modify (like \code{"point"} or \code{"bin"}), or a Geom/Stat object (like \code{GeomPoint} or @@ -22,6 +23,7 @@ Modify geom/stat aesthetic defaults for future plots \examples{ update_geom_defaults("point", list(colour = "darkblue")) ggplot(mtcars, aes(mpg, wt)) + geom_point() -update_geom_defaults("point", list(colour = "black")) +# Reset defaults by using `new = NULL` +update_geom_defaults("point", NULL) } \keyword{internal} From 3433f9aec6329587dab1ae4eaf0d8968dc9fd341 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Thu, 8 Dec 2022 22:05:12 +0100 Subject: [PATCH 2/6] Add unit test --- tests/testthat/test-geom-.R | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/testthat/test-geom-.R b/tests/testthat/test-geom-.R index 9436770838..c662485f40 100644 --- a/tests/testthat/test-geom-.R +++ b/tests/testthat/test-geom-.R @@ -5,3 +5,19 @@ test_that("aesthetic checking in geom throws correct errors", { aes <- list(a = 1:4, b = letters[1:4], c = TRUE, d = 1:2, e = 1:5) expect_snapshot_error(check_aesthetics(aes, 4)) }) + +test_that("geom defaults can be set and reset", { + l <- geom_point() + test <- l$geom$use_defaults(data_frame0()) + expect_equal(test$colour, "black") + + inv <- update_geom_defaults("point", list(colour = "red")) + test <- l$geom$use_defaults(data_frame0()) + expect_equal(test$colour, "red") + expect_equal(inv$colour, "black") + + inv <- update_geom_defaults("point", NULL) + test <- l$geom$use_defaults(data_frame0()) + expect_equal(test$colour, "black") + expect_equal(inv$colour, "red") +}) From 3c7aeb0aeecc6389f47cd3fd3516359fa31ee1ab Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Thu, 8 Dec 2022 22:06:44 +0100 Subject: [PATCH 3/6] Add NEWS bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 76bf5100a3..d13b674d4e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* `update_geom_defaults()` and `update_stat_defaults()` can now use `new = NULL` + to reset the original defaults (@teunbrand, #4993). * Fixed a regression in `geom_hex()` where aesthetics were replicated across bins (@thomasp85, #5037 and #5044) * Fixed spurious warning when `weight` aesthetic was used in `stat_smooth()` From 6483980c1ace6217e0945d3179761d5f46370255 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sun, 11 Dec 2022 12:05:58 +0100 Subject: [PATCH 4/6] Rename cache --- R/geom-defaults.r | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/R/geom-defaults.r b/R/geom-defaults.r index edbd11a617..17dc395245 100644 --- a/R/geom-defaults.r +++ b/R/geom-defaults.r @@ -1,5 +1,5 @@ -geom_defaults_cache <- new.env(parent = emptyenv()) -stat_defaults_cache <- new.env(parent = emptyenv()) +cache_geom_defaults <- new.env(parent = emptyenv()) +cache_stats_defaults <- new.env(parent = emptyenv()) #' Modify geom/stat aesthetic defaults for future plots #' @@ -20,10 +20,10 @@ update_geom_defaults <- function(geom, new) { if (is.null(new)) { vec_assert(geom, character(), 1) - old <- geom_defaults_cache[[geom]] + old <- cache_geom_defaults[[geom]] if (!is.null(old)) { new <- update_geom_defaults(geom, old) - env_unbind(geom_defaults_cache, geom) + env_unbind(cache_geom_defaults, geom) } invisible(new) @@ -32,8 +32,8 @@ update_geom_defaults <- function(geom, new) { g <- check_subclass(geom, "Geom", env = parent.frame()) old <- g$default_aes # Only update cache the first time - if (!geom %in% ls(geom_defaults_cache)) { - geom_defaults_cache[[geom]] <- old + if (!geom %in% ls(cache_geom_defaults)) { + cache_geom_defaults[[geom]] <- old } g$default_aes <- defaults(rename_aes(new), old) invisible(old) @@ -47,10 +47,10 @@ update_stat_defaults <- function(stat, new) { if (is.null(new)) { vec_assert(stat, character(), 1) - old <- stat_defaults_cache[[stat]] + old <- cache_stats_defaults[[stat]] if (!is.null(old)) { - new <- update_geom_defaults(stat, old) - env_unbind(geom_defaults_cache, stat) + new <- update_stat_defaults(stat, old) + env_unbind(cache_stats_defaults, stat) } invisible(new) @@ -59,8 +59,8 @@ update_stat_defaults <- function(stat, new) { g <- check_subclass(stat, "Stat", env = parent.frame()) old <- g$default_aes # Only update cache the first time - if (!stat %in% ls(stat_defaults_cache)) { - stat_defaults_cache[[stat]] <- old + if (!stat %in% ls(cache_stats_defaults)) { + cache_stats_defaults[[stat]] <- old } g$default_aes <- defaults(rename_aes(new), old) invisible(old) From a54e6f7ccc7f0384823fea00c2b6439e06043e78 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 8 May 2023 21:01:15 +0200 Subject: [PATCH 5/6] Better checking --- R/geom-defaults.R | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/R/geom-defaults.R b/R/geom-defaults.R index 7ad1775b1f..3b8d87f263 100644 --- a/R/geom-defaults.R +++ b/R/geom-defaults.R @@ -37,7 +37,12 @@ cache_stats_defaults <- new.env(parent = emptyenv()) update_geom_defaults <- function(geom, new) { if (is.null(new)) { - vec_assert(geom, character(), 1) + if (!inherits(geom, "Geom")) { + check_string(geom) + } else { + geom <- check_subclass(geom, "Geom", env = parent.frame()) + geom <- gsub("^geom_", "", snake_class(geom)) + } old <- cache_geom_defaults[[geom]] if (!is.null(old)) { new <- update_geom_defaults(geom, old) @@ -67,7 +72,12 @@ update_geom_defaults <- function(geom, new) { update_stat_defaults <- function(stat, new) { if (is.null(new)) { - vec_assert(stat, character(), 1) + if (!inherits(stat, "Stat")) { + check_string(stat) + } else { + stat <- check_subclass(stat, "Stat", env = parent.frame()) + stat <- gsub("^stat_", "", snake_class(stat)) + } old <- cache_stats_defaults[[stat]] if (!is.null(old)) { new <- update_stat_defaults(stat, old) From 46a6a6ac91c0a8989b8bb1343f52cac7f8950559 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 8 May 2023 21:01:30 +0200 Subject: [PATCH 6/6] Don't empty caches --- R/geom-defaults.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/geom-defaults.R b/R/geom-defaults.R index 3b8d87f263..375650f8e1 100644 --- a/R/geom-defaults.R +++ b/R/geom-defaults.R @@ -46,7 +46,6 @@ update_geom_defaults <- function(geom, new) { old <- cache_geom_defaults[[geom]] if (!is.null(old)) { new <- update_geom_defaults(geom, old) - env_unbind(cache_geom_defaults, geom) } invisible(new) @@ -81,7 +80,6 @@ update_stat_defaults <- function(stat, new) { old <- cache_stats_defaults[[stat]] if (!is.null(old)) { new <- update_stat_defaults(stat, old) - env_unbind(cache_stats_defaults, stat) } invisible(new)