Skip to content

Commit

Permalink
throw warning when only one of two variables is AsIs
Browse files Browse the repository at this point in the history
  • Loading branch information
teunbrand committed Nov 27, 2024
1 parent e569974 commit b1d45c8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
21 changes: 21 additions & 0 deletions R/coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,24 @@ check_coord_limits <- function(
check_object(limits, is_vector, "a vector", arg = arg, call = call)
check_length(limits, 2L, arg = arg, call = call)
}

is_transform_immune <- function(data, coord_name) {
x <- inherits(data$x, "AsIs")
y <- inherits(data$y, "AsIs")
if (!(x || y)) {
# Neither variable is AsIs, so we need to transform
return(FALSE)
}
if (x && y) {
# Both variables are AsIs, so no need to transform
return(TRUE)
}
# We're now in the `xor(x, y)` case
var <- if (x) "x" else "y"
alt <- if (x) "y" else "x"
cli::cli_warn(
"{.fn {coord_name}} cannot respect the {.cls AsIs} class of {.var {var}} \\
when {.var {alt}} is not also {.cls AsIs}."
)
return(FALSE)
}
2 changes: 1 addition & 1 deletion R/coord-polar.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ CoordPolar <- ggproto("CoordPolar", Coord,
},

transform = function(self, data, panel_params) {
if (inherits(data$x, "AsIs") && inherits(data$y, "AsIs")) {
if (is_transform_immune(data, snake_class(self))) {
return(data)
}

Expand Down
2 changes: 1 addition & 1 deletion R/coord-radial.R
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
},

transform = function(self, data, panel_params) {
if (inherits(data$x, "AsIs") && inherits(data$y, "AsIs")) {
if (is_transform_immune(data, snake_class(self))) {
return(data)
}

Expand Down
2 changes: 1 addition & 1 deletion R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
},

transform = function(self, data, panel_params) {
if (inherits(data$x, "AsIs") && inherits(data$y, "AsIs")) {
if (is_transform_immune(data, snake_class(self))) {
return(data)
}

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/coord-polar.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
No appropriate placement found for `r_axis_inside`.
i Axis will be placed at panel edge.

# when both x and y are AsIs, they are not transformed

`coord_name()` cannot respect the <AsIs> class of `x` when `y` is not also <AsIs>.

9 changes: 9 additions & 0 deletions tests/testthat/test-coord-polar.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ test_that("when both x and y are AsIs, they are not transformed", {
location <- c(as.numeric(grob$x), as.numeric(grob$y))
expect_equal(location, c(0.75, 0.25))

# Check warning is thrown if only one is AsIs
p <- ggplot() +
annotate("text", x = I(0.75), y = 2.5, label = "foo") +
scale_x_continuous(limits = c(0, 10)) +
scale_y_continuous(limits = c(0, 10)) +
coord_radial()

expect_snapshot_warning(ggplotGrob(p))

})

# Visual tests ------------------------------------------------------------
Expand Down

0 comments on commit b1d45c8

Please sign in to comment.