Skip to content

Instantly share code, notes, and snippets.

@iangow
Created April 26, 2024 18:21
Show Gist options
  • Save iangow/1c17d395491067b3fc0cdc488a3d9e12 to your computer and use it in GitHub Desktop.
Save iangow/1c17d395491067b3fc0cdc488a3d9e12 to your computer and use it in GitHub Desktop.
Code to produce a Word document detailing alternative text
---
title: "Alternative text for figures"
author: Ian D. Gow
date: 2024-04-25
date-format: "D MMMM YYYY"
bibliography: book.bib
format: docx
---
```{r}
#| include: false
library(tidyverse)
files <-
tibble(line = read_lines("_quarto.yml")) |>
filter(str_detect(line, ".qmd")) |>
mutate(file = str_extract(line, "[a-zA-Z0-9-]*\\.qmd")) |>
select(file) |>
pull()
get_fig_alts <- function(file) {
regex <- ' label: (fig-[a-zA-Z0-9_-]*).*?fig-alt: (".*?")'
df <-
read_lines(file) |>
str_c(collapse = " ") |>
str_match_all(pattern = regex) |>
as.data.frame()
colnames(df) <- c("full_match", "fig_label", "fig_alt")
df |>
mutate(file = file) |>
select(file, fig_label, fig_alt) |>
as_tibble()
}
get_fig_labels <- function(file) {
regex <- ' label: (fig-[a-zA-Z0-9_-]*)'
df <-
read_lines(file) |>
str_c(collapse = " ") |>
str_match_all(pattern = regex) |>
as.data.frame()
colnames(df) <- c("full_match", "fig_label")
df |>
mutate(file = file) |>
select(file, fig_label) |>
as_tibble()
}
fig_alt_df <-
map(files, get_fig_alts) |>
list_rbind()
fig_label_df <-
map(files, get_fig_labels) |>
list_rbind()
fig_label_df |>
left_join(fig_alt_df, by = join_by(file, fig_label)) |>
filter(is.na(fig_alt))
fig_label_df |>
left_join(fig_alt_df, by = join_by(file, fig_label)) |>
count(missing_fig_alt = is.na(fig_alt))
fig_label_df |>
left_join(fig_alt_df, by = join_by(file, fig_label)) |>
filter(is.na(fig_alt)) |>
count(file)
```
```{r}
#| echo: false
fig_label_df |>
left_join(fig_alt_df, by = join_by(file, fig_label)) |>
knitr::kable()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment