flextable can easily create reporting table from
data.frame. You can merge cells, add header rows, add footer rows, change any format and specify how data should be displayed in cells.
flextable objects can be rendered in HTML format but also in Microsoft Word and PowerPoint documents.
The following table is made with function
flextableand dataset data:
When working in RStudio, flextable will be printed in the rstudio viewer pane.
One great feature of
flextables is that it can be used in R Markdown documents; HTML, Word and PowerPoint outputs are managed with method
flextable object in the rmarkdown’s chunk as demonstrated in this document (…generated from an R Markdown document).
> this is how to print a flextable in a R Markdown document ```r library(magrittr) mytable <- cars %>% head() %>% flextable() %>% autofit() mytable ```
Supported formats require some minimum pandoc versions:
|R Markdown output||pandoc version||comment|
|Microsoft Word||>= 2.0||require a recent RStudio (> june 2018)|
|Microsoft PowerPoint||>= 2.4||require pandoc installation|
To add these objects in PowerPoint or Word documents, use functions:
officer package is required to create a PowerPoint or Word document.
The following is producing a PowerPoint document:
The following is producing a Word document:
The main function is
flextable function is producing flexible tables where each cell can contain several chunks of text with their own set of formatting properties (bold, font color, etc.).
The function is using default values to format the table. It is also using a simple formatting function to create the paragraphs of text that will be rendered in each cell. All these default values can be modified after the flextable creation.
The following illustration is presenting most of the functions available to format a flextable:
A flextable is an object that will produce a reporting table from a
A flextable is containing three parts, header, body and footer. By default, there is only one header row containing the names of the data.frame. The body part is containing data from the data.frame. The footer part is not implemented by default.
Any formatting property related to text, paragraphs, cells in the table can be modified.
A cell is made of one single paragraph of text. Paragraphs can contain several chunks of text with different formatting but also images.
The dimensions of the flextable elements, widths of columns and heights of rows, are always defined. Beeing able to define the exact size necessary to display data on a single line is a key concept. See functions
Columns to display (
col_keys) are by default all columns of the data.frame but can be choosen from a subset of existing columns and also unexisting columns. Unexisting columns are containing blanks by default but this content can be customized.
Let’s have a step by step demo. First create a flextable and change header font in bold.
flextable: it is creating a flextable object based on input data. Optional argument
col_keys is used to only display a subset of columns.
theme_ are sugar functions whose role is to apply a set of formatting instructions to a flextable. For example,
theme_vanilla set specific borders, right align paragraphs and make headers bold.
Table layout can be modified. One can add or change header/footer rows, change cells height and width and merge cells.
Also, there is an important function named
autofit (it adjusts widths and heights regarding to text widths and heights).
To learn more, see article about layouts.
To learn more, see article about format.
Conditional formatting can be made by using the selector arguments. All formatting functions are accepting selector arguments.
Selectors can be used to specify the rows and columns where the operation should happened.
flextable functions have selectors
i for rows selection and
j for columns selection can be expressed in different ways:
i = ~ Species %in% "versicolor", j = ~ Species + Sepal.Length
j, a p length if p columns; for
i, a n length if n rows