|
1 | 1 | startPNG <- function(filename, width, height, res, ...) { |
2 | | - # shiny.useragg is an experimental option that isn't officially supported or |
3 | | - # documented. It's here in the off chance that someone really wants |
4 | | - # to use ragg (say, instead of showtext, for custom font rendering). |
5 | | - # In the next shiny release, this option will likely be superseded in |
6 | | - # favor of a fully customizable graphics device option |
7 | | - if ((getOption('shiny.useragg') %||% FALSE) && is_installed("ragg")) { |
8 | | - pngfun <- ragg::agg_png |
| 2 | + pngfun <- if ((getOption('shiny.useragg') %||% TRUE) && is_installed("ragg")) { |
| 3 | + ragg::agg_png |
9 | 4 | } else if (capabilities("aqua")) { |
10 | 5 | # i.e., png(type = 'quartz') |
11 | | - pngfun <- grDevices::png |
| 6 | + grDevices::png |
12 | 7 | } else if ((getOption('shiny.usecairo') %||% TRUE) && is_installed("Cairo")) { |
13 | | - pngfun <- Cairo::CairoPNG |
| 8 | + Cairo::CairoPNG |
14 | 9 | } else { |
15 | 10 | # i.e., png(type = 'cairo') |
16 | | - pngfun <- grDevices::png |
| 11 | + grDevices::png |
17 | 12 | } |
18 | 13 |
|
19 | 14 | args <- rlang::list2(filename=filename, width=width, height=height, res=res, ...) |
@@ -57,33 +52,31 @@ startPNG <- function(filename, width, height, res, ...) { |
57 | 52 | grDevices::dev.cur() |
58 | 53 | } |
59 | 54 |
|
60 | | -#' Run a plotting function and save the output as a PNG |
| 55 | +#' Capture a plot as a PNG file. |
61 | 56 | #' |
62 | | -#' This function returns the name of the PNG file that it generates. In |
63 | | -#' essence, it calls `png()`, then `func()`, then `dev.off()`. |
64 | | -#' So `func` must be a function that will generate a plot when used this |
65 | | -#' way. |
66 | | -#' |
67 | | -#' For output, it will try to use the following devices, in this order: |
68 | | -#' quartz (via [grDevices::png()]), then [Cairo::CairoPNG()], |
69 | | -#' and finally [grDevices::png()]. This is in order of quality of |
70 | | -#' output. Notably, plain `png` output on Linux and Windows may not |
71 | | -#' antialias some point shapes, resulting in poor quality output. |
72 | | -#' |
73 | | -#' In some cases, `Cairo()` provides output that looks worse than |
74 | | -#' `png()`. To disable Cairo output for an app, use |
75 | | -#' `options(shiny.usecairo=FALSE)`. |
| 57 | +#' The PNG graphics device used is determined in the following order: |
| 58 | +#' * If the ragg package is installed (and the `shiny.useragg` is not |
| 59 | +#' set to `FALSE`), then use [ragg::agg_png()]. |
| 60 | +#' * If a quartz device is available (i.e., `capabilities("aqua")` is |
| 61 | +#' `TRUE`), then use `png(type = "quartz")`. |
| 62 | +#' * If the Cairo package is installed (and the `shiny.usecairo` option |
| 63 | +#' is not set to `FALSE`), then use [Cairo::CairoPNG()]. |
| 64 | +#' * Otherwise, use [grDevices::png()]. In this case, Linux and Windows |
| 65 | +#' may not antialias some point shapes, resulting in poor quality output. |
76 | 66 | #' |
77 | 67 | #' @param func A function that generates a plot. |
78 | 68 | #' @param filename The name of the output file. Defaults to a temp file with |
79 | 69 | #' extension `.png`. |
80 | 70 | #' @param width Width in pixels. |
81 | 71 | #' @param height Height in pixels. |
82 | | -#' @param res Resolution in pixels per inch. This value is passed to |
83 | | -#' [grDevices::png()]. Note that this affects the resolution of PNG rendering in |
| 72 | +#' @param res Resolution in pixels per inch. This value is passed to the |
| 73 | +#' graphics device. Note that this affects the resolution of PNG rendering in |
84 | 74 | #' R; it won't change the actual ppi of the browser. |
85 | | -#' @param ... Arguments to be passed through to [grDevices::png()]. |
86 | | -#' These can be used to set the width, height, background color, etc. |
| 75 | +#' @param ... Arguments to be passed through to the graphics device. These can |
| 76 | +#' be used to set the width, height, background color, etc. |
| 77 | +#' |
| 78 | +#' @return A path to the newly generated PNG file. |
| 79 | +#' |
87 | 80 | #' @export |
88 | 81 | plotPNG <- function(func, filename=tempfile(fileext='.png'), |
89 | 82 | width=400, height=400, res=72, ...) { |
|
0 commit comments