r-lib / vdiffr

Visual regression testing and graphical diffing with testthat
https://vdiffr.r-lib.org
Other
185 stars 31 forks source link

"svglite only supports one page" error related to gtable #85

Closed yutannihilation closed 3 years ago

yutannihilation commented 4 years ago

I found, while there's no error if I run the whole tests on ggplot2, running tests/testthat/test-guides.R solely fails with svglite only supports one page error. This seems somehow related to gtable or grid, but I'm not sure what's happening. Forgive me to file this issue with a not-so-minimal reprex so that I (or someone) can investigate more when I have a chance...

Here's a reprex and the result. The first expect_doppelganger() without axis labels succeeds, but if I add the labels it fails.

test_that("multiplication works", {
  test_draw_axis <- function(add_labels = FALSE) {
    theme <- theme_test() + theme(axis.line = element_line(size = 0.5))
    positions <- c("top", "right", "bottom", "left")

    n_breaks <- 3
    break_positions <- seq_len(n_breaks) / (n_breaks + 1)
    labels <- if (add_labels) as.character(seq_along(break_positions))

    # create the axes
    axes <- lapply(positions, function(position) {
      ggplot2:::draw_axis(break_positions, labels, axis_position = position, theme = theme)
    })
    axes_grob <- gTree(children = do.call(gList, axes))

    # arrange them so there's some padding on each side
    gt <- gtable(
      widths = unit(c(0.05, 0.9, 0.05), "npc"),
      heights = unit(c(0.05, 0.9, 0.05), "npc")
    )
    gt <- gtable_add_grob(gt, list(axes_grob), 2, 2, clip = "off")
    plot(gt)
  }

  vdiffr::expect_doppelganger("test1", test_draw_axis(FALSE))
  vdiffr::expect_doppelganger("test2", test_draw_axis(TRUE))
})

( https://github.com/yutannihilation/vdiffr-test-svglite-error/blob/master/tests/testthat/test-vdiffr.R )

Result

==> devtools::test()

Loading vdiffrGtableTest
Testing vdiffrGtableTest
✓ |  OK F W S | Context
⠏ |   0       | vdiffrNULL
x |   1 1     | vdiffr [0.2 s]
─────────────────────────────
test-vdiffr.R:35: error: multiplication works
svglite only supports one page
Backtrace:
 1. vdiffr::expect_doppelganger("test2", test_draw_axis(TRUE)) tests/testthat/test-vdiffr.R:35:2
 4. vdiffrGtableTest:::test_draw_axis(TRUE)
 6. gtable:::plot.gtable(gt)
 7. grid::grid.newpage()
─────────────────────────────

══ Results ══════════════════════════════════════════════
Duration: 0.6 s

OK:       1
Failed:   1
Warnings: 0
Skipped:  0

No one is perfect!
dustinfife commented 3 years ago

I too have the same error. I've created a function designed to modify a ggplot2 object:

modify_points = function(p, shape=19, colour="black", size=1.5) {
  c = ggplot2::ggplot_build(p)

  # make modifications
  c$data[[1]]$size = size
  c$data[[1]]$colour = colour
  c$data[[1]]$shape = shape
  c = ggplot2::ggplot_gtable(c)

  # return plot
  plot(c)
}

Like, @yutannihilation, also get the same error ("svglite only supports one page").

lionel- commented 3 years ago

Seems to be fixed by #106.