r - 测试保存ggplots
问题描述
我正在为 ggplots 创建一堆测试。我包括检查标签、输出对象是否为 ggplot 等内容。但我不知道如何测试绘图是否使用testthat
and保存ggsave
。你知道怎么做吗?
解决方案
这是一种基于文件大小进行测试的方法。如果文件不存在,则为 NA,如果确实存在,则应 > 0。
library(testthat)
library(ggplot2)
test_that("plot is saved to file", {
file <- tempfile(fileext = ".png")
expect_equal(file.size(file), NA_real_)
plot <- ggplot(mtcars, aes(wt, mpg)) +
geom_point()
ggsave(file, plot, "png")
expect_true(file.size(file) > 0)
unlink(file)
})
推荐阅读
- python - 当步骤函数python中某些事情失败时重试逻辑
- javascript - 反应笑话和 MSAL 得到 BrowserAuthError
- excel - Excel散点图背景颜色可以根据数据值自定义吗?
- php - jQuery代码不适用于php中的每次迭代
- design-patterns - 包装方法是一种设计模式吗?
- symfony - 无法连接到 Ratchet ZMQ 服务器并且无法推送更改
- amazon-web-services - 为第三方提供商启用 CustomSMSSender 以发送短信验证码
- asp.net - 可见 IIS 404 详细错误。服务器文件目录暴露
- c# - 使用 C# 在 Db2 上绑定
- lambda - 动作处理程序不支持自定义类型和数组