Skip to content

Commit

Permalink
fix: plot_layout_properties(): type and ids fixed for LABEL=FALSE
Browse files Browse the repository at this point in the history
ph_location_type() check for type id range (#602)

`ph_location_type()` now throws an informative error if ...
- `id` for a `type` is our of range (#602)
- type exists but is not present in current layout
  • Loading branch information
markheckmann authored Sep 11, 2024
1 parent 0c3fef3 commit b6f053c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: officer
Title: Manipulation of Microsoft Word and PowerPoint Documents
Version: 0.6.7.010
Version: 0.6.7.011
Authors@R: c(
person("David", "Gohel", , "[email protected]", role = c("aut", "cre")),
person("Stefan", "Moog", , "[email protected]", role = "aut"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ now sorts the resulting data by placeholder position. This yields an intuitive o
top to bottom and left to right.
- `ph_location_type()` now throws an error if the `id` for a `type` is out of range (#602) and a more
informative error message if the type is not present in layout (#601).
- `plot_layout_properties()` assignment order fixed for `labels= FALSE` (#604)

## Features

Expand Down
30 changes: 15 additions & 15 deletions R/pptx_informations.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,42 +97,42 @@ layout_properties <- function( x, layout = NULL, master = NULL ){
#' @importFrom graphics plot rect text box
#' @examples
#' x <- read_pptx()
#' plot_layout_properties( x = x, layout = "Title Slide",
#' master = "Office Theme" )
#' plot_layout_properties( x = x, layout = "Two Content" )
#' plot_layout_properties(x = x, layout = "Title Slide", master = "Office Theme")
#' plot_layout_properties(x = x, layout = "Two Content")
#' plot_layout_properties(x = x, layout = "Two Content", labels = FALSE)
#' @family functions for reading presentation informations
plot_layout_properties <- function (x, layout = NULL, master = NULL, labels = TRUE, title = FALSE)
{
#'
plot_layout_properties <- function(x, layout = NULL, master = NULL, labels = TRUE, title = FALSE) {
old_par <- par(mar = c(2, 2, 1.5, 0))
on.exit(par(old_par))

dat <- layout_properties(x, layout = layout, master = master)
if (length(unique(dat$name)) != 1) {
stop("one single layout need to be choosen")
}
dat <- dat[order(dat$type, as.integer(dat$id)), ] # set order for type idx. Removing the line would result in the default layout properties order, i.e., top->bottom left->right.
dat$type_idx <- stats::ave(dat$type, dat$type, FUN = seq_along) # NB: returns character index

s <- slide_size(x)
h <- s$height
w <- s$width
offx <- dat$offx
offy <- dat$offy
cx <- dat$cx
cy <- dat$cy
list2env(dat[, c("offx", "offy", "cx", "cy")], environment()) # make available inside functions

if (labels) {
labels <- dat$ph_label
} else {
labels <- dat$type[order(as.integer(dat$id))]
rle_ <- rle(labels)
labels <- sprintf("type: '%s' - id: %.0f", labels, unlist(lapply(rle_$lengths, seq_len)))
labels <- sprintf("type: '%s' - id: %s", dat$type, dat$type_idx)
}

plot(x = c(0, w), y = -c(0, h), asp = 1, type = "n", axes = FALSE, xlab = NA, ylab = NA)
if (title) {
title(main = paste("Layout:", layout))
}
rect(xleft = 0, xright = w, ybottom = 0, ytop = -h, border = "darkgrey")
rect(xleft = offx, xright = offx + cx, ybottom = -offy, ytop = -(offy + cy))
text(x = offx + cx/2, y = -(offy + cy/2), labels = labels, cex = 0.5, col = "red")
mtext("y [inch]", side = 2, line = 0, cex = 1.2, col="darkgrey")
mtext("x [inch]", side = 1, line = 0, cex = 1.2, col="darkgrey")
text(x = offx + cx / 2, y = -(offy + cy / 2), labels = labels, cex = 0.5, col = "red")
mtext("y [inch]", side = 2, line = 0, cex = 1.2, col = "darkgrey")
mtext("x [inch]", side = 1, line = 0, cex = 1.2, col = "darkgrey")
}


Expand Down
6 changes: 3 additions & 3 deletions man/plot_layout_properties.Rd

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/testthat/docs_dir/test-content-order.pptx
Binary file not shown.
27 changes: 27 additions & 0 deletions tests/testthat/test-pptx-info.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ test_that("plot layout properties", {
master = "Office Theme"
)
dev.off()

png2 <- tempfile(fileext = ".png")
png(png2, width = 7, height = 6, res = 150, units = "in")
plot_layout_properties(
Expand All @@ -99,8 +100,34 @@ test_that("plot layout properties", {
labels = FALSE
)
dev.off()

expect_snapshot_doc(name = "plot-titleslide-layout", x = png1, engine = "testthat")
expect_snapshot_doc(name = "plot-titleslide-layout-nolabel", x = png2, engine = "testthat")

# issue #604
p <- test_path("docs_dir/test-content-order.pptx")
x <- read_pptx(p)

png3 <- tempfile(fileext = ".png")
png(png3, width = 7, height = 6, res = 150, units = "in")
plot_layout_properties(
x = x, layout = "Many Contents",
master = "Office Theme",
labels = TRUE
)
dev.off()

png4 <- tempfile(fileext = ".png")
png(png4, width = 7, height = 6, res = 150, units = "in")
plot_layout_properties(
x = x, layout = "Many Contents",
master = "Office Theme",
labels = FALSE
)
dev.off()

expect_snapshot_doc(name = "plot-content-order", x = png3, engine = "testthat")
expect_snapshot_doc(name = "plot-content-order-nolabel", x = png4, engine = "testthat")
})


Expand Down

0 comments on commit b6f053c

Please sign in to comment.