Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ragg if available #138

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Suggests:
methods,
highr,
Cairo,
ragg,
stringr,
testthat (>= 3.0.0),
leaflet
Expand Down
29 changes: 21 additions & 8 deletions R/options.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@
#' \item{\code{repr.plot.*}}{
#' Those are for representations of \code{recordedplot} instances:
#' \describe{
#' \item{\code{repr.plot.width}}{Plotting area width in inches (default: 7)}
#' \item{\code{repr.plot.height}}{Plotting area height in inches (default: 7)}
#' \item{\code{repr.plot.pointsize}}{Text height in pt (default: 12)}
#' \item{\code{repr.plot.bg}}{Background color (default: white)}
#' \item{\code{repr.plot.antialias}}{Which kind of antialiasing to use for for lines and text? 'gray', 'subpixel' or 'none'? (default: gray)}
#' \item{\code{repr.plot.res}}{PPI for rasterization (default: 120)}
#' \item{\code{repr.plot.quality}}{Quality of JPEG format in \% (default: 90)}
#' \item{\code{repr.plot.family}}{Vector font family. 'sans', 'serif', 'mono' or a specific one (default: sans)}
#' \item{\code{repr.plot.width}}{Plotting area width in inches (default: \code{7})}
#' \item{\code{repr.plot.height}}{Plotting area height in inches (default: \code{7})}
#' \item{\code{repr.plot.pointsize}}{Text height in pt (default: \code{12})}
#' \item{\code{repr.plot.bg}}{Background color (default: \code{'white'})}
#' \item{\code{repr.plot.antialias}}{
#' Which kind of antialiasing to use for for lines and text?
#' 'gray', 'subpixel' or 'none'?
#' (default: \code{'gray'})
#' }
#' \item{\code{repr.plot.res}}{PPI for rasterization (default: \code{120})}
#' \item{\code{repr.plot.quality}}{Quality of JPEG format in \% (default: \code{90})}
#' \item{\code{repr.plot.family}}{Font family. 'sans', 'serif', 'mono' or a specific one (default: \code{'sans'})}
#' \item{\code{repr.plot.backends}}{
#' The order in which backends are tried.
#' \describe{
#' \item{\link[ragg:ragg]{ragg}}{Can emit only raster formats (png, jpeg), good quality}
#' \item{\link[Cairo:Cairo]{Cairo}}{Cannot handle per-element fonts, excellent quality}
#' \item{\link[grDevices:Devices]{grDevices}}{OK quality}
#' }
#' (default: \code{c('ragg', 'Cairo', 'grDevices')})
#' }
#' }
#' }
#' \item{\code{repr.vector.quote}}{
Expand Down
52 changes: 31 additions & 21 deletions R/repr_recordedplot.r
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
is_cairo_installed <- function() requireNamespace('Cairo', quietly = TRUE)
is_ragg_installed <- function() requireNamespace('ragg', quietly = TRUE)

get_device <- function(ragg, Cairo, grDevices) {
backends <- getOption('repr.plot.backends')
for (o in backends) switch(
o,
ragg = if (is_ragg_installed()) return(ragg),
Cairo = if (is_cairo_installed()) return(Cairo),
grDevices = return(grDevices),
stop("Unknown entry in getOption('repr.plot.backends'): ", o)
)
stop(
"No plotting devide found in getOption('repr.plot.backends'): ",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type: devide should be device.

Since this is an message intended for end users: maybe it could be even more explicit and say something like

stop(
"None of the backends specified in getOption('repr.plot.backends'): ",
toString(backends):
"seems to be available."
"\n Did you forget ..."
"Please specify a valid backend using 'options(repr.plot.backends = c(
)

(or, instead of available, you could say 'working' or whatever you think is the most accurate).

toString(backends),
"\nDid you forget to add 'grDevices' to the vector?"
)
}

# checking capability of X11 is slow, the short circult logic avoids
# this if any other devices are found.
Expand Down Expand Up @@ -87,13 +104,11 @@ repr_png.recordedplot <- function(obj,
res = getOption('repr.plot.res'),
...) {
if (!is_cairo_installed() && !check_capability('png')) return(NULL)

dev.cb <- function(tf)
if (is_cairo_installed())
Cairo::Cairo(width, height, tf, 'png', pointsize, bg, 'transparent', 'in', res)
else
png(tf, width, height, 'in', pointsize, bg, res, antialias = antialias)

dev.cb <- function(tf) get_device(
ragg = ragg::agg_png(tf, width, height, 'in', pointsize, bg, res), # scaling, bitsize
Copy link

@casparvl casparvl Sep 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code, at first I would have expected this to cause issues if the ragg package was not present on the end-user system, since I'd expect the namespace to not be known. Previously, for Cairo, you had it wrapped in if-else statemtents, so then the Cairo namespace would clearly only be called if the is_cairo_installed() returned true.

However, I initially didn't have ragg, and this line didn't produce any issues. I'm no R expert, so just want to check: are you sure this will work ok, even if some of the backends are not installed? And which function now imports this namespace? Before, I believe that was done by the requireNamespace call that was done by e.g. is_cairo_installed(), but now, it's not clear to me.

Cairo = Cairo::Cairo(width, height, tf, 'png', pointsize, bg, 'transparent', 'in', res),
grDevices = png(tf, width, height, 'in', pointsize, bg, res, antialias = antialias)
)
repr_recordedplot_generic(obj, '.png', TRUE, dev.cb)
}

Expand All @@ -111,13 +126,11 @@ repr_jpg.recordedplot <- function(obj,
quality = getOption('repr.plot.quality'),
...) {
if (!is_cairo_installed() && !check_capability('jpeg')) return(NULL)

dev.cb <- function(tf)
if (is_cairo_installed())
Cairo::Cairo(width, height, tf, 'jpeg', pointsize, bg, 'transparent', 'in', res, quality = quality)
else
jpeg(tf, width, height, 'in', pointsize, quality, bg, res, antialias = antialias)

dev.cb <- function(tf) get_device(
ragg = ragg::agg_jpeg(tf, width, height, 'in', pointsize, bg, res, quality = quality),
Cairo = Cairo::Cairo(width, height, tf, 'jpeg', pointsize, bg, 'transparent', 'in', res, quality = quality),
grDevices = jpeg(tf, width, height, 'in', pointsize, quality, bg, res, antialias = antialias)
)
repr_recordedplot_generic(obj, '.jpg', TRUE, dev.cb)
}

Expand All @@ -138,13 +151,10 @@ repr_svg.recordedplot <- function(obj,
family = getOption('repr.plot.family'),
...) {
if (!is_cairo_installed() && !capabilities('cairo')) return(NULL) #only cairo can do SVG

dev.cb <- function(tf)
if (is_cairo_installed())
Cairo::Cairo(width, height, tf, 'svg', pointsize, bg, 'transparent', 'in')
else
svg(tf, width, height, pointsize, FALSE, family, bg, antialias)

dev.cb <- get_device(
Cairo = Cairo::Cairo(width, height, tf, 'svg', pointsize, bg, 'transparent', 'in'),
grDevices = svg(tf, width, height, pointsize, FALSE, family, bg, antialias)
)
repr_recordedplot_generic(obj, '.svg', FALSE, dev.cb)
}

Expand Down
29 changes: 21 additions & 8 deletions man/repr-options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.