HenrikBengtsson / R.devices

🎨 R package: Unified Handling of Graphics Devices
https://henrikbengtsson.github.io/R.devices/
19 stars 5 forks source link

Add options to compress output files, e.g. svg.gz and eps.gz #26

Open HenrikBengtsson opened 7 years ago

HenrikBengtsson commented 7 years ago

Some image file formats such as EPS, Postscript and SVG can be significantly compressed. It would be handy if one could compress the output files on the fly using, say, gzip compression.

Some ideas:

toSVG("foo", compression = "gzip", { plot(1:100) })
devEval("svg", name="foo", compression = "gzip", { plot(1:100) })

devOptions("svg", compression = "gzip")
toSVG("foo", { plot(1:100) })
devEval("svg", name="foo", { plot(1:100) })

These should then all output a foo.svg.gz file.

HenrikBengtsson commented 7 years ago

Notes to self:

Graphics devices do not support using a file connection as output, e.g.

> file <- file("foo.png", open = "wb")
> png(filename = file)
Error in png(filename = file) : invalid 'filename' argument

This means that any compression needs to be done on already created image files. This sounds like a task for the DevEvalFileProduct class.

Some devices support compression, e.g.

> args(tiff)
function (filename = "Rplot%03d.tiff", width = 480, height = 480, 
    units = "px", pointsize = 12, compression = c("none", "rle", 
        "lzw", "jpeg", "zip", "lzw+p", "zip+p"), bg = "white", 
    res = NA, ..., type = c("cairo", "Xlib", "quartz"), antialias) 

How should selective compression be done? That is, we typically only want to compress for certain types, e.g.

res <- devEval(c("png", "svg", "x11", "eps")), name = "foo", plot(1:3) })

Could we use a compression = FALSE (none), compression = TRUE (all), compression = c("svg", "eps")(only SVG and EPS extensions), andcompression = NA` (default set of extensions that we typically want to compression; possibly set via an option).