You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In relation to #126 could testthat support compressed svgs produced by {svglite}? It seems that it should be pretty simple to decompress the svg in testthat::compare_file_text using gzfile() of gzcon(), so {brio} doesn't need to support gz compressed files natively. I haven't looked what might be required in the shiny app to review snapshots.
The main use case for this is massively reducing the filesize of SVG snapshots produced by {vdiffr}. This is causing issues with the size of the tarball produced by R CMD build, which are being flagged by CRAN's incoming checks.
Why do we have such large SVGs? Checking these images produced from statistical models seems very sensitive to small differences in the fitted model due to OS differences etc, that go away with larger data. But then plotting outputs from these models often involves plotting data, which inflates the sizes of resulting plots. Regardless, reducing the file size of SVG snapshots would be good in terms of CRAN's policy on retention of package sources.
If you agree this is useful, would it be best to limit such a change to supporting only compressed svg or allowing tests of compressed text files more generally? Only gz compression or others? I'd happily produce a PR implementing the above if there is interest.
I think the place to start would be to prototype a custom compare function that you can pass to expect_snapshot_file(). If that's sufficient, we can figure out how to include in vdiffr. If it's not enough, it'll suggest what other changes are needed in testthat.
Returning to this, here is a pair of functions implementing the compare function @hadley suggested I start with
library("svglite")
# functions for testthat compareread_svg<-function(f) {
f<-if (identical(tools::file_ext(f), "svgz")) {
fc<- gzfile(f)
on.exit(close(fc))
base::readLines(fc)
} else {
base::readLines(f)
}
f
}
compare_file_text_svg<-function(old, new) {
old<- read_svg(old)
new<- read_svg(new)
identical(old, new)
}
# helper to do a plot and store it in temp file as a svgsave_svg<-function(code, compress=FALSE, width=10, height=8, ...) {
extn<- ifelse(compress, ".svgz", ".svg")
path<- tempfile(fileext=extn)
svglite::svglite(path, width=width, height=height, ...)
on.exit(dev.off())
codepath
}
# generate some plotsp1<- save_svg(plot(1:10), compress=TRUE)
p2<- save_svg(plot(1:5), compress=TRUE)
p3<- save_svg(plot(1:10), compress=FALSE)
# compare them
compare_file_text_svg(p1, p1) # want this to be TRUE#> [1] TRUE
compare_file_text_svg(p1, p2) # this should be FALSE#> [1] FALSE
compare_file_text_svg(p1, p3) # want this to be TRUE#> [1] TRUE
brio::read_lines() doesn't handle connections so I switched to using base::readLines() for the compressed version. I'm not sure if this matters, or whether it is safer to use
@gavinsimpson how much have you been able to reduce your _snaps folder using this method? And any chance this will be implemented in testthat any time soon?
@hadley I was under the impression that this would need vdiffr modifications to write the compressed svg snapshots, and support for reading compressed text files in testthat to do the actual testing/comparison.
I'm pretty sure this only needs vdiffr, since vdiffr can pass compare = compare_file_text_svg to expect_snapshot_file(). So I'm going to move this issue to vdiffr.
I'm very much a non-expert here, but doesn't CRAN judge package size based on the package tarball (.tar.gz)? Since the tarball is compressed, would there be much/any benefit to individually compressing the SVG snapshot files before putting them in the tarball?
@dmuenz there were certainly differences in the tar.gz size produced with and without using compressed svg for a limited set of experiments I ran before posting this. This could have been due to the specific compression algorithm used when building the tar.gz file with R CMD build (if the one used by compressed svg was more aggressive than the one used to compress the package tarball), but I would have to check if similar improvements could be made by changing the default compression when building.
Regardless, although this wasn't my original motivation for raising this issue, if the current RAMpocalypse tells us anything, we shouldn't assume that storage availability on users machines will increase at previously-observed rates, so limiting installation size of packages would be a general win too
Activity
hadley commentedon Mar 23, 2023
I think the place to start would be to prototype a custom
comparefunction that you can pass toexpect_snapshot_file(). If that's sufficient, we can figure out how to include in vdiffr. If it's not enough, it'll suggest what other changes are needed in testthat.gavinsimpson commentedon Mar 27, 2024
Returning to this, here is a pair of functions implementing the compare function @hadley suggested I start with
Created on 2024-03-27 with reprex v2.1.0
brio::read_lines()doesn't handle connections so I switched to usingbase::readLines()for the compressed version. I'm not sure if this matters, or whether it is safer to usewillgearty commentedon Oct 16, 2024
@gavinsimpson how much have you been able to reduce your _snaps folder using this method? And any chance this will be implemented in testthat any time soon?
gavinsimpson commentedon Oct 19, 2024
@willgearty I can't recall now, but it was not insignificant.
hadley commentedon Oct 29, 2024
IMO this is a vdiffr feature, not a testthat feature.
gavinsimpson commentedon Oct 29, 2024
@hadley I was under the impression that this would need vdiffr modifications to write the compressed svg snapshots, and support for reading compressed text files in testthat to do the actual testing/comparison.
hadley commentedon Aug 8, 2025
I'm pretty sure this only needs vdiffr, since vdiffr can pass
compare = compare_file_text_svgtoexpect_snapshot_file(). So I'm going to move this issue to vdiffr.dmuenz commentedon Jul 15, 2026
I'm very much a non-expert here, but doesn't CRAN judge package size based on the package tarball (.tar.gz)? Since the tarball is compressed, would there be much/any benefit to individually compressing the SVG snapshot files before putting them in the tarball?
gavinsimpson commentedon Jul 16, 2026
@dmuenz there were certainly differences in the tar.gz size produced with and without using compressed svg for a limited set of experiments I ran before posting this. This could have been due to the specific compression algorithm used when building the tar.gz file with
R CMD build(if the one used by compressed svg was more aggressive than the one used to compress the package tarball), but I would have to check if similar improvements could be made by changing the default compression when building.Regardless, although this wasn't my original motivation for raising this issue, if the current RAMpocalypse tells us anything, we shouldn't assume that storage availability on users machines will increase at previously-observed rates, so limiting installation size of packages would be a general win too