Chapter 1 Overview

The flextable package provides a framework for easily create tables for reporting and publications. Functions are provided to let users create tables, modify and format their content and define their content.

Tables can be embedded within:

  • R Markdown documents with support for HTML, Word, PDF and PowerPoint documents.
  • Microsoft Word or PowerPoint documents with package officer.

Tables can also be rendered as R plots or graphic files (png, pdf and jpeg).

1.1 Introduction

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. Tables content can also contain mixed type of content, text and images.

The following table is an example of a flextable rendered in HTML.

ft <- flextable(airquality[,])
ft <- add_header_row(ft,
  colwidths = c(4, 2),
  values = c("Air quality", "Time")
ft <- theme_vanilla(ft)
ft <- add_footer_lines(ft, "Daily air quality measurements in New York, May to September 1973.")
ft <- color(ft, part = "footer", color = "#666666")
ft <- set_caption(ft, caption = "New York Air Quality Measurements")

The main function is flextable. Call the function with a data.frame as argument. If you are using RStudio or another R GUI, the table will be displayed in the Viewer panel or in your default browser.


Why the table we have produced use font ‘Helvetica’ with color black and size 11 point? The formatting properties that were used are the default properties.

It is also using a simple formatting function to create the paragraphs of text that will be rendered in each cell.

If you want to change the default appearance of the tables, you can use the set_flextable_defaults function.

  font.size = 10, theme_fun = theme_vanilla,
  padding = 6,
  background.color = "#EFEFEF")

The package also provides a set of functions to easily create some tables:

  • as_flextable() allows to transform R models into a table ready for export
  • proc_freq() allows to create a contingency table like the PROC FREQ of SAS.
  • tabulator() creates a table whose layout is showing dimensions of
    aggregations across rows and columns.
  • summarizor() provides statistical summaries as pretty tables.

Finally, it allows to automate the production of simple tables in ‘R Markdown’ for the display of ‘data.frame’ and models (lm, glm, gam, lme, lmer, kmeans and few other one) with functions use_df_printer() and use_model_printer().

1.2 Other ressources

1.2.1 Function reference

Function reference (manuals) is available here:

These manuals are also available when using R as usual help pages.

1.2.3 Cheat sheet

A cheat sheet has been written by Clémentine Jager and available here: Flextable cheat sheet

1.3 Walkthrough: simple example

flextable has been designed to allow the construction of simple and complex tables. A set of functions will allow you to change colors, fonts, add headers and many other things to give you the possibility to build the tables that fit your needs.

Let’s have a step by step simple demo.

First, the flextable has to be created.

myft <- flextable(head(mtcars), 
  col_keys = c("am", "carb", "gear", "mpg", "drat" ))

Optional argument col_keys is used to only display a subset of columns.

Many sugar functions can be used to format flextables: bg(), fontsize(), italic(), bold(), color(), padding()

flextable is separating the data and the display of the data. It makes possible to do conditional formatting. Conditional formatting can be made by using the selector arguments. All formatting functions are accepting selector arguments.

myft <- italic(myft, j = 3)
myft <- color(myft, ~ drat > 3.5, ~ drat, color = "red")
myft <- bold(myft, ~ drat > 3.5, ~ drat, bold = TRUE)

Table layout can be modified. One can add or change header/footer rows, change cells height and width and merge cells.

myft <- add_header_row(
  x = myft, values = c("some measures", "other measures"),
  colwidths = c(3, 2))
myft <- align(myft, i = 1, part = "header", align = "center")

Functions theme_ are sugar functions whose role is to apply a set of formatting instructions to a flextable.

myft <- theme_vanilla(myft)