Here is a modified ggsave that uses this approach.
library(ragg)
library(purrr)
library(digest)
library(ggplot2)
myggsave <- function(fname, height = 210, width = 297, units = "mm", forceplot = TRUE, ...){
hash <- map_chr(last_plot(), digest, algo="xxhash32")
hashfile <- paste0(fname, ".hash.rds")
if (!file.exists(hashfile)){
print(paste("Creating", fname))
ggsave(fname, device = agg_png, height = height, width = width, units = units, ...)
saveRDS(hash, hashfile)
} else {
existing <- readRDS(hashfile)
if (isTRUE(all.equal(hash, existing))){
print(paste("No change to", fname))
} else {
if (forceplot){
print(paste("Overwriting", fname))
ggsave(fname, device = agg_png, height = height, width = width, units = units, ...)
saveRDS(hash, hashfile)
} else {
print(paste("Keeping old version of", fname))
}
}
}
}