Add a table into a Word document using R software and ReporteRs package

The ReporteRs package is used to create a Word document from R software. The function addFlexTable() can be used to add a simple or customized table into the document.

  1. The first step is to create a table using one of the functions below :
  1. The second step is to add the created table into the Word document using addFlexTable() function as follow :
# doc : docx object # flextable : FlexTable object addFlexTable(doc, flextable)

The aim of this R tutorial is to show you step by step, how to add simple and formatted table into a Word document.

In the following examples, we’ll add the first 5 rows of iris data sets into the Word document.

data<-iris[1:5, ] data
 Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa

Add a simple table

library(ReporteRs) doc 

R software and Reporters package, add table to a Word document

An optional argument of addFlextable() function is par.properties which value can be parRight(), parLeft(), parJustify() for the table alignments. It can be used as follow :

Note also that, row names are not shown by default when FlexTable() function is used to create a table. To make row names visible use the function as follow :

Add a formatted table

Change the background colors of rows and columns

You should know three functions to change the appearance of table rows and columns :

These functions can be used as follow :

library(ReporteRs) doc 

R software and Reporters package, add table to a Word document

Note that, i and j are, respectively, the index of rows and column to change

Change cell background and text colors

We can change the background colors of some cells according to their values using the function setFlexTableBackgroundColors().

As an example, We’ll set up the background color of column 2 according to the value of the Sepal.Width variable (iris data sets) :

The text values of the table cells can be also customized as demonstrated in the example below :

library(ReporteRs) doc 

R software and Reporters package, add table to a Word document

Insert content into a table : header and footer rows

Additional rows, such as header and footer, can be easily added into a table as illustrated in the example hereafter. Contents can be also added in a particular cell.

The functions addHeaderRow() and addFooterRow() are used to add a header and a footer.

A simplified format of these functions are :

addHeaderRow(x, value, colspan, text.properties) addFooterRow(x, value, colspan, text.properties)
  • x : a FlexTable object
  • value : character vector to add as a header or a footer
  • colspan : a numeric vector (optional) specifying the number of columns to span for each corresponding value
  • text.properties : the text properties to apply to each cell (optional).
library(ReporteRs) doc 

R software and Reporters package, add table to a Word document

Analyze, format and export a correlation matrix into a Word document

library(ReporteRs) doc 

R software and Reporters package, add table to a Word document

Powerpoint

A pptx object works the same but does not require any parProperties

Infos

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

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!