Skip to content

Commit

Permalink
plot_layout_properties(): args layout accepts index (davidgohel#595)
Browse files Browse the repository at this point in the history
Accept the layout index (see `layout_summary()`) as alternative to
the layout name (suggestion 2 in davidgohel#595).
  • Loading branch information
markheckmann committed Oct 1, 2024
1 parent 5a363bf commit a774c94
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 8 additions & 6 deletions R/pptx_informations.R
Original file line number Diff line number Diff line change
Expand Up @@ -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*).
Expand All @@ -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))

Expand All @@ -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?"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions man/plot_layout_properties.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/test-pptx-info.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a774c94

Please sign in to comment.