@@ -272,3 +272,43 @@ gtable_height <- function(x) sum(x$heights)
272272# ' @param x A gtable object
273273# ' @export
274274gtable_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+ }
0 commit comments