Table layout

flextable layout can be managed with few functions.

library(flextable)
library(officer)
library(magrittr)

Cell merging

vertical merging

merge_v will merge adjacent duplicated cells for each column of the selection.

select_columns <- c("Species", "Petal.Length", "Petal.Width")
myft <- flextable(iris[46:55,], col_keys = select_columns) %>% 
  merge_v(~ Species + Petal.Width )

tabwid(myft) 

horizontal merging

merge_h will merge adjacent duplicated cells for each row of the selection.

select_columns <- c("Species", "Petal.Length", "Petal.Width")
myft <- flextable(head(mtcars, n = 10 ) ) %>% 
  merge_h( ) %>%
  border(border = fp_border(), part = "all") # and add borders

tabwid(myft)

general merging function

select_columns <- c("Species", "Petal.Length", "Petal.Width")
myft <- flextable(head(mtcars, n = 6 ) ) %>% 
  merge_at( i = 1:3, j = 1:3) %>%
  border(border = fp_border(), part = "all") # and add borders

tabwid(myft)

delete merging informations

If you want to get rid of all merging (i.e. for development purposes), use merge_none:

tabwid(myft %>% merge_none())

Manage headers

col_keys

Parameter col_keys define the variables to display and their order.

library(dplyr)
iris %>% 
  group_by(Species) %>% 
  do( head(., n = 3) ) %>% 
  flextable(col_keys = c("Species", "Sepal.Length", "Petal.Length") ) %>%
  theme_booktabs() %>% 
  tabwid()

If parameter col_keys has variables that are not existing in the dataset, they will be considered as blank columns and can be used as separators (in fact they can be use as you want, there is only no mapping of data associated).

iris %>% group_by(Species) %>% do( head(., n = 3) ) %>%
  flextable(col_keys = c("Species", "col_1", "Sepal.Length", "Petal.Length") ) %>%
  theme_vanilla() %>% autofit() %>% 
  border(j=2, border = fp_border(width=0), part = "all") %>% 
  tabwid()

col_keys default values are the names of the data.frame used to fill the flextable.

Change labels

Use set_header_labels to replace labels of the bottom row of header.

ft <- flextable( head( iris ) ) %>% 
  set_header_labels(Sepal.Length = "Sepal", 
    Sepal.Width = "Sepal", Petal.Length = "Petal",
    Petal.Width = "Petal", Species = "Species" )
  
ft %>% theme_vanilla() %>% autofit() %>% tabwid()

add a row of labels

Use add_header to add an header row.

ft <- ft %>% 
  add_header(Sepal.Length = "length",
    Sepal.Width = "width", Petal.Length = "length",
    Petal.Width = "width", Species = "Species", top = FALSE ) 
ft %>% theme_vanilla() %>% autofit() %>% tabwid()
ft <- ft %>% 
  add_header(Sepal.Length = "Inches",
    Sepal.Width = "Inches", Petal.Length = "Inches",
    Petal.Width = "Inches", Species = "Species", top = TRUE )

# merge identical cells
ft <- ft %>% merge_h(part = "header") %>% merge_v(part = "header")

ft %>% theme_vanilla() %>% autofit() %>% tabwid()

Define headers with a reference table

Use set_header_df with a data.frame as parameter. Columns of the dataset will be transposed and joined using a key column.

Input dataset

Variable col_keys define key values to match with flextable column keys (defined by argument col_keys of flextable function).

This key column will not be displayed. Other variables will added as rows. Note that variables names are not displayed.

typology <- data.frame(
  col_keys = c( "Sepal.Length", "Sepal.Width", "Petal.Length",
                "Petal.Width", "Species" ),
  type = c("double", "double", "double", "double", "factor"),
  what = c("Sepal", "Sepal", "Petal", "Petal", "Species"),
  measure = c("Length", "Width", "Length", "Width", "Species"),
  stringsAsFactors = FALSE )
typology %>% flextable() %>% theme_vanilla() %>% autofit() %>% tabwid()

Add the dataset as header rows

Then use set_header_df with parameter key that specifies name of the column used to permform the join. Order of columns matters, first column will be first row, second one will be the second row, etc.

flextable( head( iris ) ) %>% 
  set_header_df( mapping = typology, key = "col_keys" ) %>% 
  merge_h(part = "header") %>% merge_v(part = "header") %>% 
  theme_vanilla() %>% autofit() %>% tabwid()

Cell widths and heights

The default sizes of flextable columns and rows are calculated with a simple algorithm. This will drive to inadequate rows heights and columns widths in some cases (when data values are wider than headers). You can use function dim to get flextable dimensions.

ft_base <- flextable(head(iris)) %>% 
  theme_tron_legacy()
tabwid(ft_base)
dim(ft_base)
#> $widths
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#>         0.75         0.75         0.75         0.75         0.75 
#> 
#> $heights
#> [1] 0.25 0.25 0.25 0.25 0.25 0.25 0.25

Pretty dimensions

Function dim_pretty is computing optimized widths and heights.

dim_pretty(ft_base)
#> $widths
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#>    0.9460630    0.8688196    0.9074752    0.8302318    0.5991121 
#> 
#> $heights
#> [1] 0.2044994 0.1756366 0.1756366 0.1756366 0.1756366 0.1756366 0.1756366

This function is time consuming and should be used sparingly.

Adjusts automatically cell widths and heights

Function autofit optimises widths and heights of the flextable. This function is almost always to be called once when using flextable, it makes compact tables.

ft <- ft_base %>% 
  autofit(add_w = 0, add_h = 0)

dim(ft)
#> $widths
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#>    0.9460630    0.8688196    0.9074752    0.8302318    0.5991121 
#> 
#> $heights
#> [1] 0.2044994 0.1756366 0.1756366 0.1756366 0.1756366 0.1756366 0.1756366
tabwid(ft)

Adjusts manually cell widths and heights

Function width and height let you control dimensions of a flextable.

ft <- ft_base %>% autofit() %>% 
  width(j = ~ Species, width = 2) %>% 
  height(i = 4:5, height = .5)
tabwid(ft)