add object into a new shape in the current slide. This function is able to add all supported outputs to a presentation. See section Methods (by class) to see supported outputs.
ph_with(x, value, location, ...)# S3 method for character
ph_with(x, value, location, ...)
# S3 method for numeric
ph_with(x, value, location, format_fun = format, ...)
# S3 method for factor
ph_with(x, value, location, ...)
# S3 method for logical
ph_with(x, value, location, format_fun = format, ...)
# S3 method for block_list
ph_with(x, value, location, level_list = integer(0), ...)
# S3 method for unordered_list
ph_with(x, value, location, ...)
# S3 method for data.frame
ph_with(
x,
value,
location,
header = TRUE,
tcf = table_conditional_formatting(),
alignment = NULL,
...
)
# S3 method for gg
ph_with(x, value, location, res = 300, alt_text, scale = 1, ...)
# S3 method for plot_instr
ph_with(x, value, location, res = 300, ...)
# S3 method for external_img
ph_with(x, value, location, use_loc_size = TRUE, ...)
# S3 method for fpar
ph_with(x, value, location, ...)
# S3 method for empty_content
ph_with(x, value, location, ...)
# S3 method for xml_document
ph_with(x, value, location, ...)
an rpptx object
object to add as a new shape. Supported objects are vectors, data.frame, graphics, block of formatted paragraphs, unordered list of formatted paragraphs, pretty tables with package flextable, editable graphics with package rvg, 'Microsoft' charts with package mschart.
a placeholder location object.
It will be used to specify the location of the new shape. This location
can be defined with a call to one of the ph_location functions. See
section "see also"
.
further arguments passed to or from other methods. When
adding a ggplot
object or plot_instr
, these arguments will be used
by png function.
format function for non character vectors
The list of levels for hierarchy structure as integer values. If used the object is formated as an unordered list. If 1 and 2, item 1 level will be 1, item 2 level will be 2.
display header if TRUE
conditional formatting settings defined by table_conditional_formatting()
alignment for each columns, 'l' for left, 'r' for right and 'c' for center. Default to NULL.
resolution of the png image in ppi
Alt-text for screen-readers
Multiplicative scaling factor, same as in ggsave
if set to FALSE, external_img width and height will be used.
ph_with(character)
: add a character vector to a new shape on the
current slide, values will be added as paragraphs.
ph_with(numeric)
: add a numeric vector to a new shape on the
current slide, values will be be first formatted then
added as paragraphs.
ph_with(factor)
: add a factor vector to a new shape on the
current slide, values will be be converted as character and then
added as paragraphs.
ph_with(block_list)
: add a block_list
made
of fpar
to a new shape on the current slide.
ph_with(unordered_list)
: add a unordered_list
made
of fpar
to a new shape on the current slide.
ph_with(data.frame)
: add a data.frame to a new shape on the current slide with
function block_table()
. Use package flextable
instead for more
advanced formattings.
ph_with(gg)
: add a ggplot object to a new shape on the
current slide. Use package rvg
for more advanced graphical features.
ph_with(plot_instr)
: add an R plot to a new shape on the
current slide. Use package rvg
for more advanced graphical features.
ph_with(external_img)
: add a external_img
to a new shape
on the current slide.
When value is a external_img object, image will be copied
into the PowerPoint presentation. The width and height
specified in call to external_img
will be
ignored, their values will be those of the location,
unless use_loc_size is set to FALSE.
ph_with(fpar)
: add an fpar
to a new shape
on the current slide as a single paragraph in a block_list
.
ph_with(empty_content)
: add an empty_content
to a new shape
on the current slide.
ph_with(xml_document)
: add an xml_document object to a new shape on the
current slide. This function is to be used to add custom openxml code.
ph_location_type, ph_location, ph_location_label, ph_location_left, ph_location_right, ph_location_fullsize, ph_location_template
# this name will be used to print the file
# change it to "youfile.pptx" to write the pptx
# file in your working directory.
fileout <- tempfile(fileext = ".pptx")
doc_1 <- read_pptx()
sz <- slide_size(doc_1)
# add text and a table ----
doc_1 <- add_slide(doc_1, layout = "Two Content", master = "Office Theme")
doc_1 <- ph_with(
x = doc_1, value = c("Table cars"),
location = ph_location_type(type = "title")
)
doc_1 <- ph_with(
x = doc_1, value = names(cars),
location = ph_location_left()
)
doc_1 <- ph_with(
x = doc_1, value = cars,
location = ph_location_right()
)
# add a base plot ----
anyplot <- plot_instr(code = {
col <- c(
"#440154FF", "#443A83FF", "#31688EFF",
"#21908CFF", "#35B779FF", "#8FD744FF", "#FDE725FF"
)
barplot(1:7, col = col, yaxt = "n")
})
doc_1 <- add_slide(doc_1)
doc_1 <- ph_with(doc_1, anyplot,
location = ph_location_fullsize(),
bg = "#006699"
)
# add a ggplot2 plot ----
if (require("ggplot2")) {
doc_1 <- add_slide(doc_1)
gg_plot <- ggplot(data = iris) +
geom_point(
mapping = aes(Sepal.Length, Petal.Length),
size = 3
) +
theme_minimal()
doc_1 <- ph_with(
x = doc_1, value = gg_plot,
location = ph_location_type(type = "body"),
bg = "transparent"
)
doc_1 <- ph_with(
x = doc_1, value = "graphic title",
location = ph_location_type(type = "title")
)
}
# add a external images ----
doc_1 <- add_slide(doc_1,
layout = "Title and Content",
master = "Office Theme"
)
doc_1 <- ph_with(
x = doc_1, value = empty_content(),
location = ph_location(
left = 0, top = 0,
width = sz$width, height = sz$height, bg = "black"
)
)
svg_file <- file.path(R.home(component = "doc"), "html/Rlogo.svg")
if (require("rsvg")) {
doc_1 <- ph_with(
x = doc_1, value = "External images",
location = ph_location_type(type = "title")
)
doc_1 <- ph_with(
x = doc_1, external_img(svg_file, 100 / 72, 76 / 72),
location = ph_location_right(), use_loc_size = FALSE
)
doc_1 <- ph_with(
x = doc_1, external_img(svg_file),
location = ph_location_left(),
use_loc_size = TRUE
)
}
# add a block_list ----
dummy_text <- readLines(system.file(
package = "officer",
"doc_examples/text.txt"
))
fp_1 <- fp_text(bold = TRUE, color = "pink", font.size = 0)
fp_2 <- fp_text(bold = TRUE, font.size = 0)
fp_3 <- fp_text(italic = TRUE, color = "red", font.size = 0)
bl <- block_list(
fpar(ftext("hello world", fp_1)),
fpar(
ftext("hello", fp_2),
ftext("hello", fp_3)
),
dummy_text
)
doc_1 <- add_slide(doc_1)
doc_1 <- ph_with(
x = doc_1, value = bl,
location = ph_location_type(type = "body")
)
# fpar ------
fpt <- fp_text(
bold = TRUE, font.family = "Bradley Hand",
font.size = 150, color = "#F5595B"
)
hw <- fpar(
ftext("hello ", fpt),
hyperlink_ftext(
href = "https://cran.r-project.org/index.html",
text = "cran", prop = fpt
)
)
doc_1 <- add_slide(doc_1)
doc_1 <- ph_with(
x = doc_1, value = hw,
location = ph_location_type(type = "body")
)
# unordered_list ----
ul <- unordered_list(
level_list = c(1, 2, 2, 3, 3, 1),
str_list = c("Level1", "Level2", "Level2", "Level3", "Level3", "Level1"),
style = fp_text(color = "red", font.size = 0)
)
doc_1 <- add_slide(doc_1)
doc_1 <- ph_with(
x = doc_1, value = ul,
location = ph_location_type()
)
print(doc_1, target = fileout)
Run the code above in your browser using DataLab