Skip to content

Commit 2cdafed

Browse files
cpsievertwch
andauthored
Use ggplot2::get_alt_text() if available to provide better default alt text (#3398)
* Close #3397: Use ggplot2::get_alt_text() if available to provide more informative default alt text for ggplots in renderPlot() * Update R/render-plot.R Co-authored-by: Winston Chang <[email protected]> * better Rd docs * make logic more self-contained * Add news Co-authored-by: Winston Chang <[email protected]>
1 parent b4caa91 commit 2cdafed

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ shiny 1.6.0.9000
1818

1919
* `icon(lib="fontawesome")` is now powered by the `{fontawesome}` package, which will make it easier to use the latest FA icons in the future (by updating the `{fontawesome}` package). (#3302)
2020

21+
* Closed #3397: `renderPlot()` new uses `ggplot2::get_alt_text()` to inform an `alt` text default (for `{ggplot2}` plots). (#3398)
22+
2123
* `modalDialog()` gains support for `size = "xl"`. (#3410)
2224

2325
### Other improvements

R/render-plot.R

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
#' @param res Resolution of resulting plot, in pixels per inch. This value is
3737
#' passed to [grDevices::png()]. Note that this affects the resolution of PNG
3838
#' rendering in R; it won't change the actual ppi of the browser.
39-
#' @param alt Alternate text for the HTML `<img>` tag
40-
#' if it cannot be displayed or viewed (i.e., the user uses a screen reader).
41-
#' In addition to a character string, the value may be a reactive expression
42-
#' (or a function referencing reactive values) that returns a character string.
43-
#' NULL or "" is not recommended because those should be limited to decorative images
44-
#' (the default is "Plot object").
39+
#' @param alt Alternate text for the HTML `<img>` tag if it cannot be displayed
40+
#' or viewed (i.e., the user uses a screen reader). In addition to a character
41+
#' string, the value may be a reactive expression (or a function referencing
42+
#' reactive values) that returns a character string. If the value is `NA` (the
43+
#' default), then `ggplot2::get_alt_text()` is used to extract alt text from
44+
#' ggplot objects; for other plots, `NA` results in alt text of "Plot object".
45+
#' `NULL` or `""` is not recommended because those should be limited to
46+
#' decorative images.
4547
#' @param ... Arguments to be passed through to [grDevices::png()].
4648
#' These can be used to set the width, height, background color, etc.
4749
#' @param env The environment in which to evaluate `expr`.
@@ -58,7 +60,7 @@
5860
#' interactive R Markdown document.
5961
#' @export
6062
renderPlot <- function(expr, width = 'auto', height = 'auto', res = 72, ...,
61-
alt = "Plot object",
63+
alt = NA,
6264
env = parent.frame(), quoted = FALSE,
6365
execOnResize = FALSE, outputArgs = list()
6466
) {
@@ -212,7 +214,7 @@ resizeSavedPlot <- function(name, session, result, width, height, alt, pixelrati
212214
src = session$fileUrl(name, outfile, contentType = "image/png"),
213215
width = width,
214216
height = height,
215-
alt = alt,
217+
alt = result$alt,
216218
coordmap = coordmap,
217219
error = attr(coordmap, "error", exact = TRUE)
218220
)
@@ -288,6 +290,7 @@ drawPlot <- function(name, session, func, width, height, alt, pixelratio, res, .
288290
recordedPlot = grDevices::recordPlot(),
289291
coordmap = getCoordmap(value, width*pixelratio, height*pixelratio, res*pixelratio),
290292
pixelratio = pixelratio,
293+
alt = if (anyNA(alt)) getAltText(value) else alt,
291294
res = res
292295
)
293296
}
@@ -302,10 +305,10 @@ drawPlot <- function(name, session, func, width, height, alt, pixelratio, res, .
302305
),
303306
function(result) {
304307
result$img <- dropNulls(list(
305-
src = session$fileUrl(name, outfile, contentType='image/png'),
308+
src = session$fileUrl(name, outfile, contentType = 'image/png'),
306309
width = width,
307310
height = height,
308-
alt = alt,
311+
alt = result$alt,
309312
coordmap = result$coordmap,
310313
# Get coordmap error message if present
311314
error = attr(result$coordmap, "error", exact = TRUE)
@@ -339,6 +342,24 @@ custom_print.ggplot <- function(x) {
339342
), class = "ggplot_build_gtable")
340343
}
341344

345+
# Infer alt text description from renderPlot() value
346+
# (currently just ggplot2 is supported)
347+
getAltText <- function(x, default = "Plot Object") {
348+
# Since, inside renderPlot(), custom_print.ggplot()
349+
# overrides print.ggplot, this class indicates a ggplot()
350+
if (!inherits(x, "ggplot_build_gtable")) {
351+
return(default)
352+
}
353+
# ggplot2::get_alt_text() was added in v3.3.4
354+
# https://github.com/tidyverse/ggplot2/pull/4482
355+
get_alt <- getNamespace("ggplot2")$get_alt_text
356+
if (!is.function(get_alt)) {
357+
return(default)
358+
}
359+
alt <- paste(get_alt(x$build), collapse = " ")
360+
if (nzchar(alt)) alt else default
361+
}
362+
342363
# The coordmap extraction functions below return something like the examples
343364
# below. For base graphics:
344365
# plot(mtcars$wt, mtcars$mpg)

man/renderCachedPlot.Rd

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/renderPlot.Rd

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)