Skip to content

Commit

Permalink
Roll back updates to update_geom/stat_defaults() (tidyverse#5837)
Browse files Browse the repository at this point in the history
* Revert "Update to `update_*_defaults()` (tidyverse#5781)"

This reverts commit 60407ac.

* remove news entry
  • Loading branch information
teunbrand committed Apr 18, 2024
1 parent 9243e2f commit be4b7b8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 63 deletions.
2 changes: 0 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

* When facets coerce the faceting variables to factors, the 'ordered' class
is dropped (@teunbrand, #5666).
* `update_geom_defaults()` and `update_stat_defaults()` have a reset mechanism
when using `new = NULL` and invisible return the previous defaults (#4993).
* `coord_map()` and `coord_polar()` throw informative warnings when used
with the guide system (#5707).
* When passing a function to `stat_contour(breaks)`, that function is used to
Expand Down
54 changes: 17 additions & 37 deletions R/geom-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#' @param stat,geom Name of geom/stat to modify (like `"point"` or
#' `"bin"`), or a Geom/Stat object (like `GeomPoint` or
#' `StatBin`).
#' @param new One of the following:
#' * A named list of aesthetics to serve as new defaults.
#' * `NULL` to reset the defaults.
#' @param new Named list of aesthetics.
#' @keywords internal
#' @export
#' @examples
Expand All @@ -18,7 +16,7 @@
#' ggplot(mtcars, aes(mpg, wt)) + geom_point()
#'
#' # reset default
#' update_geom_defaults("point", NULL)
#' update_geom_defaults("point", aes(color = "black"))
#'
#'
#' # updating a stat's default aesthetic settings
Expand All @@ -31,45 +29,27 @@
#' geom_function(fun = dnorm, color = "red")
#'
#' # reset default
#' update_stat_defaults("bin", NULL)
#' update_stat_defaults("bin", aes(y = after_stat(count)))
#'
#' @rdname update_defaults
update_geom_defaults <- function(geom, new) {
update_defaults(geom, "Geom", new, env = parent.frame())
g <- check_subclass(geom, "Geom", env = parent.frame())
old <- g$default_aes
new <- rename_aes(new)
new_names_order <- unique(c(names(old), names(new)))
new <- defaults(new, old)[new_names_order]
g$default_aes[names(new)] <- new
invisible()
}

#' @rdname update_defaults
#' @export
update_stat_defaults <- function(stat, new) {
update_defaults(stat, "Stat", new, env = parent.frame())
}

cache_defaults <- new_environment()

update_defaults <- function(name, subclass, new, env = parent.frame()) {
obj <- check_subclass(name, subclass, env = env)
index <- snake_class(obj)

if (is.null(new)) { # Reset from cache

old <- cache_defaults[[index]]
if (!is.null(old)) {
new <- update_defaults(name, subclass, new = old, env = env)
}
invisible(new)

} else { # Update default aesthetics

old <- obj$default_aes
# Only update cache the first time defaults are changed
if (!exists(index, envir = cache_defaults)) {
cache_defaults[[index]] <- old
}
new <- rename_aes(new)
name_order <- unique(c(names(old), names(new)))
new <- defaults(new, old)[name_order]
obj$default_aes[names(new)] <- new
invisible(old)

}
g <- check_subclass(stat, "Stat", env = parent.frame())
old <- g$default_aes
new <- rename_aes(new)
new_names_order <- unique(c(names(old), names(new)))
new <- defaults(new, old)[new_names_order]
g$default_aes[names(new)] <- new
invisible()
}
10 changes: 3 additions & 7 deletions man/update_defaults.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 3 additions & 17 deletions tests/testthat/test-geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,7 @@ test_that("aesthetic checking in geom throws correct errors", {
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")
})


test_that("updating geom aesthetic defaults preserves class and order", {

Expand All @@ -37,7 +23,7 @@ test_that("updating geom aesthetic defaults preserves class and order", {

expect_equal(updated_defaults, intended_defaults)

update_geom_defaults("point", NULL)
update_geom_defaults("point", original_defaults)

})

Expand All @@ -60,6 +46,6 @@ test_that("updating stat aesthetic defaults preserves class and order", {

expect_equal(updated_defaults, intended_defaults)

update_stat_defaults("bin", NULL)
update_stat_defaults("bin", original_defaults)

})

0 comments on commit be4b7b8

Please sign in to comment.