flextable overview


flextable can easily create reporting table from data.frame. You can merge cells, add header 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 flextable.

The input dataset and the metadata for columns labels are printed below:

#> Source: local data frame [9 x 5]
#> Groups: Species [3]
#> # A tibble: 9 x 5
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#>          <dbl>       <dbl>        <dbl>       <dbl>     <fctr>
#> 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          7.0         3.2          4.7         1.4 versicolor
#> 5          6.4         3.2          4.5         1.5 versicolor
#> 6          6.9         3.1          4.9         1.5 versicolor
#> 7          6.3         3.3          6.0         2.5  virginica
#> 8          5.8         2.7          5.1         1.9  virginica
#> 9          7.1         3.0          5.9         2.1  virginica
#>       col_keys    what measure
#> 1 Sepal.Length   Sepal  Length
#> 2  Sepal.Width   Sepal   Width
#> 3 Petal.Length   Petal  Length
#> 4  Petal.Width   Petal   Width
#> 5      Species Species Species

Getting started

Let’s have a step by step demo. First create a flextable and change header font in bold. Function tabwid will wrap it in an htmlwidget.


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

flextable function: flextable create a flextable object based on input data. flextable does not format automatically the table but it is preparing the object to be formatted. Optional argument col_keys is used to only display a subset of columns.

tabwid function: tabwid is the function that transform that flextable object into an html widget object (in r markdown documents or shiny applications). We need it here as we are working in an r markdown document.

Let’s keep it simple and apply a theme to format the whole table. Functions 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.

myft <- myft %>% theme_vanilla()


Table layout can be modified. Man can add or change header rows, change cells height and width and merge cells. Also, there is an important function named autofit.

We will use merge_v to merge identical consecutive cells of columns “carb” and “am”.

myft <- myft %>%
  merge_v(j = c("am", "carb") )

Cells can be merged with functions merge_none, merge_v and merge_h.

set_header_labels set labels:

myft <- myft %>%
  set_header_labels( carb = "# carb." ) %>% 
  width(width = .75) # set width of all columns to .75 in

Headers can be modified with functions set_header_df, set_header_labels and add_header.


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

myft <- myft %>% italic(j = 1) %>% 
  bg(bg = "#C90000", part = "header") %>% 
  color(color = "white", part = "header") %>% 
  border(border = fp_border(color = "orange"), part = "all")

Conditional formatting can be made by using the selector arguments. All formatting functions are accepting selector arguments.

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


autofit adjust widths and heights of cells. This is 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.

myft <- myft %>% autofit()



Flextables can be inserted in r markdown documents and in shiny applications. Use tabwid in these cases, when working in RStudio, flextable will be printed in the rstudio viewer pane (call to tabwid is not necessary in this case). Note that flextables are not designed to work with flexdashboard documents.

To add these objects in PowerPoint or Word documents, use functions: - ph_with_flextable (PowerPoint) - body_add_flextable (Word)

officer package is required to create a PowerPoint or Word document.

ft <- flextable(head(mtcars)) %>% 
  theme_zebra() %>% 

ppt <- read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_flextable(value = ft, type = "body") 
if( interactive() ) print(ppt, target = "test.pptx")

doc <- read_docx() %>% 
  body_add_flextable(value = ft)
if( interactive() ) print(doc, target = "test.docx")