Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to update_*_defaults() #5781

Merged
merged 6 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# ggplot2 (development version)

* `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: 37 additions & 17 deletions R/geom-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#' @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 One of the following:
#' * A named list of aesthetics to serve as new defaults.
#' * `NULL` to reset the defaults.
#' @keywords internal
#' @export
#' @examples
Expand All @@ -16,7 +18,7 @@
#' ggplot(mtcars, aes(mpg, wt)) + geom_point()
#'
#' # reset default
#' update_geom_defaults("point", aes(color = "black"))
#' update_geom_defaults("point", NULL)
#'
#'
#' # updating a stat's default aesthetic settings
Expand All @@ -29,27 +31,45 @@
#' geom_function(fun = dnorm, color = "red")
#'
#' # reset default
#' update_stat_defaults("bin", aes(y = after_stat(count)))
#' update_stat_defaults("bin", NULL)
#'
#' @rdname update_defaults
update_geom_defaults <- function(geom, new) {
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()
update_defaults(geom, "Geom", new, env = parent.frame())
}

#' @rdname update_defaults
#' @export
update_stat_defaults <- function(stat, new) {
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()
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)

}
}
10 changes: 7 additions & 3 deletions man/update_defaults.Rd

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

20 changes: 17 additions & 3 deletions tests/testthat/test-geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,21 @@ 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 @@ -23,7 +37,7 @@ test_that("updating geom aesthetic defaults preserves class and order", {

expect_equal(updated_defaults, intended_defaults)

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

})

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

expect_equal(updated_defaults, intended_defaults)

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

})
Loading