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.
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
```
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:
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.