Skip to content

Commit 11c42a6

Browse files
authored
Add as.gtable() (#98)
1 parent cd4cfd7 commit 11c42a6

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
S3method("[",gtable)
44
S3method("dimnames<-",gtable)
5+
S3method(as.gtable,default)
6+
S3method(as.gtable,grob)
7+
S3method(as.gtable,gtable)
58
S3method(cbind,gtable)
69
S3method(dim,gtable)
710
S3method(dimnames,gtable)
@@ -15,6 +18,7 @@ S3method(print,gtable)
1518
S3method(rbind,gtable)
1619
S3method(t,gtable)
1720
S3method(widthDetails,gtable)
21+
export(as.gtable)
1822
export(gtable)
1923
export(gtable_add_col_space)
2024
export(gtable_add_cols)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# gtable (development version)
22

3+
* Added `as.gtable()` S3 method (#97).
4+
35
# gtable 0.3.5
46

57
# gtable 0.3.4

R/gtable.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,43 @@ gtable_height <- function(x) sum(x$heights)
272272
#' @param x A gtable object
273273
#' @export
274274
gtable_width <- function(x) sum(x$widths)
275+
276+
#' Convert to a gtable
277+
#'
278+
#' @param x An object to convert.
279+
#' @param ... Arguments forwarded to methods.
280+
#'
281+
#' @return A gtable object
282+
#' @export
283+
as.gtable <- function(x, ...) {
284+
check_dots_used()
285+
UseMethod("as.gtable")
286+
}
287+
288+
#' @export
289+
as.gtable.default <- function(x, ...) {
290+
cli::cli_abort("Can't convert {.obj_type_friendly {x}} to a {.cls gtable}.")
291+
}
292+
293+
#' @export
294+
as.gtable.gtable <- function(x, ...) x
295+
296+
#' @export
297+
#' @describeIn as.gtable Creates a 1-cell gtable containing the grob.
298+
#' @param widths,heights Scalar unit setting the size of the table. Defaults
299+
#' to [grid::grobWidth()] and [grid::grobHeight()] of `x` respectively.
300+
as.gtable.grob <- function(x, widths = NULL, heights = NULL, ...) {
301+
if (length(widths) > 1) {
302+
widths <- widths[1]
303+
cli::cli_warn("{.arg widths} truncated to length 1.")
304+
}
305+
if (length(heights) > 1) {
306+
heights <- heights[1]
307+
cli::cli_warn("{.arg heights} truncated to length 1.")
308+
}
309+
table <- gtable(
310+
widths = widths %||% grobWidth(x),
311+
heights = heights %||% grobHeight(x)
312+
)
313+
gtable_add_grob(table, x, t = 1L, l = 1L, b = 1L, r = 1L, ...)
314+
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ reference:
1919
- gtable_col
2020
- gtable_row
2121
- gtable_spacer
22+
- as.gtable
2223

2324
- title: Modification
2425
contents:

man/as.gtable.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/gtable.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# as.gtable sensibly converts objects
2+
3+
Can't convert an integer vector to a <gtable>.
4+
5+
---
6+
7+
Arguments in `...` must be used.
8+
x Problematic argument:
9+
* foo = "bar"
10+
i Did you misspell an argument name?
11+

tests/testthat/test-gtable.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
test_that("as.gtable sensibly converts objects", {
2+
3+
# gtable --> gtable is a no-op
4+
g1 <- gtable(unit(1, "npc"), unit(1, "npc"))
5+
g2 <- circleGrob(r = unit(1, "cm"))
6+
7+
expect_identical(as.gtable(g1), g1)
8+
9+
test <- as.gtable(g2)
10+
expect_s3_class(test, "gtable")
11+
expect_equal(as.numeric(convertUnit(gtable_width(test), "cm")), 2)
12+
expect_equal(as.numeric(convertUnit(gtable_height(test), "cm")), 2)
13+
14+
expect_warning(
15+
as.gtable(g2, widths = unit(c(1, 1), "cm")),
16+
"truncated to length 1"
17+
)
18+
expect_warning(
19+
as.gtable(g2, heights = unit(c(1, 1), "cm")),
20+
"truncated to length 1"
21+
)
22+
expect_snapshot_error(as.gtable(1:5))
23+
expect_snapshot_error(as.gtable(g1, foo = "bar"))
24+
})

0 commit comments

Comments
 (0)