officer
officer copied to clipboard
Alternative text for tables with chunk option
As in davidgohel/officedown#53, but for tables. Same motivation, accessibility.
I haven't looked into much and as in pr davidgohel/officedown#53, I expect I'll make an attempt, although I find myself stressed for time at this moment....
table.alt
?
I didn't know it was possible. OK!
Hey @davidgohel !
Friday afternoons are great for inspiration, don't you think? :)
I've made a hook that does the trick. Do you have any comments on names or anything else before I proceed with a pull req?
```{r setup, include=FALSE}
library(knitr)
default_source_hook <- knit_hooks$get('chunk')
knit_hooks$set(
chunk = function(x, options) {
if(is.character(options$tab.alt) || is.character(options$tab.altHeader)) {
if(!is.character(options$tab.alt)) options$tab.alt <- ""
if(!is.character(options$tab.altHeader)) options$tab.altHeader <- ""
ooxml <- paste0("<w:tblCaption w:val=\"%s\" />",
"<w:tblDescription w:val=\"%s\" />")
ooxml <- sprintf(ooxml, options$tab.altHeader, options$tab.alt)
x <- gsub("</w:tblPr>",paste0(ooxml,"</w:tblPr>"),x)
}
default_source_hook(x, options)
}
)
```
```{r tab.cap="caption 1", tab.id="mtcars", tab.altHeader="Table with mtcars data", tab.alt="This is the famous mtcars data."}
head(mtcars)```
A note on tab.altHeader. tab.alt (tblDescription) is the one that matters for accessibility, or so I understand. In some versions of word, there is support for this also for figures, I've noticed. What do you think? In or out? If in, I can update the code for fig also.
It needs to be done in officer. Sorry, I can't work on the subject for now. Please wait, I'll do the job in officer and let you know when officedown will be able to use it.
ok, @davidgohel!
This is now complete. Chunk options tab.alt.title
and tab.alt.description
can be used as alternative text for tables in Word (with officer and flextable since few versions and with officedown now).
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.