Skip to content

Commit

Permalink
Replace text at bkm with plots.
Browse files Browse the repository at this point in the history
* Add body_replace_gg_at_bkm.
* Add body_replace_plot_at_bkm.
  • Loading branch information
trekonom committed Aug 3, 2024
1 parent 9461342 commit 9632ae1
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 30 deletions.
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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ replaced with a hyphen-minus (Unicode character 002D). Closes #573.

## 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.

#' with different text.

- `docx_summary()` gains parameter 'detailed' which allows to get a detailed
summary including formatting properties of runs in a paragraph. Formatting
properties are stored in a list column `run`, where each element
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
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.

Loading

0 comments on commit 9632ae1

Please sign in to comment.