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

Added alt param #3006

Merged
merged 10 commits into from Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Expand Up @@ -2,6 +2,8 @@
shiny 1.5.0.9000
================

## Full changelog

### Accessibility

* Added [bootstrap accessibility plugin](https://github.com/paypal/bootstrap-accessibility-plugin) under the hood to improve accessibility of shiny apps for screen-reader and keyboard users: the enhancements include better navigations for alert, tooltip, popover, modal dialog, dropdown, tab Panel, collapse, and carousel elements. (#2911)
Expand All @@ -14,6 +16,8 @@ shiny 1.5.0.9000

* Closed #2847: `selectInput()` is reasonably accessible for screen readers even when `selectize` option is set to TRUE. To improve `selectize.js` accessibility, We have added [selectize-plugin-a11y](https://github.com/SLMNBJ/selectize-plugin-a11y) by default. (#2993)

* Closed #612: Added `alt` argument to `renderPlot()` and `renderCachedPlot()` to specify descriptive texts for `plotOutput()` objects, which is essential for screen readers. By default, alt text is set to the static text, "Plot object," but even dynamic text can be made with reactive function. (#3006, thanks @trafficonese and @leonawicz for the original PR and discussion via #2494)

### Minor new features and improvements

* When UI is specified as a function (e.g. `ui <- function(req) { ... }`), the response can now be an HTTP response as returned from the (newly exported) `httpResponse()` function. (#2970)
Expand Down
15 changes: 15 additions & 0 deletions R/render-cached-plot.R
Expand Up @@ -293,6 +293,7 @@ renderCachedPlot <- function(expr,
cacheKeyExpr,
sizePolicy = sizeGrowthRatio(width = 400, height = 400, growthRate = 1.2),
res = 72,
alt = "Plot object",
schloerke marked this conversation as resolved.
Show resolved Hide resolved
cpsievert marked this conversation as resolved.
Show resolved Hide resolved
cache = "app",
...,
outputArgs = list()
Expand Down Expand Up @@ -341,6 +342,14 @@ renderCachedPlot <- function(expr,
# values get filled by an observer below.
fitDims <- reactiveValues(width = NULL, height = NULL)

# Make sure alt param to be reactive function
if (is.reactive(alt))
altWrapper <- alt
else if (is.function(alt))
altWrapper <- reactive({ alt() })
else
altWrapper <- function() { alt }

resizeObserver <- NULL
ensureResizeObserver <- function() {
if (!is.null(resizeObserver))
Expand Down Expand Up @@ -387,6 +396,8 @@ renderCachedPlot <- function(expr,
isolate({
width <- fitDims$width
height <- fitDims$height
# Make sure alt text to be reactive function
alt <- altWrapper()
})

pixelratio <- session$clientData$pixelratio %OR% 1
Expand All @@ -398,6 +409,7 @@ renderCachedPlot <- function(expr,
func = isolatedFunc,
width = width,
height = height,
alt = alt,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
jooyoungseo marked this conversation as resolved.
Show resolved Hide resolved
jooyoungseo marked this conversation as resolved.
Show resolved Hide resolved
pixelratio = pixelratio,
res = res
),
Expand Down Expand Up @@ -471,6 +483,7 @@ renderCachedPlot <- function(expr,
plotObj = drawReactiveResult,
width = width,
height = height,
alt = alt,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
pixelratio = pixelratio
)
}
Expand All @@ -480,6 +493,7 @@ renderCachedPlot <- function(expr,
hybrid_chain(possiblyAsyncResult, function(result) {
width <- result$width
height <- result$height
alt <- result$alt
jooyoungseo marked this conversation as resolved.
Show resolved Hide resolved
pixelratio <- result$pixelratio

# Three possibilities when we get here:
Expand All @@ -500,6 +514,7 @@ renderCachedPlot <- function(expr,
result$plotObj,
width,
height,
alt,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
jooyoungseo marked this conversation as resolved.
Show resolved Hide resolved
pixelratio,
res
),
Expand Down
24 changes: 20 additions & 4 deletions R/render-plot.R
Expand Up @@ -36,6 +36,12 @@
#' @param res Resolution of resulting plot, in pixels per inch. This value is
#' passed to [grDevices::png()]. Note that this affects the resolution of PNG
#' rendering in R; it won't change the actual ppi of the browser.
#' @param alt Alternate text for the HTML `<img>` tag
#' if it cannot be displayed or viewed (i.e., the user uses a screen reader).
#' In addition to a character string, the value may be a reactive expression
#' (or a function referencing reactive values) that returns a character string.
#' NULL or "" is not recommended because those should be limited to decorative images
#' (the default is "Plot object").
#' @param ... Arguments to be passed through to [grDevices::png()].
#' These can be used to set the width, height, background color, etc.
#' @param env The environment in which to evaluate `expr`.
Expand All @@ -51,7 +57,7 @@
#' call to [plotOutput()] when `renderPlot` is used in an
#' interactive R Markdown document.
#' @export
renderPlot <- function(expr, width='auto', height='auto', res=72, ...,
renderPlot <- function(expr, width='auto', height='auto', res=72, alt="Plot object", ...,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
env=parent.frame(), quoted=FALSE,
execOnResize=FALSE, outputArgs=list()
) {
Expand All @@ -75,6 +81,13 @@ renderPlot <- function(expr, width='auto', height='auto', res=72, ...,
else
heightWrapper <- function() { height }

if (is.reactive(alt))
altWrapper <- alt
else if (is.function(alt))
altWrapper <- reactive({ alt() })
else
altWrapper <- function() { alt }

getDims <- function() {
width <- widthWrapper()
height <- heightWrapper()
Expand Down Expand Up @@ -112,6 +125,7 @@ renderPlot <- function(expr, width='auto', height='auto', res=72, ...,
func = func,
width = dims$width,
height = dims$height,
alt = altWrapper(),
pixelratio = pixelratio,
res = res
), args))
Expand Down Expand Up @@ -140,7 +154,7 @@ renderPlot <- function(expr, width='auto', height='auto', res=72, ...,
dims <- getDims()
pixelratio <- session$clientData$pixelratio %OR% 1
result <- do.call("resizeSavedPlot", c(
list(name, shinysession, result, dims$width, dims$height, pixelratio, res),
list(name, shinysession, result, dims$width, dims$height, altWrapper(), pixelratio, res),
args
))

Expand All @@ -159,7 +173,7 @@ renderPlot <- function(expr, width='auto', height='auto', res=72, ...,
markRenderFunction(outputFunc, renderFunc, outputArgs = outputArgs)
}

resizeSavedPlot <- function(name, session, result, width, height, pixelratio, res, ...) {
resizeSavedPlot <- function(name, session, result, width, height, alt, pixelratio, res, ...) {
schloerke marked this conversation as resolved.
Show resolved Hide resolved
if (result$img$width == width && result$img$height == height &&
result$pixelratio == pixelratio && result$res == res) {
return(result)
Expand All @@ -181,14 +195,15 @@ resizeSavedPlot <- function(name, session, result, width, height, pixelratio, re
src = session$fileUrl(name, outfile, contentType = "image/png"),
width = width,
height = height,
alt = alt,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
coordmap = coordmap,
error = attr(coordmap, "error", exact = TRUE)
)

result
}

drawPlot <- function(name, session, func, width, height, pixelratio, res, ...) {
drawPlot <- function(name, session, func, width, height, alt, pixelratio, res, ...) {
schloerke marked this conversation as resolved.
Show resolved Hide resolved
# 1. Start PNG
# 2. Enable displaylist recording
# 3. Call user-defined func
Expand Down Expand Up @@ -272,6 +287,7 @@ drawPlot <- function(name, session, func, width, height, pixelratio, res, ...) {
src = session$fileUrl(name, outfile, contentType='image/png'),
width = width,
height = height,
alt = alt,
schloerke marked this conversation as resolved.
Show resolved Hide resolved
coordmap = result$coordmap,
# Get coordmap error message if present
error = attr(result$coordmap, "error", exact = TRUE)
Expand Down
8 changes: 8 additions & 0 deletions man/renderCachedPlot.Rd

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

8 changes: 8 additions & 0 deletions man/renderPlot.Rd

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