Skip to content

Commit b1d45c8

Browse files
committed
throw warning when only one of two variables is AsIs
1 parent e569974 commit b1d45c8

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

R/coord-.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,24 @@ check_coord_limits <- function(
284284
check_object(limits, is_vector, "a vector", arg = arg, call = call)
285285
check_length(limits, 2L, arg = arg, call = call)
286286
}
287+
288+
is_transform_immune <- function(data, coord_name) {
289+
x <- inherits(data$x, "AsIs")
290+
y <- inherits(data$y, "AsIs")
291+
if (!(x || y)) {
292+
# Neither variable is AsIs, so we need to transform
293+
return(FALSE)
294+
}
295+
if (x && y) {
296+
# Both variables are AsIs, so no need to transform
297+
return(TRUE)
298+
}
299+
# We're now in the `xor(x, y)` case
300+
var <- if (x) "x" else "y"
301+
alt <- if (x) "y" else "x"
302+
cli::cli_warn(
303+
"{.fn {coord_name}} cannot respect the {.cls AsIs} class of {.var {var}} \\
304+
when {.var {alt}} is not also {.cls AsIs}."
305+
)
306+
return(FALSE)
307+
}

R/coord-polar.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ CoordPolar <- ggproto("CoordPolar", Coord,
180180
},
181181

182182
transform = function(self, data, panel_params) {
183-
if (inherits(data$x, "AsIs") && inherits(data$y, "AsIs")) {
183+
if (is_transform_immune(data, snake_class(self))) {
184184
return(data)
185185
}
186186

R/coord-radial.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ CoordRadial <- ggproto("CoordRadial", Coord,
275275
},
276276

277277
transform = function(self, data, panel_params) {
278-
if (inherits(data$x, "AsIs") && inherits(data$y, "AsIs")) {
278+
if (is_transform_immune(data, snake_class(self))) {
279279
return(data)
280280
}
281281

R/coord-sf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ CoordSf <- ggproto("CoordSf", CoordCartesian,
7777
},
7878

7979
transform = function(self, data, panel_params) {
80-
if (inherits(data$x, "AsIs") && inherits(data$y, "AsIs")) {
80+
if (is_transform_immune(data, snake_class(self))) {
8181
return(data)
8282
}
8383

tests/testthat/_snaps/coord-polar.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@
1212
No appropriate placement found for `r_axis_inside`.
1313
i Axis will be placed at panel edge.
1414

15+
# when both x and y are AsIs, they are not transformed
16+
17+
`coord_name()` cannot respect the <AsIs> class of `x` when `y` is not also <AsIs>.
18+

tests/testthat/test-coord-polar.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ test_that("when both x and y are AsIs, they are not transformed", {
170170
location <- c(as.numeric(grob$x), as.numeric(grob$y))
171171
expect_equal(location, c(0.75, 0.25))
172172

173+
# Check warning is thrown if only one is AsIs
174+
p <- ggplot() +
175+
annotate("text", x = I(0.75), y = 2.5, label = "foo") +
176+
scale_x_continuous(limits = c(0, 10)) +
177+
scale_y_continuous(limits = c(0, 10)) +
178+
coord_radial()
179+
180+
expect_snapshot_warning(ggplotGrob(p))
181+
173182
})
174183

175184
# Visual tests ------------------------------------------------------------

0 commit comments

Comments
 (0)