knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
PowerPoint templates and layouts are the driving force of
r2pptx. David Gohel's officeverse documentation is excellent, and covers the bases on creating new templates. I'll try to fill in
some of the gaps that someone newer to using
r2pptx might be wondering about.
In the way we use them for these packages, PowerPoint templates are just normal
.pptx files. Explicitly, they are not
.potx files --
officer cannot read these files. PowerPoint templates have two sections relevant here -- the normal slides, and the slide master.
The normal slides are what you would see if you presented the slide deck. If you have existing slides in the
.pptx file you use as your template for
r2pptx, your output slide deck
will start with those pre-existing slides.
The slide master contains what we call layouts. These are blueprints for how new slides will be created. In the layouts, placeholders are created to stand in for content you will input later on -- for example an empty title text box. These are very useful for us, because they let us define style, size, and location for elements through the PowerPoint UI that we can then fill later using
r2pptx. Otherwise, we would need to define the style, size, and location of each element we created using R -- potentially more exact and reproducible, but much harder.
We set the template path for the slide decks we create in
r2pptx via the
object (the variable doesn't have to be named
presentation). By default this is the
default template included in the
officer package. We can get the template path
presentation <- new_presentation() print(template_path(presentation))
We can also set a new path using the
template_path()<- method. When a new template path is set, R will check to make sure a file exists at that path. If you have any
.pptx file in your computer, you can try subbing in that file path as the value of
# for the example, make a copy the default template and pretend it's a totally # different template (since that's all we have on the testing server) new_template_path <- tempfile(pattern = "new_template", fileext = ".pptx") file.copy( system.file(package = "officer", "template/template.pptx"), new_template_path )
template_path(presentation) <- new_template_path print(template_path(presentation))
You can also set the template path when you create the presentation -- this is probably the easier way to set your template path.
presentation <- new_presentation(new_template_path) print(template_path(presentation))
And the code will fail if the template path doesn't point to an existing file.
template_path(presentation) <- "some_fake_template_path.pptx"
Finally, if you find yourself using the same template path over and over, you can change the default
r2pptx default template path using
options("default_pptx_template" = new_template_path) presentation <- new_presentation() print(template_path(presentation))
# remove the copied template unlink(new_template_path)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.