How to test graphical output of functions? How to test graphical output of functions? r r

How to test graphical output of functions?


Mango Solutions have published an open source package, visualTest, that does fuzzy matching of plots, to address this use case.

The package is on github, so install using:

devtools::install_github("MangoTheCat/visualTest")library(visualTest)

Then use the function getFingerprint() to extract a finger print for each plot, and compare using the function isSimilar(), specifying a suitable threshold.

First, create some plots on file:

png(filename = "test1.png")img()dev.off()png(filename = "test2.png")plot(1:11, col="red")dev.off()

The finger print is a numeric vector:

> getFingerprint(file = "test1.png") [1]  4  7  4  4 10  4  7  7  4  7  7  4  7  4  5  9  4  7  7  5  6  7  4  7  4  4 10[28]  4  7  7  4  7  7  4  7  4  3  7  4  4  3  4  4  5  5  4  7  4  7  4  7  7  7  4[55]  7  7  4  7  4  7  5  6  7  7  4  8  6  4  7  4  7  4  7  7  7  4  4 10  4  7  4> getFingerprint(file = "test2.png") [1]  7  7  4  4 17  4  7  4  7  4  7  7  4  5  9  4  7  7  5  6  7  4  7  7 11  4  7[28]  7  5  6  7  4  7  4 14  4  3  4  7 11  7  4  7  5  6  7  7  4  7 11  7  4  7  5[55]  6  7  7  4  8  6  4  7  7  4  4  7  7  4 10 11  4  7  7

Compare using isSimilar():

> isSimilar(file = "test2.png",+           fingerprint = getFingerprint(file = "test1.png"),+           threshold = 0.1+ )[1] FALSE

You can read more about the package at http://www.mango-solutions.com/wp/products-services/r-services/r-packages/visualtest/


It's worth noting that the vdiffr package also supports comparing plots. A nice feature is that it integrates with the testthat package -- it's actually used for testing in ggplot2 -- and it has an add-in for RStudio to help manage your testsuite.