diff --git a/R/labels.R b/R/labels.R index 309f38902e..09f0490759 100644 --- a/R/labels.R +++ b/R/labels.R @@ -80,10 +80,17 @@ setup_plot_labels <- function(plot, layers, data) { extra_labels <- setdiff(names(plot_labels), known_labels) if (length(extra_labels) > 0) { - extra_labels <- paste0( - "{.code ", extra_labels, " = \"", plot_labels[extra_labels], "\"}" + + warn_labels <- plot_labels[extra_labels] + warn_labels <- ifelse( + vapply(warn_labels, is.function, logical(1)), + "{.cls function}", + paste0("{.val ", warn_labels, "}") ) + + extra_labels <- paste0("{.field ", extra_labels, "} : ", warn_labels) names(extra_labels) <- rep("*", length(extra_labels)) + cli::cli_warn(c( "Ignoring unknown labels:", extra_labels diff --git a/tests/testthat/_snaps/labels.md b/tests/testthat/_snaps/labels.md index 2a4bd75ff4..dcbcfe974f 100644 --- a/tests/testthat/_snaps/labels.md +++ b/tests/testthat/_snaps/labels.md @@ -15,7 +15,9 @@ # warnings are thrown for unknown labels Ignoring unknown labels: - * `foo = "bar"` + * foo : "i don't exist" + * bar : + * qux : "expression(me * neither)" # plot.tag.position rejects invalid input diff --git a/tests/testthat/test-labels.R b/tests/testthat/test-labels.R index 4befce8af6..be947ee1c9 100644 --- a/tests/testthat/test-labels.R +++ b/tests/testthat/test-labels.R @@ -110,7 +110,13 @@ test_that("get_alt_text checks dots", { }) test_that("warnings are thrown for unknown labels", { - p <- ggplot(mtcars, aes(mpg, disp)) + geom_point() + labs(foo = 'bar') + p <- ggplot(mtcars, aes(mpg, disp)) + + geom_point() + + labs( + foo = "i don't exist", + bar = function(x) "i don't exist either", + qux = expression(me * neither) + ) expect_snapshot_warning(ggplot_build(p)) })