r-lib / gtable

The layout packages that powers ggplot2
https://gtable.r-lib.org
Other
87 stars 18 forks source link

Handling of NULL grobs in gtable_add_grob() #80

Open paleolimbot opened 5 years ago

paleolimbot commented 5 years ago

I ran across this while working on tidyverse/ggplot2#2810. It seems as though the gtable $grobs element is assumed to be a list of grobs, as the print() and grid.draw() methods fail if any of these elements are NULL. However, they are not checked by gtable_add_grob() when grobs is a list. This makes it difficult to debug, as the errors are delayed until render time.

gt <- gtable::gtable(
  widths = grid::unit(1, "null"), 
  heights = grid::unit(1, "null")
)

# correctly identified as not a grob
gtable::gtable_add_grob(gt, NULL, 1, 1, 1, 1)
#> Error: grobs must either be a single grob or a list of grobs

# creating this gtable is fine
gt_null <- gtable::gtable_add_grob(gt, list(NULL), 1, 1, 1, 1)

# ...but nothing can be done with it
print(gt_null)
#> TableGrob (1 x 1) "layout": 1 grobs
#> Error in vapply(x$grobs, as.character, character(1)): values must be length 1,
#>  but FUN(X[[1]]) result is length 0
grid::grid.draw(gt_null)
#> Error in gList(structure(list(wrapvp = structure(list(x = structure(0.5, class = "unit", valid.unit = 0L, unit = "npc"), : only 'grobs' allowed in "gList"

Created on 2019-05-03 by the reprex package (v0.2.1)