diff --git a/DESCRIPTION b/DESCRIPTION index 243fdd06..02f6cdd5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "david.gohel@ardata.fr", role = c("aut", "cre")), person("Stefan", "Moog", , "moogs@gmx.de", role = "aut"), diff --git a/NEWS.md b/NEWS.md index b0bc4912..e71f19f7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/pptx_informations.R b/R/pptx_informations.R index 8c96cb18..ed52c788 100644 --- a/R/pptx_informations.R +++ b/R/pptx_informations.R @@ -97,12 +97,12 @@ 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)) @@ -110,29 +110,29 @@ plot_layout_properties <- function (x, layout = NULL, master = NULL, labels = TR 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") } diff --git a/man/plot_layout_properties.Rd b/man/plot_layout_properties.Rd index 57a54fb3..780a60e3 100644 --- a/man/plot_layout_properties.Rd +++ b/man/plot_layout_properties.Rd @@ -31,9 +31,9 @@ visualise placeholders locations and identifier. } \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) } \seealso{ Other functions for reading presentation informations: diff --git a/tests/testthat/_snaps/pptx-info/plot-content-order-nolabel.png b/tests/testthat/_snaps/pptx-info/plot-content-order-nolabel.png new file mode 100644 index 00000000..421ddcfa Binary files /dev/null and b/tests/testthat/_snaps/pptx-info/plot-content-order-nolabel.png differ diff --git a/tests/testthat/_snaps/pptx-info/plot-content-order.png b/tests/testthat/_snaps/pptx-info/plot-content-order.png new file mode 100644 index 00000000..a9209c08 Binary files /dev/null and b/tests/testthat/_snaps/pptx-info/plot-content-order.png differ diff --git a/tests/testthat/docs_dir/test-content-order.pptx b/tests/testthat/docs_dir/test-content-order.pptx new file mode 100644 index 00000000..77edc1da Binary files /dev/null and b/tests/testthat/docs_dir/test-content-order.pptx differ diff --git a/tests/testthat/test-pptx-info.R b/tests/testthat/test-pptx-info.R index 79c070ac..dc4c68df 100644 --- a/tests/testthat/test-pptx-info.R +++ b/tests/testthat/test-pptx-info.R @@ -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( @@ -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") })