Using BrailleR to create alt text in RMarkdown

I'm trying to use the BrailleR package to create alt-text. When I try, the alt-text simply shows up as literally "BrailleR::VI(p1)" instead of the output of the function. How can I get the output to pass to the chunk?

The function in this example returns

This is an untitled chart with no subtitle or caption.
It has x-axis 'class' with labels 2seater, compact, midsize, minivan, pickup, subcompact and suv.
It has y-axis 'count' with labels 0, 20, 40 and 60.
The chart is a bar chart with 7 vertical bars.
Bar 1 is centered at 1, and length is from 0 to 5.
Bar 2 is centered at 2, and length is from 0 to 47.
Bar 3 is centered at 3, and length is from 0 to 41.
Bar 4 is centered at 4, and length is from 0 to 11.
Bar 5 is centered at 5, and length is from 0 to 33.
Bar 6 is centered at 6, and length is from 0 to 35.
Bar 7 is centered at 7, and length is from 0 to 62.

When I build the following example and look at the alt-text in HTML, it is literally "BrailleR::VI(p1)" instead of the output from the function.

---
title: "Brailler Alt Text"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
#| label: make-plot
library(tidyverse)

p1 <- mpg %>%
  ggplot(aes(class)) + geom_bar()

BrailleR::VI(p1)
```


```{r}
#| label: print-plot
#| fig.cap: "Bar plot of car class"
#| fig.alt: BrailleR::VI(p1)

p1
```



I figured out that if I specify the chunk option in-line, it will work! I still think this is a bug and filed it here: fig.alt option doesn't work with quarto style chunk option when specifying with code · Issue #2501 · rstudio/rmarkdown · GitHub

---
title: "Brailler Alt Text"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
#| label: make-plot
library(tidyverse)

p1 <- mpg %>%
  ggplot(aes(class)) + geom_bar()

BrailleR::VI(p1)
```


```{r, fig.alt=str_c(BrailleR::VI(p1)$text, collapse=" ")}
#| label: print-plot
#| fig.cap: "Bar plot of car class"

p1
```
1 Like

_putting back here answer I gave at Issues · rstudio/rmarkdown · GitHub

This really a side effect of using YAML syntax for chunk options, which Quarto does expect by default.

  • When using chunk header to place options (usual knitr way), you are using special key = value syntax that will be parsed as R. This means by default, value is parsed and evaluated as an R expression.

  • When using YAML syntax for placing options in-chunk, this is really using YAML syntax. This means key: value respect the YAML spec, and it won't parse and evaluate a string as R code expression.

R YAML parser has a way to trigger expression evaluation before YAML parsing which is using !expr

```{r}
#| label: print-plot-bad
#| fig.cap: "Bar plot of car class with bad alt-text"
#| fig.alt: !expr str_c(BrailleR::VI(p1)$text, collapse=" ")
p1
```

We explain the limitation in

  • Quarto doc: Quarto – Using R

    Chunk options included this way use YAML syntax rather than R syntax for consistency with options provided in YAML front matter. You can still however use R code for option values by prefacing them with !expr. For example:

  • In knitr post about new chunk header syntax: News from knitr v1.35 to v1.37: Alternative Syntax for Chunk Options, and Some New Engines - Yihui Xie | 谢益辉

    Personally I do not like YAML but I know many people do (I can never remember how to properly write a string value that contains both single quotes and colons). For the traditional comma-separated syntax, you only need to know the R syntax, with which you should be familiar. Another benefit of the comma-separated syntax is that you can write R expressions freely, which is a little awkward in YAML—to provide an R expression as a YAML value, you need to add !expr before the expression

So the behavior you see is expected. You need to adapt the syntax to your usage.

Hope it helps understand

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.