From 91a6d25abc43971530dbdcaa80da045960ce215b Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 28 Oct 2024 20:17:34 +0100 Subject: [PATCH 1/3] Early exit earlier --- R/guides-.R | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/R/guides-.R b/R/guides-.R index d250c78025..08c2b68243 100644 --- a/R/guides-.R +++ b/R/guides-.R @@ -71,6 +71,9 @@ guides <- function(...) { 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) } idx_false <- vapply(args, isFALSE, FUN.VALUE = logical(1L)) @@ -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)) { From c850c6d9b169a69496af8592718dc513f7332bcf Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 28 Oct 2024 20:18:02 +0100 Subject: [PATCH 2/3] fix `is.guide()` -> `inherits()` --- R/guides-.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/guides-.R b/R/guides-.R index 08c2b68243..fce682a55f 100644 --- a/R/guides-.R +++ b/R/guides-.R @@ -68,14 +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(`` = 'cannot be `FALSE`. Use \"none\" instead')") From 530a5bf5a7862cf1452dd16e0ce22a113e0082aa Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 29 Oct 2024 09:06:07 +0100 Subject: [PATCH 3/3] explicitly test `guides()` adds old S3 guides properly --- tests/testthat/test-guides.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-guides.R b/tests/testthat/test-guides.R index 23df1f75e2..0613d13ccc 100644 --- a/tests/testthat/test-guides.R +++ b/tests/testthat/test-guides.R @@ -1300,12 +1300,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 ) ) })