Rmarkdown Powerpoint



  • Why is it important to be able to generate a PowerPoint report from R ?
  • Create a simple PowerPoint document
    • Format the text of a PowerPoint document
  • Create a PowerPoint document from a template file
  1. Rmarkdown is an R syntax that helps make documents in many different formats. In essence, you write a mixture of plain english with some different “code wrappers” to.
  2. This short video shows you how to use your RStudio (recent version) and make a Powerpoint Slide / Presentation directly from R (R Markdown).

PowerPoint presentation. To create a PowerPoint presentation from R Markdown, you specify the powerpointpresentation output format in the YAML metadata of your document. Please note that this output format is only available in rmarkdown = v1.9, and requires at least Pandoc v2.0.5. Learn to use markdown to format Power Automate approval requests.


There are at least, two reasons for this, as described in the next sections.

Reason I : Many collaborators works with Microsoft office tools

About 1 billion people worldwide use Microsoft Office (1 in 7 people on the planet; source: Microsoft).

Furthermore, many collaborators still working with MS Office software (Word, PowerPoint, Excel) for :

  • editing their text and tracking changes
  • copy-pasting texts, images and tables from multiple sources
  • saving and analyzing their data

In this context, a report generated as a PDF or HTMl files is less useful with some collaborators.

Reason II : keeping beautiful R graphs beautifull for publications

R plots can be customized to be as beautiful as your imagination can make them. Unfortunately, preserving this beauty is not always an easy task when you want to publish these graphs or show them in a professional presentations.

Yes, this problem can be solved using knitr/rmarkdown/Latex/Beamer/Slidify. However, it would be very difficult to remake the whole presentation in a different format. Furthermore, many journals don’t accept Latex documents.

(source).

The answer of this question is yes and no. There have been several attempts to solve this problem, but many of them are not easy to use.

One of the previous solutions is R2PPT package. Unfortunately R2PPT is available for Windows OS only and it depends on rcom or RDCOMClient for generating Microsoft PowerPoint presentations.

The goal of this R tutorial is to show you how to easily and quickly, format and export R outputs (including data tables, plots, paragraphs of text and R scripts) from R statistical software to a Microsoft PowerPoint document (.pptx file format) using ReporteRs package.

ReporteRs is a Java-based solution, so it works on Windows, Mac and Linux.

Use the R code below :

Note that ReporteRs relies on Java (>= 1.6) ; make sure you have an installed JRE

The version of Java installed on your computer, can be checked as follow :

Four simple steps are required :

  1. Use the pptx() function to create a PowerPoint object.
  2. Use the addSlide() function to add a slide into the PowerPoint document.
  3. Add contents into the created slide using the functions below :
    • addTitle: Add a title
    • addParagraph: Add paragraphs of text
    • addFlexTable: Add a table
    • addPlot: Add a plot generated in R
    • addImage: Add external images
    • addRScript: Highlight and add R code
    • addDate : Add a date
    • addPageNumber : Add a page number
    • AddFooter : Add a footer
  4. Write the document into a .pptx file using writeDoc() function

Slide layout

Before showing you an example of how to create and format PowerPoint from R Software, let’s first discuss about slide layout. This is very important to understand the examples provided in this tutorial.

When creating a new slide, you should specify the layout of the slide. The available layouts in the “MS Office PowerPoint” (default template, on my computer) are illustrated in the figure below :

addSlide() function can be used to add a new slide into a PowerPoint document from R software. A simplified format of the function is :


  • doc : a pptx object where slide has to be added
  • slide.layout : the layout to use for the slide.


Among the possible values for the argument slide.layout, there are : “Title Slide”, “Title and Content”, “Two Content”, “Section Header”, “Content with Caption”, “Title Only”, “Comparison”.

However, you should use only the available slide layouts in your computer.


R markdown slides

To view the slide layouts available in your computer, use the R code below :


These layouts are illustrated below :

Note that, the selected layout determines the contents you can add into the slide (See the figure above).

  1. Example 1 - If you choose ‘Title and Content’ as a slide layout, you can add only :
    • a title
    • and one content which can be texts, plots, images, tables or R code
  2. Example 2 - If you choose ‘Two Content’ as a slide layout, you can add :
    • a title
    • and two contents : For example, you can add a table in the left panel and a paragraph of texts in the right panel.
  3. Example 3 - If you choose ‘Comparison’, you can add a title and four contents (plots, tables, paragraphs, images)

Whatever the slide layout chosen, you can use the functions addDate(), addFooter() and addPageNumber() to add date, footer and slide number, respectively.

Generate a simple PowerPoint document from R software

The R code below creates a PowerPoint document with a title slide, plots, tables, and an R script :

The PowerPoint document created by the R code above is available here : R software and ReporteRs package - Example of creating a PowerPoint document

Note that, you can use addPageNumber() function without specifying the value of the slide number, as follow :

In this case slide number is added using the default setting (e.g : 1 for slide1, 2 for slide 2).

If you want to customize the numbering, use the function as follow :


Format the text of a PowerPoint document

Text properties : font, color and size

As illustrated in the figure below, text properties include :

  • Font : family (e.g : “Arial”), size (e.g : 11), style (e.g : “italic”)
  • Underlined text
  • Color (e.g : “blue”)
  • Vertical align (superscript, subscript)

The default font size and font family of the PowerPoint can be modified as follow :

Change the appearance of a “Piece Of Text” (pot)


The function pot() [Pieces Of Text] is used to modify the appearance of a text. It can be used also to create a hyperlink. The format is :


  • value : the text to be formatted
  • format : the properties to use for formatting the text


The allowed values for the argument format are the following functions :

  • textProperties() : the text formatting properties
  • textBold(), textItalic(), textBoldItalic() and textNormal() which are shortcuts for bold, italic, bold-italic and normal text, respectively.

These functions can take the arguments below :


  • color : font color; e.g : color=“#000000” or color = “black”.
  • font.size : a integer indicating the font size.
  • font.weight : the font weight. Possible values are “normal” or “bold”.
  • font.style : the font style. Possible values are “normal” or “italic”.
  • underlined : a logical value specifying if the text should be underlined.
  • font.family : the font family; e.g : “Arial”.
  • vertical.align : a character indicating font vertical alignments. Expected values are “baseline”' or “subscript” or “superscript”. Default value is baseline.
  • shading.color : background color of the text (e.g “#000000” or “black”)


The R code below creates a PowerPoint document containing a formatted text and a hyperlink :

Add plots and images

The functions addPlot() and addImage() can be used for adding a plot or an external image to the document. addPlot() works with all R plots (base graphics, lattice, ggplot2 and grid).

These two functions can be used as follow :

The R code below creates a PowerPoint document containing a histogram and an image (downloaded from R website) :

The PowerPoint document created by the R code above is available here : R software and ReporteRs package - PowerPoint document containing plots and images

  1. Note that, addPlot() function can take other arguments such as pointsize to change the size of plotted texts (default value is 12; in pixels)

  2. For addImage() function, the allowed file formats are PNG, WMF, JPEG and GIF images.


Add a table

addFlexTable() function is used to format and add a table into the PowerPoint.

It can be used as follow :

  • STEP 1 : Create a table using FlexTable() or vanilla.table() function. These two functions generate a ‘flexible’ table which can be easily formatted before adding into the slide.
  • STEP 2 : Add the create table into the document using addFlexTable() function as follow :

setZebraStyle() function can be used to color odd and even rows differently; for example, odd rows in gray color and even rows in white color.

The example below creates a PowerPoint document with 3 slides containing a simple table (slide 1), vanilla table (slide 2) and a zebra striped table (slide 3) :

The PowerPoint document created by the R code above is available here : R software and ReporteRs package - PowerPoint document containing tables

Add ordered and unordered lists

Lists can be added using addParagraph() function as follow :


  • value : a set of items to be added as a list
  • par.properties : the paragraph formatting properties. It takes list.style and level as arguments :
    • list.style : possible values are ‘unordered’ and ‘ordered’
    • level : a numeric value indicating the level of the item to be added in the list


The example below generates a one-slide PowerPoint document containing an ordered and unordered lists :

This approach is useful in many situations :

  • If you work in a corporate environment and you want to generate a PowerPoint document based on a template with specific fonts, color, logos, etc.
  • If you want to modify and insert new contents into an existing PowerPoint document.
  • If you want to use text formatting styles and slide layouts from a given template file.

Note that, if you use a template file to create a PowerPoint document, slide layouts are those available in the template.

A template file can be specified to the pptx() function as follow :

In the next section We’ll :

  • download a PowerPoint template file from STHDA website
  • Check the available slide layouts in the template file
  • Create a PowerPoint document based on the template

Slide layouts available in the template file

You can use one of the layout below when adding a new slide into the PowerPoint :

Create a PowerPoint document from the template file

Note that, the template file contains already one empty slide which can be removed manually.

The PowerPoint document created by the R code above is available here : R software and ReporteRs package - PowerPoint document from template

This analysis has been performed using R (ver. 3.1.0).

You can read more about ReporteRs and download the source code at the following link :

GitHub (David Gohel): ReporteRs


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.
Show me some love with the like buttons below.. Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.
Montrez-moi un peu d'amour avec les like ci-dessous .. Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!



Recommended for You!


Rmarkdown Powerpoint Figure Size



More books on R and data science

Recommended for you

This section contains best data science and self-development resources to help you on your path.

Coursera - Online Courses and Specialization

Data science

  • Course: Machine Learning: Master the Fundamentals by Standford
  • Specialization: Data Science by Johns Hopkins University
  • Specialization: Python for Everybody by University of Michigan
  • Courses: Build Skills for a Top Job in any Industry by Coursera
  • Specialization: Master Machine Learning Fundamentals by University of Washington
  • Specialization: Statistics with R by Duke University
  • Specialization: Software Development in R by Johns Hopkins University
  • Specialization: Genomic Data Science by Johns Hopkins University

Popular Courses Launched in 2020

  • Google IT Automation with Python by Google
  • AI for Medicine by deeplearning.ai
  • Epidemiology in Public Health Practice by Johns Hopkins University
  • AWS Fundamentals by Amazon Web Services

Trending Courses

  • The Science of Well-Being by Yale University
  • Google IT Support Professional by Google
  • Python for Everybody by University of Michigan
  • IBM Data Science Professional Certificate by IBM
  • Business Foundations by University of Pennsylvania
  • Introduction to Psychology by Yale University
  • Excel Skills for Business by Macquarie University
  • Psychological First Aid by Johns Hopkins University
  • Graphic Design by Cal Arts

Books - Data Science

Our Books

  • Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
  • Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
  • Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
  • R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
  • GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
  • Network Analysis and Visualization in R by A. Kassambara (Datanovia)
  • Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
  • Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)

Others

  • R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
  • Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
  • Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
  • Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
  • An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
  • Deep Learning with R by François Chollet & J.J. Allaire
  • Deep Learning with Python by François Chollet


Want to Learn More on R Programming and Data Science?
Follow us by EmailOn Social Networks:

Get involved :
Click to follow us on Facebook and Google+ :
Comment this article by clicking on 'Discussion' button (top-right position of this page)

Description

Format for converting from R Markdown to an MS PowerPointdocument.

Rmarkdown Powerpoint Theme

The function will allow you to specify the destination of your chunks in the outputPowerPoint file. In this case, you must specify the layout and master for thelayout you want to use, as well as the ph argument, which will allow you to specifythe placeholder to be generated to place the result. Use the officer package to helpyou choose the identfiers to use.

This function also support Vector graphics output in an editable format (using packagervg). Wrap you R plot commands with function dml to use this graphic capability.

Usage

R Markdown Powerpoint

Arguments

base_format

a scalar character, format to be used as a base document forofficedown. default to powerpoint_presentation butcan also be powerpoint_presentation2 from bookdown

layout

default slide layout name to use

master

default master layout name where layout is located Pro logic 9 for mac os.

tcf

default conditional formatting settingsdefined by officer::table_conditional_formatting()

..

arguments used by powerpoint_presentation

Value

R Markdown output format to pass to render

Examples

Example output