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.
Two main functions are provided:
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.). Function
display lets customise text of cells (See display function).
regulartable function has been written because the first one is ressource consumming. The main difference is that it is producing tables where cells can contain only one chunk of text with its own set of formatting properties. Note that
flextable functions are both returning an object of class
Note also that
regulartable supports strange variable names (with percent symbols, with spaces, etc.) and
flextable does not support them.*
The following table is made with
regulartableand dataset data:
A flextable is an object that will produce a reporting table from a
data.frame object. Any formatting property related to text, paragraphs, cells in the table can be modified. Content can also be customized.
Columns to display 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.
A flextable is containing three parts, one for header rows,
one for body rows and one for footer rows. 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 never implemented when creating the
A cell is made of one single paragraph of text. When the flextable object is created by function
flextable(), paragraphs can contain several chunks of text with different formatting. When the flextable object is created by function
regulartable(), paragraphs can contain only one chunk of text with one single set of formatting properties.
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
Let’s have a step by step demo. First create a regulartable and change header font in bold.
flextable or regulartable function:
flextable create 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.
To learn more, see article about layouts.
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).
We will use
merge_v() to merge identical consecutive cells of columns “carb” and “am”.
set_header_labels() set labels:
autofit() adjust widths and heights of cells. This should be the last operation as some operations make columns wider, e.g. changing font size, changing font weight.
autofit makes sure that any content is displayed as a single line of text.
To learn more, see article about format.
Conditional formatting can be made by using the selector arguments. All formatting functions are accepting selector arguments.
When working in RStudio, flextable will be printed in the rstudio viewer pane.
flextables can be inserted in R Markdown documents; HTML and Word output are managed with method
flextable object in the rmarkdown’s chunk as demonstrated in this document (…generated from an R Markdown document).
Note that pandoc 2.0 required for flextable rendering in docx with
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:
xtable objects can be transformed as flextable objects with function