Skip to content

Commit

Permalink
feat: remove_slide() gains new argument rm_images to enable image…
Browse files Browse the repository at this point in the history
…s deletion

fix #565
  • Loading branch information
davidgohel committed May 5, 2024
1 parent 762d475 commit 66acdea
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: officer
Type: Package
Title: Manipulation of Microsoft Word and PowerPoint Documents
Version: 0.6.6.003
Version: 0.6.6.004
Authors@R: c(
person("David", "Gohel", role = c("aut", "cre"), email = "david.gohel@ardata.fr"),
person("Stefan", "Moog", role = "aut", email = 'moogs@gmx.de'),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- add coverage to rtf, contributions from Davide Garolini at roche.
- `cursor_reach()` gains new argument `fixed` to enable exact matching.
It defaults to FALSE for backward compatibility.
- `remove_slide()` gains new argument `rm_images` to enable images deletion
from ppt/media.

# officer 0.6.5

Expand Down
11 changes: 10 additions & 1 deletion R/pptx_slide_manip.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@ on_slide <- function(x, index) {
#' @description Remove a slide from a pptx presentation.
#' @param x an rpptx object
#' @param index slide index, default to current slide position.
#' @param rm_images if TRUE (defaults to FALSE), images presented in
#' the slide to remove are also removed from the file.
#' @note cursor is set on the last slide.
#' @examples
#' my_pres <- read_pptx()
#' my_pres <- add_slide(my_pres)
#' my_pres <- remove_slide(my_pres)
#' @family functions slide manipulation
#' @seealso [read_pptx()], [ph_with()], [ph_remove()]
remove_slide <- function(x, index = NULL) {
remove_slide <- function(x, index = NULL, rm_images = FALSE) {
l_ <- length(x)
if (l_ < 1) {
stop("presentation contains no slide to delete", call. = FALSE)
Expand All @@ -111,6 +113,13 @@ remove_slide <- function(x, index = NULL) {
filename <- basename(x$presentation$slide_data()$target[index])
location <- which(x$slide$get_metadata()$name %in% filename)

if (rm_images) {
media_files <- pptx_summary(x)$media_file
if (length(media_files)) {
unlink(file.path(x$package_dir, media_files), force = TRUE)
}
}

del_file <- x$slide$remove_slide(location)

# update presentation elements
Expand Down
5 changes: 4 additions & 1 deletion man/remove_slide.Rd

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

0 comments on commit 66acdea

Please sign in to comment.