-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add step in pkgdown workflow to ensure that all figures in Rmd documents have an alt text #129
Comments
In my comment on #101, this was one of the things I included in the # - [X] Verify all images include helpful alt-text
rmd_files <- list.files(pattern = "Rmd", recursive = TRUE)
with_plot <- files_with(rmd_files, "plot\\(") # covers ggplot as well
with_fig_alt <- files_with(rmd_files, "fig\\.alt")
result <- rmd_files[xor(with_fig_alt, with_plot)]
if (length(result) > 0) {
say("❌ No alt-text in the following files")
say(result)
say("[Example: ```{r, fig.alt = 'Alt text here'}]")
} else {
say("🟢 All images include helpful alt-text")
} Happy to look into integrating it in the pkgdown workflow if you want. |
Thanks! I agree the In this specific case, the proposed code would not detect files that contain some figures without all text. The R solution would be to use the xml2 package. Here is a code sample that should be used to loop over all the HTML files in the xml2::read_html("https://github.com/epiverse-trace/ColOpenData/raw/gh-pages/articles/demographic_data.html") |>
xml2::xml_find_all("//img[not(@alt)]")
#> {xml_nodeset (2)}
#> [1] <img src="demographic_data_files/figure-html/data%20filtering%20and%20plo ...
#> [2] <img src="demographic_data_files/figure-html/other%20data%20plot-1.png" w ... Created on 2024-02-28 with reprex v2.0.2 |
pages_without_alt <- list.files(pattern = ".html$", full.names = TRUE, recursive = TRUE) |>
lapply(function(page) {
figs_without_alt <- xml2::read_html(page) |>
xml2::xml_find_all("//img[not(@alt)]")
return(list(page = basename(page), missing_alts = length(figs_without_alt)))
}) |>
do.call(rbind.data.frame, args = _)
stop(sum(pages_without_alt$missing_alts > 0), " pages include figures without alt text.") |
The only part that is a bit disappointing is that it'd be super difficult to point out exactly where in the file the issue is since it'd require us to keep a correspondence table between lines in the HTML and corresponding Rmd document. |
According to In ggplot2/README.Rmd they specify
One option is to standardize on
Here is a code snippet that uses the source files It assumes
# install.packages("parsermd")
parse_rmd_files <- function() {
# inner function that does all the work
chunk_details <- function(filename) {
parsed <- parsermd::parse_rmd(filename)
ast <- parsermd::rmd_select(parsed, parsermd::has_type("rmd_chunk"))
missing_alt_text <- lapply(ast, function(chunk) {
caption <- !is.null(chunk$options$fig.cap)
plot <- length(grep("ggplot\\(", chunk$code)) > 0
alt_text <- !is.null(chunk$options$fig.alt)
sum(as.integer(c(plot, caption, alt_text))) == 1
})
missing_chunk_indices <- as.list(which(unlist(missing_alt_text)))
details <- lapply(missing_chunk_indices, function(index) {
chunk <- ast[[index]][["code"]]
name <- ast[[index]][["name"]]
list(file = filename, chunk_name = name, code = chunk)
})
details
}
# start's here
rmd_files <- list.files(pattern = "Rmd", recursive = TRUE)
results <- lapply(rmd_files, chunk_details)
results[lengths(results) > 0]
}
report <- parse_rmd_files()
I modified one of the two plots in diptheria.Rmd in {epidemics} to test it and it worked. I also ran it on the rest of {epidemics} and it picked out all the chunks in all the |
This is now addressed upstream in r-lib/pkgdown#2522. |
No description provided.
The text was updated successfully, but these errors were encountered: