flextable overview

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. flextable objects can be rendered in HTML format but also in Microsoft Word and PowerPoint documents.

The following table is made with function flextable and dataset data:

data <- iris[c(1:3, 51:53, 101:104),]

Species

Sepal

Petal

Length

Width

Length

Width

setosa

5.100

3.500

1.400

0.200

4.900

3.000

1.400

0.200

4.700

3.200

1.300

0.200

versicolor

7.000

3.200

4.700

1.400

6.400

3.200

4.500

1.500

6.900

3.100

4.900

1.500

virginica

6.300

3.300

6.000

2.500

5.800

2.700

5.100

1.900

7.100

3.000

5.900

2.100

6.300

2.900

5.600

1.800

Supported output formats

When working in RStudio, flextable will be printed in the rstudio viewer pane.

R Markdown documents

One great feature of flextables is that it can be used in R Markdown documents; HTML, Word and PowerPoint outputs are managed with method knitr::knit_print.

Print the 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
```

pandoc versions

Supported formats require some minimum pandoc versions:

R Markdown output pandoc version comment
HTML >= 1.12
Microsoft Word >= 2.0 require a recent RStudio (> june 2018)
Microsoft PowerPoint >= 2.4 require pandoc installation

PowerPoint or Word documents with package officer

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:

Download file example.pptx - view with office web viewer

The following is producing a Word document:

Download file example.docx - view with office web viewer


Getting started

The main function is flextable.

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:

flextable keypoints

header

row 1 - cell from 1 to 5

row 2 - cell from 1 to 2

row 2 cell from 3 to 5

row 3 cell 1

row 3 cell 2

row 3 cell 3

row 3 cell 4

row 3 cell 5

body

data[1,1]

data[1,2]

data[1,3]

data[1,4]

data[1,5]

data[2,1]

data[2,2]

data[2,3]

data[2,4]

data[2,5]

...

...

...

...

...

data[n,1]

data[n,2]

data[n,3]

data[n,4]

data[n,5]

footer

Grouped footer 1.1

Grouped footer 1.2

Grouped footer 2.1

Let’s have a step by step demo. First create a flextable and change header font in bold.

am

carb

gear

mpg

drat

1.000

4.000

4.000

21.000

3.900

1.000

4.000

4.000

21.000

3.900

1.000

1.000

4.000

22.800

3.850

0.000

1.000

3.000

21.400

3.080

0.000

2.000

3.000

18.700

3.150

0.000

1.000

3.000

18.100

2.760

flextable: it is creating a flextable object based on input data. Optional argument col_keys is used to only display a subset of columns.

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.

am

carb

gear

mpg

drat

1.000

4.000

4.000

21.000

3.900

1.000

4.000

4.000

21.000

3.900

1.000

1.000

4.000

22.800

3.850

0.000

1.000

3.000

21.400

3.080

0.000

2.000

3.000

18.700

3.150

0.000

1.000

3.000

18.100

2.760

Layout

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).

am

# carb.

gear

mpg

drat

1.000

4.000

4.000

21.000

3.900

4.000

21.000

3.900

1.000

4.000

22.800

3.850

0.000

3.000

21.400

3.080

2.000

3.000

18.700

3.150

1.000

3.000

18.100

2.760

To learn more, see article about layouts.

Formatting

To learn more, see article about format.

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

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

am

# carb.

gear

mpg

drat

1.000

4.000

4.000

21.000

3.900

4.000

21.000

3.900

1.000

4.000

22.800

3.850

0.000

3.000

21.400

3.080

2.000

3.000

18.700

3.150

1.000

3.000

18.100

2.760

Selectors

Selectors can be used to specify the rows and columns where the operation should happened.

Many flextable functions have selectors i and j: bg, bold, border, color, padding, fontsize, italic, align, …

i for rows selection and j for columns selection can be expressed in different ways:

ft <- flextable(head(iris, n = 10))
ft <- color(ft, i = ~ Sepal.Length < 5, j = ~ Sepal.Length + Sepal.Width, color = "orange")
ft

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

ft <- flextable(head(iris, n = 10))
ft <- color(ft, i = 1:3, j = 1:3, color = "orange")
ft

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

dat <- head(iris, n = 10)
ft <- flextable(dat)

ft <- color(ft, i = dat$Sepal.Length < 5, color = "orange")
ft

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

dat <- head(iris, n = 10)
ft <- flextable(dat)
ft <- color(ft, j = "Sepal.Length", color = "orange", part = "all")
ft

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa