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.
# 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
library(ReporteRs) doc
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 :
You should know three functions to change the appearance of table rows and columns :
These functions can be used as follow :
library(ReporteRs) doc
Note that, i and j are, respectively, the index of rows and column to change
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
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)
library(ReporteRs) doc
library(ReporteRs) doc
A pptx object works the same but does not require any parProperties
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!