tests/testthat/test-save.R

test_that("save_ggplot works with normal ggplot", {
  library(ggplot2)
  p <- ggplot(mtcars, aes(wt, mpg)) +
    geom_point() +
    ggtitle("My Plot")
  temp_file <- tempfile(fileext = ".png")
  save_ggplot(p, temp_file)
  expect_true(file.exists(temp_file))
  file.remove(temp_file)
})

test_that("save_ggplot works with canvas", {
  library(ggplot2)
  p <- ggplot(mtcars, aes(wt, mpg)) +
    geom_point() +
    ggtitle("My Plot") +
    canvas(8, 6, dpi = 400)
  temp_file <- tempfile(fileext = ".png")
  save_ggplot(p, temp_file)
  expect_true(file.exists(temp_file))
  file.remove(temp_file)
})

test_that("save_ggplot saves with correct dimensions", {
  skip_if_not_installed("png")
  library(ggplot2)
  p <- ggplot(mtcars, aes(wt, mpg)) +
    geom_point() +
    ggtitle("My Plot") +
    canvas(800, 600, units = "px")
  temp_file <- tempfile(fileext = ".png")
  save_ggplot(p, temp_file)
  expect_true(file.exists(temp_file))
  info <- png::readPNG(temp_file)
  expect_equal(dim(info)[1:2], c(600, 800))
  file.remove(temp_file)
})

Try the ggview package in your browser

Any scripts or data that you put into this service are public.

ggview documentation built on Oct. 2, 2024, 5:06 p.m.