Skip to content

Instantly share code, notes, and snippets.

@markheckmann
Last active October 8, 2025 14:18
Show Gist options
  • Save markheckmann/a2b01b2906ea25c89f9835bcf5ad9e1c to your computer and use it in GitHub Desktop.
Save markheckmann/a2b01b2906ea25c89f9835bcf5ad9e1c to your computer and use it in GitHub Desktop.
WIP: pass dots to ph_with method. Working version for all methods.
### Prepare ----------------------------------------
library(devtools)
dev_mode(TRUE)
devtools::install_github("markheckmann/officer", "aec62c3")
library(officer)
library(ggplot2)
### Example 1 ----------------------------------------
loc <- "body[1]"
layout <- "Two Content"
dots <- list(ln = "red", rot = 3, geom = "round2SameRect", bg = "#ff000015")
fp <- fpar(ftext("text for first page header", prop = fp_text_lite(color = "red", font.size = 40, bold = TRUE)))
bl <- block_list(fp, fp)
ul <- unordered_list(
level_list = c(1, 2, 1),
str_list = c("Level1", "Level2", "Level1"),
style = list(
fp_text(color = "red", font.size = 0),
fp_text(color = "pink", font.size = 0),
fp_text(color = "orange", font.size = 0)
))
url <- "https://i.imgur.com/Wwbw7a2.png"
file_png <- tempfile(fileext = ".png")
download.file(url = url, destfile = file_png)
ext_img <- external_img(file_png, width = 4, height = 3)
p <- ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
theme_minimal(base_size = 15) +
theme(
plot.background = element_rect(fill = "transparent", colour = NA),
panel.background = element_rect(fill = "transparent", colour = NA)
)
x <- read_pptx() |>
add_slide(layout, title = "ph_with.character()") |>
ph_with(location = loc, "The body", .dots = dots) |>
add_slide(layout, title = "ph_with.numeric()") |>
ph_with(location = loc, 1:3, .dots = dots) |>
add_slide(layout, title = "ph_with.factor()") |>
ph_with(location = loc, factor(letters[1:3]), .dots = dots) |>
add_slide(layout, title = "ph_with.Date()") |>
ph_with(location = loc, Sys.Date(), date_format = "%D", .dots = dots) |>
add_slide(layout, title = "ph_with.fpar()") |>
ph_with(location = loc, fp, .dots = dots) |>
add_slide(layout, title = "ph_with.block_list()") |>
ph_with(location = loc, bl, .dots = dots) |>
add_slide(layout, title = "ph_with.unordered_list()") |>
ph_with(location = loc, ul, .dots = dots) |>
add_slide(layout, title = "ph_with.empty_content()") |>
ph_with(location = loc, empty_content(), .dots = dots) |>
add_slide(layout, title = "ph_with.external_img()") |> # not: geom
ph_with(location = loc, ext_img, use_loc_size = FALSE, .dots = dots) |>
add_slide(layout, title = "ph_with.plot_instr()") |> # not: geom
ph_with(location = loc, plot_instr(code = barplot(1:5, col = 2:6)), .dots = dots) |>
add_slide(layout, title = "ph_with.gg()") |> # not: geom
ph_with(location = loc, p, .dots = dots, scaling = 2) |>
add_slide(layout, title = "ph_with.data.frame()") |> # not: geom, ln, bg, rotation
ph_with(location = loc, mtcars[1:3, ], .dots = dots, width = 9)
x |> print(preview = TRUE)
### Example 2 ----------------------------------------
layout <- "Two Content"
loc <- "body"
file_png <- tempfile(fileext = ".png")
download.file("https://i.imgur.com/Wwbw7a2.png", file_png)
ext_img <- external_img(file_png, width = 4, height = 3)
my_line <- sp_line(color = "blue", lty = "dot", linecmpd = "dbl", lwd = 3)
anyplot <- plot_instr(code = barplot(1:5, col = 2:6))
x <- read_pptx() |>
# external_img
add_slide(layout, title = "ph_with.external_img()") |> # not: geom
ph_with(ext_img, loc, use_loc_size = FALSE, rot = 10, bg = "#ff000015") |>
ph_with(ext_img, loc, use_loc_size = FALSE, rot = -10, left = 5, top = 2.5, bg = "#ff00ff15") |>
ph_with(ext_img, loc, use_loc_size = FALSE, rot = 5, left = 3, top = 4, bg = "#0000ff15", ln = my_line) |>
# gg
add_slide(layout, title = "ph_with.gg()") |> # not: geom
ph_with(p, loc, rot = 10, bg = "#ff000015") |>
ph_with(p, loc, rot = 5, left = 5, top = 2, bg = "#ff00ff15", scaling = 2, ln = my_line) |>
# plot_instr
add_slide(layout, title = "ph_with.plot_instr()") |> # not: geom
ph_with(anyplot, loc, rot = 5, bg = "#ff000015", ln = "#ff000060") |>
ph_with(anyplot, loc, rot = -5, left = 5, top = 2, bg = "#0000ff15", ln = my_line, scaling = 1.5)
x |> print(preview = TRUE)
### End ----------------------------------------
dev_mode(FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment