Skip to content

Commit

Permalink
Restore old guides in guides() (#6167)
Browse files Browse the repository at this point in the history
* Early exit earlier

* fix `is.guide()` -> `inherits()`

* explicitly test `guides()` adds old S3 guides properly
  • Loading branch information
teunbrand authored Jan 21, 2025
1 parent 50b7d30 commit 3d1102c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 6 additions & 8 deletions R/guides-.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ NULL
#' }
guides <- function(...) {
args <- list2(...)
if (length(args) > 0) {
if (is.list(args[[1]]) && !is.guide(args[[1]])) args <- args[[1]]
args <- rename_aes(args)
# If there are no guides do nothing
if (length(args) == 0) {
return(NULL)
}

if (is.list(args[[1]]) && !inherits(args[[1]], "guide")) args <- args[[1]]
args <- rename_aes(args)

idx_false <- vapply(args, isFALSE, FUN.VALUE = logical(1L))
if (isTRUE(any(idx_false))) {
deprecate_warn0("3.3.4", "guides(`<scale>` = 'cannot be `FALSE`. Use \"none\" instead')")
Expand All @@ -84,11 +87,6 @@ guides <- function(...) {
return(guides_list(guides = args))
}

# If there are no guides, do nothing
if (length(args) == 0) {
return(NULL)
}

# Raise warning about unnamed guides
nms <- names(args)
if (is.null(nms)) {
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-guides.R
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,16 @@ test_that("old S3 guides can be implemented", {

withr::local_environment(my_env)

my_guides <- guides(x = guide_circle())
expect_length(my_guides$guides, 1)
expect_s3_class(my_guides$guides[[1]], "guide")

expect_snapshot_warning(
expect_doppelganger(
"old S3 guide drawing a circle",
ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
guides(x = "circle")
my_guides
)
)
})
Expand Down

0 comments on commit 3d1102c

Please sign in to comment.