You can install {altcheckr} from GitHub using the {remotes} package.
Use the alt_get()
function to scrape the attributes of
each <img>
element on a web page that you name in the
url
argument,
The function uses {xml2} and {rvest} to scrape a given web page and extract image attributes, with a little bit of {purrr} to get it into a data frame.
The function returns a tibble where each row is an image element from
that page and columns are the the image source (src
), alt
text (alt
) and link to a file with a longer description
(longdesc
), if it exists (sometimes used for complex
images). The alt
column will be created and filled with
NA
if it isn’t present.
Setting the argument all_attributes
to TRUE
will return all the attributes provided in the <img>
element, not just src
, alt
and
longdesc
.
Here is a preview of the tibble that is output from
alt_get()
:
print(get_img)
#> # A tibble: 102 × 2
#> src alt
#> <chr> <chr>
#> 1 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/08f1/live/61c4aaa0… An U…
#> 2 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/488a/live/63fe40c0… Hann…
#> 3 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/e009/live/53be9ab0… Rach…
#> 4 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/9572/live/ff2a9890… A py…
#> 5 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/8bef/live/9dc756c0… Anne…
#> 6 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/25c2/live/81c6fbc0… Kati…
#> 7 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/b86c/live/2f1e5e00… Dona…
#> 8 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/c22b/live/ddb3c8e0… Woma…
#> 9 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/4f1e/live/ac404c00… Kemi…
#> 10 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/2f4c/live/14fa6b40… An e…
#> # ℹ 92 more rows
You can then pass the output of alt_get()
to
alt_check()
to perform a series of basic assessments of
each image’s alt text.
(You can also pass any data frame that contains a src
and alt
column, where alt
contains the text to
be assessed by alt_check()
. For example, {altcheckr} has a
built-in dataset: example_get
.)
This will return the same tibble as alt_get()
, but new
columns have now been appended.
Each new column is the outcome of a check for a possible accessibility issue with the alt text. For example, whether the alt text actually exists and whether it is long.
print(check_img)
#> # A tibble: 102 × 10
#> src alt alt_exists nchar_count nchar_assess file_ext self_evident
#> <chr> <chr> <chr> <int> <chr> <lgl> <lgl>
#> 1 https://iche… An U… Exists 135 Long FALSE TRUE
#> 2 https://iche… Hann… Exists 32 OK FALSE FALSE
#> 3 https://iche… Rach… Exists 272 Long FALSE FALSE
#> 4 https://iche… A py… Exists 63 OK FALSE FALSE
#> 5 https://iche… Anne… Exists 419 Long FALSE TRUE
#> 6 https://iche… Kati… Exists 60 OK FALSE FALSE
#> 7 https://iche… Dona… Exists 21 OK FALSE FALSE
#> 8 https://iche… Woma… Exists 49 OK FALSE FALSE
#> 9 https://iche… Kemi… Exists 35 OK FALSE FALSE
#> 10 https://iche… An e… Exists 177 Long FALSE TRUE
#> # ℹ 92 more rows
#> # ℹ 3 more variables: terminal_punct <lgl>, spellcheck <list>, not_basic <list>
And here is the structure now:
dplyr::glimpse(check_img)
#> Rows: 102
#> Columns: 10
#> $ src <chr> "https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/08…
#> $ alt <chr> "An UNRWA worker and displaced Palestinians check the d…
#> $ alt_exists <chr> "Exists", "Exists", "Exists", "Exists", "Exists", "Exis…
#> $ nchar_count <int> 135, 32, 272, 63, 419, 60, 21, 49, 35, 177, 68, 19, 135…
#> $ nchar_assess <chr> "Long", "OK", "Long", "OK", "Long", "OK", "OK", "OK", "…
#> $ file_ext <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,…
#> $ self_evident <lgl> TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, F…
#> $ terminal_punct <lgl> FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, F…
#> $ spellcheck <list> "UNRWA", <>, <"Wes", "Streeting">, <"Calakmul", "Campe…
#> $ not_basic <list> <"an", "unrwa", "worker", "displaced", "palestinians",…