diff --git a/NEWS.md b/NEWS.md index f2ef4677..57b89b1e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,6 +22,7 @@ informative error message if the type is not present in layout (#601). - `plot_layout_properties()` plots more information by default now: layout name, ph label, ph id, ph type + index by default (#606). - `ph_location_type()`: new `type_idx` arg replaces the deprecated `id` arg (#606). - Add `ph_location_id()` as a new member to the `ph_location_*` family. It references a placeholder via its unique id (#606). +- `plot_layout_properties()`: Accept the layout index (see `layout_summary()`) as alternative to the layout name (#595). ## Features diff --git a/R/pptx_informations.R b/R/pptx_informations.R index d4b21a34..8076a41f 100644 --- a/R/pptx_informations.R +++ b/R/pptx_informations.R @@ -118,7 +118,7 @@ layout_properties <- function(x, layout = NULL, master = NULL) { #' _NB_: The id is set by PowerPoint automatically and lack a meaningful order. #' #' @param x an `rpptx` object -#' @param layout slide layout name. +#' @param layout slide layout name or numeric index (row index from [layout_summary()). #' @param master master layout name where `layout` is located. #' @param title if `TRUE` (default), adds a title with the layout name at the top. #' @param labels if `TRUE` (default), adds placeholder labels (centered in *red*). @@ -129,19 +129,20 @@ layout_properties <- function(x, layout = NULL, master = NULL) { #' @param cex named list or vector to specify font size for `labels`, `type`, and `id`. Default is #' `c(labels = .5, type = .5, id = .5)`. See [graphics::text()] for details on how `cex` works. #' @importFrom graphics plot rect text box +#' @family functions for reading presentation information #' @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 = "Two Content") # no master needed for unique layout +#' plot_layout_properties(x = x, layout = 4) # use layout index #' plot_layout_properties(x = x, layout = "Two Content", title = FALSE, type = FALSE, id = FALSE) #' #' # change font size #' plot_layout_properties(x = x, layout = "Two Content", cex = c(labels = 1, id = .7, type = .7)) #' -#' @family functions for reading presentation information -#' plot_layout_properties <- function(x, layout = NULL, master = NULL, labels = TRUE, title = TRUE, type = TRUE, id = TRUE, cex = NULL) { + stop_if_not_rpptx(x, "x") old_par <- par(mar = c(2, 2, 1.5, 0)) on.exit(par(old_par)) @@ -155,7 +156,8 @@ plot_layout_properties <- function(x, layout = NULL, master = NULL, labels = TRU } .cex <- utils::modifyList(x = cex_default, val = as.list(cex), keep.null = TRUE) - dat <- layout_properties(x, layout = layout, master = master) + la <- get_layout(x, layout, master) + dat <- layout_properties(x, layout = la$layout_name, master = la$master_name) if (length(unique(dat$name)) > 1) { cli::cli_abort(c("One single layout must be chosen", "x" = "Did you supply a master?" @@ -183,7 +185,7 @@ plot_layout_properties <- function(x, layout = NULL, master = NULL, labels = TRU mtext("x [inch]", side = 1, line = 0, cex = 1.2, col = "darkgrey") if (title) { - title(main = paste("Layout:", layout)) + title(main = paste("Layout:", la$layout_name)) } if (labels) { # centered text(x = offx + cx / 2, y = -(offy + cy / 2), labels = dat$ph_label, cex = .cex$labels, col = "red", adj = c(.5, 1)) # adj-vert: avoid interference with type/id in small phs diff --git a/man/plot_layout_properties.Rd b/man/plot_layout_properties.Rd index b7071f39..5a1657c0 100644 --- a/man/plot_layout_properties.Rd +++ b/man/plot_layout_properties.Rd @@ -18,7 +18,7 @@ plot_layout_properties( \arguments{ \item{x}{an \code{rpptx} object} -\item{layout}{slide layout name.} +\item{layout}{slide layout name or numeric index (row index from [layout_summary()).} \item{master}{master layout name where \code{layout} is located.} @@ -56,7 +56,8 @@ function family: \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 = "Two Content") # no master needed for unique layout +plot_layout_properties(x = x, layout = 4) # use layout index plot_layout_properties(x = x, layout = "Two Content", title = FALSE, type = FALSE, id = FALSE) # change font size diff --git a/tests/testthat/test-pptx-info.R b/tests/testthat/test-pptx-info.R index adf1635b..1ffa1786 100644 --- a/tests/testthat/test-pptx-info.R +++ b/tests/testthat/test-pptx-info.R @@ -146,6 +146,26 @@ test_that("slide summary", { }) + +test_that("plot layout properties: layout arg takes numeric index", { + x <- read_pptx() + las <- layout_summary(x) + ii <- as.numeric(rownames(las)) + + discarded_plot <- function(x, layout = NULL, master = NULL) { # avoid Rplots.pdf creation + file <- tempfile(fileext = ".png") + png(file, width = 7, height = 6, res = 150, units = "in") + plot_layout_properties(x, layout, master) + dev.off() + } + + for (idx in ii) { + expect_no_error(discarded_plot(x, idx)) + } + expect_no_error(discarded_plot(x, 1, "Office Theme")) +}) + + test_that("color scheme", { x <- read_pptx() cs <- color_scheme(x)