Skip to content

Commit f7da3b4

Browse files
authored
Fix label formatting bug in warning (#6483)
* handle warnings about function-labels * include in test * approve snapshot of updated test
1 parent 16c5029 commit f7da3b4

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

R/labels.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,17 @@ setup_plot_labels <- function(plot, layers, data) {
8080
extra_labels <- setdiff(names(plot_labels), known_labels)
8181

8282
if (length(extra_labels) > 0) {
83-
extra_labels <- paste0(
84-
"{.code ", extra_labels, " = \"", plot_labels[extra_labels], "\"}"
83+
84+
warn_labels <- plot_labels[extra_labels]
85+
warn_labels <- ifelse(
86+
vapply(warn_labels, is.function, logical(1)),
87+
"{.cls function}",
88+
paste0("{.val ", warn_labels, "}")
8589
)
90+
91+
extra_labels <- paste0("{.field ", extra_labels, "} : ", warn_labels)
8692
names(extra_labels) <- rep("*", length(extra_labels))
93+
8794
cli::cli_warn(c(
8895
"Ignoring unknown labels:",
8996
extra_labels

tests/testthat/_snaps/labels.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
# warnings are thrown for unknown labels
1616

1717
Ignoring unknown labels:
18-
* `foo = "bar"`
18+
* foo : "i don't exist"
19+
* bar : <function>
20+
* qux : "expression(me * neither)"
1921

2022
# plot.tag.position rejects invalid input
2123

tests/testthat/test-labels.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ test_that("get_alt_text checks dots", {
110110
})
111111

112112
test_that("warnings are thrown for unknown labels", {
113-
p <- ggplot(mtcars, aes(mpg, disp)) + geom_point() + labs(foo = 'bar')
113+
p <- ggplot(mtcars, aes(mpg, disp)) +
114+
geom_point() +
115+
labs(
116+
foo = "i don't exist",
117+
bar = function(x) "i don't exist either",
118+
qux = expression(me * neither)
119+
)
114120
expect_snapshot_warning(ggplot_build(p))
115121
})
116122

0 commit comments

Comments
 (0)