The latest version of ‘flextable’ is on CRAN since few days.

This version comes with some major improvements:

It also contains some new features that we are happy to introduce in this post.


This new function prepares aggregations in a layout that can be turned into a nice flextable. The objective is to provide a tool to allow the creation of reporting tables where dimensions are arranged in rows and columns and where one or more statistics are displayed in the cell contents. ‘flextable’ grammar can be used to customize the table generated with comamnd flextable::as_flextable().

The function will create this table from already calculated aggregations.

In a Word document, the result would be the following. Note the page number is indicated in the top row of the table. The code for this example is available in the flextable gallery here.

‘tabulator’ benefits for all flextable grammar and can build complex tables made with graphics and text:

Summarizor for table summary

Function summarizor() is an utility function that prepare a numerical summary for each variables of a dataset, eventually by groups. It is supposed to be used as argument of tabulator().

The code for this example is available in the flextable gallery here.

Automating flextable printing

flextable provides two new functions dedicated to ‘R Markdown’ that turn automatically data.frame and models to flextable.

data.frames as flextables

We brought a code that we were using for our trainings that allows to display data.frame as flextables inside R Markdown documents. We tried to mimic the output of ‘data.table’ and ‘tibble’ by displaying just the first few rows and the type of the columns. To take advantage of this feature, we need to call the use_df_printer() function and voila!


Models as flextables

We have implemented the reporting of models into flextable, the function as_flextable() is doing the transformation. For now, the following models are supported: gam, lm, glm, glmmTMB, glmmadmb, htest, kmeans, pam, lme, gls, merMod, nlme and brmsfit.

This is an example made with a lme model:

m1 <- lme(distance ~ age, data = Orthodont)
ft <- as_flextable(m1)

In a R Markdown document, it is possible to automate the printing of models as flextable by using the function use_model_printer(). This is an example made with a kmean clustering:

kmeans(scale(mtcars[1:7]), 5)

Separate column names into multiple rows

You can now separate collapsed column names into multiple rows with function separate_header(). This makes reporting aggregations calculated with ‘dplyr’ or ‘data.table’ easy.

In the following example we aggregate the famous ‘penguins’ table.

Without any intermediate manipulation, we create the ‘flextable’ and restructure header labels by using separate_header().

ft <- dat |> 
  flextable() |> 
  separate_header() |> 

It’s then quite easy to customize it and produce a flextable as below:

The code for this example is available in the flextable gallery here.

GitHub Discussions

Let us know about your issues, questions and feedbacks in ‘GitHub Discussions’ now available in the Github page of ‘flextable’:

Follow us:  -  Recommanded sites: R-bloggers R weekly Twitter #rstats Jobs for R-users