Skip to content

Try coerce ordered in vec_rbind0() #5139

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 10 commits into from
Jan 10, 2023
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* Using two ordered factors as facetting variables in
`facet_grid(..., as.table = FALSE)` now throws a warning instead of an
error (@teunbrand, #5109).
* Added `scale_linewidth_manual()` and `scale_linewidth_identity()` to support
the `linewidth` aesthetic (@teunbrand, #5050).
* Automatic breaks in `scale_*_binned()` should no longer be out-of-bounds,
Expand Down
3 changes: 2 additions & 1 deletion R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ with_ordered_restart <- function(expr, .call) {
restart <- TRUE
if (is.ordered(x)) {
x <- factor(as.character(x), levels = levels(x))
} else {
}
if (is.ordered(y)) {
y <- factor(as.character(y), levels = levels(y))
}
} else if (is.character(x) || is.character(y)) {
Expand Down
31 changes: 31 additions & 0 deletions tests/testthat/test-utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,34 @@ test_that("cut_*() checks its input and output", {
test_that("interleave() checks the vector lengths", {
expect_snapshot_error(interleave(1:4, numeric()))
})

test_that("vec_rbind0 can combined ordered factors", {

withr::local_options(lifecycle_verbosity = "warning")

# Ideally code below throws just 1 warning (the <ordered> and <ordered> one)
# However, it was technically challenging to reduce the numbers of warnings
# See #5139 for more details

expect_warning(
expect_warning(
expect_warning(
{
test <- vec_rbind0(
data_frame0(a = factor(c("A", "B"), ordered = TRUE)),
data_frame0(a = factor(c("B", "C"), ordered = TRUE))
)
},
"<ordered> and <ordered>", class = "lifecycle_warning_deprecated"
),
"<ordered> and <factor>", class = "lifecycle_warning_deprecated"
),
"<ordered> and <factor>", class = "lifecycle_warning_deprecated"
)

# Should be <factor> not <ordered/factor>, hence the 'exact'
expect_s3_class(test$a, "factor", exact = TRUE)
# Test levels are combined sensibly
expect_equal(levels(test$a), c("A", "B", "C"))

})