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: 99 × 2
#> src alt
#> <chr> <chr>
#> 1 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/9f98/live/c613ce60… "The…
#> 2 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/f9df/live/af39c7c0… "A f…
#> 3 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/974c/live/10666b20… "Mar…
#> 4 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/ee7c/live/34e84000… "A m…
#> 5 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/8c65/live/62230a00… "A t…
#> 6 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/e2a8/live/8c3af950… "Fli…
#> 7 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/fe19/live/6ed47bd0… "A s…
#> 8 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/05e1/live/ab6022d0… "Sna…
#> 9 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/5cd2/live/eafe3dd0… "Rut…
#> 10 https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/2306/live/c4e64220… "cec…
#> # ℹ 89 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: 99 × 10
#> src alt alt_exists nchar_count nchar_assess file_ext self_evident
#> <chr> <chr> <chr> <int> <chr> <lgl> <lgl>
#> 1 https://iche… "The… Exists 38 OK FALSE FALSE
#> 2 https://iche… "A f… Exists 170 Long FALSE FALSE
#> 3 https://iche… "Mar… Exists 110 OK FALSE FALSE
#> 4 https://iche… "A m… Exists 84 OK FALSE FALSE
#> 5 https://iche… "A t… Exists 35 OK FALSE TRUE
#> 6 https://iche… "Fli… Exists 44 OK FALSE FALSE
#> 7 https://iche… "A s… Exists 256 Long FALSE FALSE
#> 8 https://iche… "Sna… Exists 70 OK FALSE FALSE
#> 9 https://iche… "Rut… Exists 108 OK FALSE TRUE
#> 10 https://iche… "cec… Exists 58 OK FALSE FALSE
#> # ℹ 89 more rows
#> # ℹ 3 more variables: terminal_punct <lgl>, spellcheck <list>, not_basic <list>
And here is the structure now:
dplyr::glimpse(check_img)
#> Rows: 99
#> Columns: 10
#> $ src <chr> "https://ichef.bbci.co.uk/ace/standard/480/cpsprodpb/9f…
#> $ alt <chr> "The crashed plane at night in the dark", "A forensics …
#> $ alt_exists <chr> "Exists", "Exists", "Exists", "Exists", "Exists", "Exis…
#> $ nchar_count <int> 38, 170, 110, 84, 35, 44, 256, 70, 108, 58, 173, 194, 2…
#> $ nchar_assess <chr> "OK", "Long", "OK", "OK", "OK", "OK", "Long", "OK", "OK…
#> $ file_ext <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,…
#> $ self_evident <lgl> FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, …
#> $ terminal_punct <lgl> FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, F…
#> $ spellcheck <list> <>, "facemask", "Dorrian", <>, <>, "Heathrow", <"Bally…
#> $ not_basic <list> "crashed", <"forensics", "officer", "suit", "gloves", …