Description
Problem
The issue emerged from a reverse dependency check 9f80c8c and affects an estimated 64 packages.
The issue comes from us transitioning the labels class to S7 and delaying the imputation of the default labels to the ggplot_build()
step. It is the same as #6290. The issue comes in three forms:
- Testing of the layer content. It will no longer work because we impute default labels later. Here are some examples:
expect_equal(p$labels$colour, "col")
expect_length(p$labels)
expect_named(p$labels, c("x", "y", "colour"))
expect_equal(names(p$labels), c("x", "y", "colour"))
- Testing of the layer structure. It will no longer work because we have moved to S7.
expect_type(p$labels, "list")
expect_s3_class(p$labels, "labels")
- Misconstructing labels. Because we are now more strict with labels, we detect more often when there are problems.
# labels should be named
labs("title")
# labels should not have duplicate names
labs(x = "foo", x = "bar")
Solution
For each of these 3 forms there are solutions.
For (1), you can use the get_labs()
function. There are two notable differences that might violate test assumptions.
- If you override the label in the scale or guide,
get_labs()
will get the final label. - If you provide a label for a variable that is not used, it will not be returned.
labels <- get_labs(p)
expect_equal(labels$colour, "col")
expect_length(labels)
expect_named(labels, c("x", "y", "colour"))
expect_equal(names(labels), c("x", "y", "colour"))
For (2), we don't have a dedicated is_labels()
function, so you'd have to test with inherits()
. The recommended way to test is using this:
expect_true(inherits(p$labels, c("ggplot2::labels", "labels"))
# In the future; after this release
expect_s7_class(p$labels, class_labels)
For (3), you should just prevent errors in your labels.
labs(title = "title")
labs(x = "bar")
Unfortunately, this is not something that is straightforward to fix in ggplot2, so this probably will have to be updated in the reverse dependencies.