Skip to content

Commit

Permalink
Fix. Add alt text to ggplot's added to a pptx with ph_with.gg. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
trekonom committed Mar 16, 2024
1 parent 5889199 commit 55682a2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# officer 0.6.5.9000

## Issues

- Fix. Add alt text to ggplot's added to a pptx with ph_with.gg. Closes ##556.

# officer 0.6.5

## Features
Expand Down
15 changes: 11 additions & 4 deletions R/ppt_ph_with_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,10 @@ ph_with.data.frame <- function(x, value, location, header = TRUE,
#' @describeIn ph_with add a ggplot object to a new shape on the
#' current slide. Use package \code{rvg} for more advanced graphical features.
#' @param res resolution of the png image in ppi
#' @param alt_text Alt-text for screen-readers
#' @param alt_text Alt-text for screen-readers. Defaults to `""`. If `""` or `NULL`
#' an alt text added with `ggplot2::labs(alt = ...)` will be used if any.
#' @param scale Multiplicative scaling factor, same as in ggsave
ph_with.gg <- function(x, value, location, res = 300, alt_text, scale = 1, ...) {
ph_with.gg <- function(x, value, location, res = 300, alt_text = "", scale = 1, ...) {
location_ <- fortify_location(location, doc = x)
slide <- x$slide$get_slide(x$cursor)
if (!requireNamespace("ggplot2")) {
Expand All @@ -405,8 +406,14 @@ ph_with.gg <- function(x, value, location, res = 300, alt_text, scale = 1, ...)
dev.off()
on.exit(unlink(file))

ext_img <- external_img(file, width = width, height = height)
ph_with(x, ext_img, location = location, alt_text = alt_text)
if (is.null(alt_text) || alt_text == "") {
alt_text <- ggplot2::get_alt_text(value)
if (is.null(alt_text)) alt_text <- ""
}

ext_img <- external_img(file, width = width, height = height, alt = alt_text)

ph_with(x, ext_img, location = location)
}

#' @export
Expand Down
5 changes: 3 additions & 2 deletions man/ph_with.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions tests/testthat/test-alt-text.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
test_that("add alt text to ggplot", {
skip_if_not_installed("ggplot2")
require(ggplot2)

alt_text <- "Alt text added with 'alt_text='."

gg <- ggplot(mtcars, aes(factor(cyl))) +
geom_bar()

doc <- read_pptx()
doc <- add_slide(doc)
doc <- ph_with(doc,
value = gg,
location = ph_location_type("body"),
alt_text = alt_text
)

xmldoc <- doc$slide$get_slide(id = 1)$get()
expect_equal(
xml_attr(xml_find_all(xmldoc, "//p:nvPicPr//p:cNvPr"), "descr"),
alt_text
)

alt_labs <- "Alt text added with 'ggplot2::labs(alt=)'."
doc <- add_slide(doc)
doc <- ph_with(doc,
value = gg + labs(alt = alt_labs),
location = ph_location_type("body")
)
xmldoc <- doc$slide$get_slide(id = 2)$get()
expect_equal(
xml_attr(xml_find_all(xmldoc, "//p:nvPicPr//p:cNvPr"), "descr"),
alt_labs
)

doc <- add_slide(doc)
doc <- ph_with(doc,
value = gg,
location = ph_location_type("body")
)
xmldoc <- doc$slide$get_slide(id = 3)$get()
expect_equal(
xml_attr(xml_find_all(xmldoc, "//p:nvPicPr//p:cNvPr"), "descr"),
""
)
})

0 comments on commit 55682a2

Please sign in to comment.