Skip to content

Commit 3ae8031

Browse files
committed
fallback for unit margins
1 parent d21f0c5 commit 3ae8031

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

R/margins.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ margin_auto <- function(t = 0, r = t, b = t, l = r, unit = "pt") {
3030
margin(t = t, r = r, b = b, l = l, unit)
3131
}
3232

33+
as_margin <- function(x, x_arg = caller_arg(x), call = caller_env()) {
34+
if (is_margin(x)) {
35+
return(x)
36+
}
37+
if (!is.unit(x)) {
38+
cli::cli_abort(
39+
"{.arg {x_arg}} must be a {.cls margin} class, \\
40+
not {.obj_type_friendly {x}}."
41+
)
42+
}
43+
if (length(x) != 4) {
44+
x <- rep(x, length.out = 4)
45+
}
46+
type <- unitType(x)
47+
if (is_unique(type)) {
48+
type <- type[1]
49+
}
50+
x <- as.numeric(x)
51+
margin(x[1], x[2], x[3], x[4], unit = type)
52+
}
53+
3354
#' Create a text grob with the proper location and margins
3455
#'
3556
#' `titleGrob()` is called when creating titles and labels for axes, legends,

R/theme-elements.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ element_text <- S7::new_class(
236236
"i" = "Results may be unexpected or may change in future versions of ggplot2."
237237
))
238238
}
239+
if (!is_margin(margin) && !is.null(margin)) {
240+
margin <- as_margin(margin)
241+
cli::cli_warn(
242+
"The {.arg margin} argument should be constructed using the \\
243+
{.fn margin} function."
244+
)
245+
}
239246

240247
colour <- color %||% colour
241248
obj <- S7::new_object(

tests/testthat/_snaps/theme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@
7878

7979
Ignoring unknown `theme()` elements: foo and bar.
8080

81+
# element_text throws appropriate conditions
82+
83+
Vectorized input to `element_text()` is not officially supported.
84+
i Results may be unexpected or may change in future versions of ggplot2.
85+
86+
---
87+
88+
The `margin` argument should be constructed using the `margin()` function.
89+
90+
---
91+
92+
Code
93+
element_text(margin = 5)
94+
Condition
95+
Error in `as_margin()`:
96+
! `margin` must be a <margin> class, not a number.
97+
8198
# Theme validation behaves as expected
8299

83100
The `aspect.ratio` theme element must be a <numeric/integer> object.

tests/testthat/test-theme.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,19 @@ test_that("subtheme functions rename arguments as intended", {
546546
)
547547
})
548548

549+
test_that("element_text throws appropriate conditions", {
550+
expect_snapshot_warning(
551+
element_text(colour = c("red", "blue"))
552+
)
553+
expect_snapshot_warning(
554+
element_text(margin = unit(1, "cm"))
555+
)
556+
expect_snapshot(
557+
element_text(margin = 5),
558+
error = TRUE
559+
)
560+
})
561+
549562
test_that("Theme validation behaves as expected", {
550563
tree <- get_element_tree()
551564
expect_silent(check_element(1, "aspect.ratio", tree))

0 commit comments

Comments
 (0)