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 officer or 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 officer or 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 presentation 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 using the template_path() method.

presentation <- new_presentation()
#> [1] "/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/officer/template/template.pptx"

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

# 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")
  system.file(package = "officer", "template/template.pptx"),
template_path(presentation) <- new_template_path
#> [1] "/var/folders/zh/y8vmg_8j29v3h7w1b434qf8m0000gn/T//Rtmp0BwJA4/new_template3b2a18de3eba.pptx"

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)
#> [1] "/var/folders/zh/y8vmg_8j29v3h7w1b434qf8m0000gn/T//Rtmp0BwJA4/new_template3b2a18de3eba.pptx"

And the code will fail if the template path doesn’t point to an existing file.

#> Error in validObject(.Object): invalid class "R2PptxPresentation" object: Template path must be a valid file. File `some_fake_template_path.pptx` not found
template_path(presentation) <- "some_fake_template_path.pptx"
#> Error in validObject(x): invalid class "R2PptxPresentation" object: Template path must be a valid file. File `some_fake_template_path.pptx` not found

Finally, if you find yourself using the same template path over and over, you can change the default the r2pptx default template path using options().

options("default_pptx_template" = new_template_path)
presentation <- new_presentation()
#> [1] "/var/folders/zh/y8vmg_8j29v3h7w1b434qf8m0000gn/T//Rtmp0BwJA4/new_template3b2a18de3eba.pptx"