Format flextable objects

A flextable is made of two parts, an header and a body. To specify which part formatting instructions should affect, use argument part. Possible values are:

Shortcuts functions

There are simple functions to modify formatting properties of flextable objects: bg, bold, border, color, padding, fontsize, italic, align.

myft <- flextable(head(iris))
tabwid(myft)

bold

myft <- flextable(head(iris)) %>% 
  # bold header
  bold(part = "header") 
tabwid(myft)

Font size

myft <- flextable(head(iris)) %>% 
  # change font size header
  fontsize(part = "header", size = 12) 
tabwid(myft)

change font color

myft <- myft %>% color(color = "#E4C994")
tabwid(myft)

Italic

myft <- myft %>% 
  italic(i = ~ Sepal.Length > 5, 
         j = ~ Sepal.Length + Sepal.Width, italic = TRUE)
tabwid(myft)

change background color

myft <- myft %>% 
  # light gray as background color for header
  bg(bg = "#E4C994", part = "header") %>% 
  # dark gray as background color for body
  bg(bg = "#333333", part = "body")

tabwid(myft)

Text alignment

myft <- myft %>% align( align = "center", part = "all" )
tabwid(myft)

add padding

myft <- myft %>% padding( padding = 3, part = "all" )
tabwid(myft)

change border

myft <- myft %>% 
  border( border = fp_border(color="white"), part = "all" )
  
tabwid(myft)

Conditional formatting

Conditional formatting can be made by using the selector arguments.

myft <- myft %>% 
  color(i = ~ Sepal.Length < 5 & Petal.Length > 1.3, 
        j = ~ Petal.Width + Species, 
        color="red") 
tabwid(myft)

i and j arguments can be also standard R vectors:

row_id <- with(head(iris), Sepal.Length < 5 & Petal.Length > 1.3 )
col_id <- c("Petal.Width", "Species")

myft <- color(myft, i = row_id, j = col_id, color="red") 

tabwid(myft)

Function style

The style function lets you style a selection of the flextable with several formatting properties.

It main advantage is to let specify a set of formatting properties for a selection.

Package officer needs to be loaded, it provides the following formatting properties:

library(officer)
def_cell <- fp_cell(border = fp_border(color="#00C9C9"))
def_par <- fp_par(text.align = "center")
def_text <- fp_text(color="#999999", italic = TRUE)
def_text_header <- update(color="black", def_text, bold = TRUE)

ft <- flextable(head(mtcars, n = 10 )) %>% 
  style( pr_c = def_cell, pr_p = def_par, pr_t = def_text, part = "body")  
tabwid(ft)

ft <- ft %>% 
  style( pr_c = def_cell, pr_p = def_par, pr_t = def_text_header, part = "header")  
tabwid(ft)

Text rotation

Text rotation is possible in flextable objects. This is achieved by using function style and its argument pr_c. This need to be a fp_cell object with text.direction='btlr' or text.direction='tbrl'.

ft <- flextable(head(iris)) %>% 
  style(pr_c = fp_cell(text.direction = "btlr"), 
        part = "header") %>% 
  theme_vanilla() %>% 
  autofit() %>% 
  height(height = 1, part = "header")

tabwid(ft)

Note for HTML output that it only works as expected when fp_cell is set with vertical.align = "middle" which is the default value of fp_cell.

display function

Below the starting point of next illustrations:

myft <- flextable(head(mtcars), 
                  col_keys = c("am", "separator", "gear", "mpg", "drat" )) %>% 
  bold(part = "header") %>% 
  border(border = fp_border( width = 0), 
         border.top = fp_border(), 
         border.bottom = fp_border(), part = "all") %>% 
  align(align = "right", part = "all" ) %>%
  border(j = ~ separator, border = fp_border(width=0), part = "all") %>% 
  width(j = ~ separator, width = .1)

tabwid(myft)

Flextable content is defined with display function.

It always requires a call to fpar which is a simple wrapper whose role is to concatenate formatted text. The following call to fpar only let you modify the display content of a cell (because fpar argument is only a single string).

myft <- myft %>%
  display( 
    mpg = fpar(formatC(mpg, format = "f", digits = 3 ) ) 
  )

tabwid(myft)

If more control is needed, i.e. for conditional formatting, one can use function ftext to associate a text with formatting properties.

myft <- myft %>%
  display(i = ~ drat > 3.6, 
    gear = fpar( ftext( gear, prop = fp_text(bold = TRUE, color="red") ) )
  )

tabwid(myft)

fpar can have multiple arguments that will be then concatenated.

myft <- myft %>%
  display(
    mpg = fpar(mpg, " and ", ftext( carb, prop = fp_text(bold = TRUE)), " carb(s)." ) 
  ) %>% autofit()

tabwid(myft)

Add images into cells

fpar handles call to minibar. This will generate a simple bar to be inserted as content.

myft <- myft %>%
  display( i = ~ gear < 4,
    drat = fpar(minibar( value = drat, max = max(.$drat), 
                         barcol = "#C90000", width = 1, height = .15) )
  ) %>% autofit()

tabwid(myft)

Note usage of ., this object represents the input dataset of flextable call. The expression is not vectorized.

External images can be embed in tables. Below an illustration. Package ionicons is used to get 2 icons (sad and happy icon). The fpar call is containing a call to external_img which will use the path to icons png file.

myft <- flextable(head(iris)) %>% 
    theme_vanilla()

if( require(ionicons) ){
  happy = as_png(name = "happy", fill = "green")
  sad = as_png(name = "sad", fill = "orange")
  happy
  sad
  myft <- myft  %>% 
    display(i = ~ Sepal.Length < 5, 
            Sepal.Length = fpar(external_img(sad, width = .2, height = .2)) ) %>% 
    display(i = ~ Sepal.Length >= 5, 
            Sepal.Length = fpar(external_img(happy, width = .2, height = .2)) )
  tabwid(myft)
}