Skip to content

Fix label formatting bug in warning #6483

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

Merged
merged 3 commits into from
Jun 3, 2025
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
11 changes: 9 additions & 2 deletions R/labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/_snaps/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
# warnings are thrown for unknown labels

Ignoring unknown labels:
* `foo = "bar"`
* foo : "i don't exist"
* bar : <function>
* qux : "expression(me * neither)"

# plot.tag.position rejects invalid input

Expand Down
8 changes: 7 additions & 1 deletion tests/testthat/test-labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

Expand Down