Skip to content

Commit

Permalink
Merge branch 'master' into improve_plot_layout_properties
Browse files Browse the repository at this point in the history
  • Loading branch information
markheckmann committed Aug 23, 2024
2 parents 9f4b32d + 626f18f commit e4f190a
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 42 deletions.
4 changes: 2 additions & 2 deletions 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.6
Version: 0.6.7.004
Authors@R: c(
person("David", "Gohel", , "[email protected]", role = c("aut", "cre")),
person("Stefan", "Moog", , "[email protected]", role = "aut"),
Expand Down Expand Up @@ -60,4 +60,4 @@ Suggests:
testthat
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ export(body_end_section_landscape)
export(body_end_section_portrait)
export(body_remove)
export(body_replace_all_text)
export(body_replace_gg_at_bkm)
export(body_replace_img_at_bkm)
export(body_replace_plot_at_bkm)
export(body_replace_text_at_bkm)
export(body_set_default_section)
export(change_styles)
Expand Down
17 changes: 15 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
# officer 0.6.6.9000
# officer 0.6.7

## Issues

- store paddings as numeric values and not integer values.
- remove_fields in `docx_summary()` now also removes "w:fldData" nodes.

## Features

- new convenience functions `body_replace_gg_at_bkm()` and `body_replace_plot_at_bkm()`
to replace text content enclosed in a bookmark with a ggplot or a base plot.
- add `unit` (in, cm, mm) argument in function `page_size()`.

# officer 0.6.6

## Issues

- Fix. `docx_summary` preserves non-breaking hyphens. Non-breaking hyphens are
replaced with a hyphen-minus (Unicode character 002D). Closes #573.

## Features
#' with different text.

- `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.
- `docx_summary()` gains parameter 'detailed' which allows to get a detailed
Expand Down
71 changes: 69 additions & 2 deletions R/docx_replace.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ docxpart_replace_img_at_bkm <- function(node, bookmark, value) {
xml_replace(run_nodes[[1]], as_xml_document(out))
}



#' @export
#' @rdname body_replace_text_at_bkm
headers_replace_text_at_bkm <- function( x, bookmark, value ){
Expand Down Expand Up @@ -156,6 +154,75 @@ footers_replace_img_at_bkm <- function( x, bookmark, value ){
x
}

#' @export
#' @title Add plots at bookmark location in a 'Word' document
#' @description
#' Use these functions if you want to replace a paragraph containing
#' a bookmark with a 'ggplot' or a base plot.
#' @param value a ggplot object for body_replace_gg_at_bkm() or a set plot instructions
#' body_replace_plot_at_bkm(), see plot_instr().
#' @param bookmark bookmark id
#' @param keep Should the bookmark be preserved? Defaults to `FALSE`,
#' i.e.the bookmark will be lost as a side effect.
#' @inheritParams body_add_gg
#'
#' @examples
#' if (require("ggplot2")) {
#' gg_plot <- ggplot(data = iris) +
#' geom_point(mapping = aes(Sepal.Length, Petal.Length))
#'
#' doc <- read_docx()
#' doc <- body_add_par(doc, "insert_plot_here")
#' doc <- body_bookmark(doc, "plot")
#' doc <- body_replace_gg_at_bkm(doc, bookmark = "plot", value = gg_plot)
#' print(doc, target = tempfile(fileext = ".docx"))
#' }
body_replace_gg_at_bkm <- function(x, bookmark, value, width = 6, height = 5,
res = 300, style = "Normal", scale = 1,
keep = FALSE, ...) {
x <- cursor_bookmark(x, bookmark)
x <- body_add_gg(
x = x, value = value, width = width, height = height,
res = res, style = style, scale = scale, pos = "on", ...
)
if (keep) {
x <- body_bookmark(x, bookmark)
}

x
}

#' @export
#' @rdname body_replace_gg_at_bkm
#' @examples
#' doc <- read_docx()
#' doc <- body_add_par(doc, "insert_plot_here")
#' doc <- body_bookmark(doc, "plot")
#' if (capabilities(what = "png")) {
#' doc <- body_replace_plot_at_bkm(
#' doc, bookmark = "plot",
#' value = plot_instr(
#' code = {
#' barplot(1:5, col = 2:6)
#' }
#' )
#' )
#' }
#' print(doc, target = tempfile(fileext = ".docx"))
body_replace_plot_at_bkm <- function(x, bookmark, value, width = 6, height = 5,
res = 300, style = "Normal",
keep = FALSE, ...) {
x <- cursor_bookmark(x, bookmark)
x <- body_add_plot(
x = x, value = value, width = width, height = height,
res = res, style = style, pos = "on", ...
)
if (keep) {
x <- body_bookmark(x, bookmark)
}

x
}

#' @export
#' @title Replace text anywhere in the document
Expand Down
8 changes: 4 additions & 4 deletions R/formatting_properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -663,16 +663,16 @@ fp_par <- function(text.align = "left",
)
)
if (!missing(padding.bottom)) {
out <- check_set_integer(obj = out, padding.bottom)
out <- check_set_numeric(obj = out, padding.bottom)
}
if (!missing(padding.left)) {
out <- check_set_integer(obj = out, padding.left)
out <- check_set_numeric(obj = out, padding.left)
}
if (!missing(padding.top)) {
out <- check_set_integer(obj = out, padding.top)
out <- check_set_numeric(obj = out, padding.top)
}
if (!missing(padding.right)) {
out <- check_set_integer(obj = out, padding.right)
out <- check_set_numeric(obj = out, padding.right)
}

out <- check_set_numeric(obj = out, line_spacing)
Expand Down
3 changes: 3 additions & 0 deletions R/fortify_docx.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ docx_summary <- function(x, preserve = FALSE, remove_fields = FALSE, detailed =
if (remove_fields) {
instrText_nodes <- xml_find_all(x$doc_obj$get(), "//w:instrText")
xml_remove(instrText_nodes)

fldData_nodes <- xml_find_all(x$doc_obj$get(), "//w:fldData")
xml_remove(fldData_nodes)
}

all_nodes <- xml_find_all(x$doc_obj$get(), "/w:document/w:body/*[self::w:p or self::w:tbl]")
Expand Down
7 changes: 6 additions & 1 deletion R/ooxml_run_objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,15 @@ inch_to_tweep <- function(x) round(x * 72 * 20, digits = 0)
#' in landscape mode then the length becomes the width and the width becomes the length.
#' @param width,height page width, page height (in inches).
#' @param orient page orientation, either 'landscape', either 'portrait'.
#' @param unit unit for width and height, one of "in", "cm", "mm".
#' @examples
#' page_size(orient = "landscape")
#' @family functions for section definition
page_size <- function(width = 21 / 2.54, height = 29.7 / 2.54, orient = "portrait") {
page_size <- function(width = 21 / 2.54, height = 29.7 / 2.54, orient = "portrait", unit = "in") {

width <- convin(unit = unit, x = width)
height <- convin(unit = unit, x = height)

if (orient %in% "portrait") {
h <- max(c(height, width))
w <- min(c(height, width))
Expand Down
2 changes: 1 addition & 1 deletion R/read_pptx.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ read_table_style <- function(path){
#' @param target path to the pptx file to write
#' @param ... unused
#' @examples
#' # write a rdocx object in a docx file ----
#' # write a rdocx object in a pptx file ----
#' file <- tempfile(fileext = ".pptx")
#' doc <- read_pptx()
#' print(doc, target = file)
Expand Down
85 changes: 85 additions & 0 deletions man/body_replace_gg_at_bkm.Rd

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

9 changes: 8 additions & 1 deletion man/page_size.Rd

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

2 changes: 1 addition & 1 deletion man/print.rpptx.Rd

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

Loading

0 comments on commit e4f190a

Please sign in to comment.