diff --git a/R/scale-type.R b/R/scale-type.R index 6b292b0e18..921847bf5e 100644 --- a/R/scale-type.R +++ b/R/scale-type.R @@ -1,4 +1,8 @@ -find_scale <- function(aes, x, env) { +find_scale <- function(aes, x, env = parent.frame()) { + if (is.null(x)) { + return(NULL) + } + type <- scale_type(x) candidates <- paste("scale", aes, type, sep = "_") diff --git a/R/scales-.r b/R/scales-.r index 79a3344cae..c185cf95df 100644 --- a/R/scales-.r +++ b/R/scales-.r @@ -97,6 +97,7 @@ scales_add_defaults <- function(scales, data, aesthetics, env) { if (is.null(new_aesthetics)) return() datacols <- lapply(aesthetics[new_aesthetics], rlang::eval_tidy, data = data) + datacols <- compact(datacols) for (aes in names(datacols)) { scales$add(find_scale(aes, datacols[[aes]], env)) diff --git a/tests/testthat/test-scale-type.R b/tests/testthat/test-scale-type.R new file mode 100644 index 0000000000..e5b145b1f1 --- /dev/null +++ b/tests/testthat/test-scale-type.R @@ -0,0 +1,12 @@ +context("test-scale-type.R") + +test_that("no scale for NULL aesthetic", { + expect_equal(find_scale("colour", NULL), NULL) +}) + +test_that("message + continuous for unknown type", { + x <- structure(1:10, class = "ggplot2_foo") + + expect_message(scale <- find_scale("colour", x), "ggplot2_foo") + expect_s3_class(scale, "ScaleContinuous") +})