Skip to content

Commit

Permalink
Add files in order of trailing numeric index (davidgohel#596)
Browse files Browse the repository at this point in the history
In class `dir_collection`, files are added to a container during
initialization using alphabetical sorting. This caused the slide
layout order to deviate from the one in the PPTX file, for example,
when calling layout_summary(). Files are now added to a container
in the order of their trailing numeric index. For example,
`slideLayout2.xml` will now preceed `slideLayout10.xml`. Before,
alphabetical sorting was used, where `slideLayout10.xml` comes before `slideLayout2.xml`.
  • Loading branch information
markheckmann committed Aug 31, 2024
1 parent c966124 commit 5823fbe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 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.007
Version: 0.6.7.008
Authors@R: c(
person("David", "Gohel", , "[email protected]", role = c("aut", "cre")),
person("Stefan", "Moog", , "[email protected]", role = "aut"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- complete the manual of `body_add_docx()` with a note about the file basename
that can not contain ' ' and trigger an error if it contains a ' '.
- `plot_layout_properties()` gains a 'title' parameter, which will add the layout name as the plot title. Defaults to `FALSE`, to not alter the old behavior. Also, the slide width and height are now correctly displayed in the plot. Before, a box was drawn around the plot area. However, the plot area var with device size, not slide size.
- class `dir_collection`: Files are now added to a container in the order of their trailing numeric index (#596).
For example, `slideLayout2.xml` will now preceed `slideLayout10.xml`. Before, alphabetical sorting was used, where `slideLayout10.xml` comes before `slideLayout2.xml`.

## Features

Expand Down
30 changes: 29 additions & 1 deletion R/ppt_class_dir_collection.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
# Extract trailing numeric index in .xml filename
#
# Useful to for slideMaster and slideLayout .xml files.
#
# Examples:
# files <- c("slideLayout1.xml", "slideLayout2.xml", "slideLayout10.xml")
# get_file_index(files)
#
get_file_index <- function(file) {
sub(pattern = ".+?(\\d+).xml$", replacement = "\\1", x = basename(file), ignore.case = TRUE) |> as.numeric()
}


# Sort xml filenames by trailing numeric index
#
# Useful to for slideMaster and slideLayout xml files.
#
# Examples:
# files <- c("slideLayout1.xml", "slideLayout2.xml", "slideLayout12.xml")
# sort_by_index(files) # => order corresponding to trailing index
# sort(files) # => incorrect lexicographical ordering
#
sort_by_index <- function(x) {
indexes <- get_file_index(x) # only
x[order(indexes)]
}


# dir_collection ---------------------------------------------------------
dir_collection <- R6Class(
"dir_collection",
Expand All @@ -6,8 +34,8 @@ dir_collection <- R6Class(
initialize = function( package_dir, container ) {
dir_ <- file.path(package_dir, container$dir_name())
private$package_dir <- package_dir

filenames <- list.files(path = dir_, pattern = "\\.xml$", full.names = TRUE)
filenames <- sort_by_index(filenames) # see issue 596
private$collection <- lapply( filenames, function(x, container){
container$clone()$feed(x)
}, container = container)
Expand Down
Binary file added inst/doc_examples/many_layouts.pptx
Binary file not shown.

0 comments on commit 5823fbe

Please sign in to comment.