this is an example of a knitr plot hook that supports only the arguments caption
and alt
.
All other arguments you would like to use you should include yourself.
For my convenience I also used nocaption
and noalt
but because these are not recognized they have the same meaning as not specifying caption
resp. alt
.
---
author: "C Lindell"
title: "my title"
date: '2020-06-24'
output:
html_document:
toc: yes
toc_float: yes
keep_md: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
knitr::knit_hooks$set(plot = function(x,options) {
base = knitr::opts_knit$get('base.url')
if (is.null(base)) base = ''
alt = ifelse (is.null(options$alt),"",options$alt)
cap = ifelse (is.null(options$caption),"",options$caption)
if (alt != ""){
sprintf('', cap, base, x, alt)
} else {
sprintf('', cap, base, x)
}
})
```
# Diagrams and pictures
How do I provide a generated diagram with a "alt text"-tag to make it readable for the vision impaired?
```{r}
library(ggplot2)
```
### plot with caption and alt_text
```{r plot1,caption='nice plot',alt='my alt_text'}
ggplot(cars, aes(speed, dist)) +
geom_line()
```
### plot without caption and with alt_text
```{r plot2,nocaption='nice plot',alt='my alt_text'}
ggplot(cars, aes(speed, dist)) +
geom_line()
```
### plot with caption and without alt_text
```{r plot3,caption='nice plot',noalt='my alt_text'}
ggplot(cars, aes(speed, dist)) +
geom_line()
```