繁体   English   中英

R test_表示ggplot没有改变

[英]R test_that a ggplot didn't change

我有一个自定义绘图功能,我希望确保即使在为绘图功能添加其他选项后它也会显示相同的绘图。

不幸的是, testthat::expect_known_hash -approach失败了因为ggplots存储了一些环境的信息,这显然会在重启R时发生变化。

在测试函数工厂的输出时会出现类似的问题,因为闭包也会带有它们的环境。

有没有人遇到过这个问题,你是怎么解决的?

compare允许您比较不同的R对象。 该包具有不同的功能来检测对象之间的差异。

compare(plot(0), plot(0))
TRUE



scatter <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(color=Species, shape=Species))
scatter2 <- ggplot(data=iris, aes(x = Sepal.Length, y = Petal.Width)) + geom_point(aes(color=Species, shape=Species))

compare(scatter, scatter2)

FALSE [TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE]
model treated as list
[layers] [1] model treated as character
[scales] model treated as character
[mapping] model treated as list
[mapping] [y] model treated as character
[coordinates] model treated as character
[facet] model treated as character

如果这是测试用例的一部分,您可以使用扩展'testthat'的包'vdiffr'。 它用于测试用例的最新版本'ggplot2',因此您可以在Github中查看示例。 它还在RStudio中安装一个插件来管理保存的测试用例,并对失败的测试进行视觉比较。 它工作得很好,我在自己的包中使用它。 它使用简化的svg设备将参考图保存到磁盘。

测试用例可能看起来像这样(其余的都像'testthat'案例):

  vdiffr::expect_doppelganger("test_001",
                              ggplot(data = cars, aes(speed, dist)) +
                                geom_point()
  )

在其他一些情况下,来自包'testthat'的expect_known_output()expect_known_value()可能有所帮助,但我猜不是在所有情况下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM