flextable examples

Complex header

The following dataset is typology, a dataset containing data for table headers.

#>           col_keys         colC          colB        colA
#> 1            sep_1                                       
#> 2            sep_2                                       
#> 3             year                       Year        Year
#> 4          premium                    Premium     Premium
#> 5      latest_eval                Latest Eval Latest Eval
#> 6     cape_cod_u_l     Cape Cod Ultimate Loss       (000)
#> 7      cape_cod_lr     Cape Cod   Ultimate LR        ( %)
#> 8 chain_ladder_u_l Chain Ladder Ultimate Loss            
#> 9  chain_ladder_lr Chain Ladder   Ultimate LR       (%\n)

The following dataset is x, it will be displayed in the table body.

#>   year   premium latest_eval cape_cod_u_l cape_cod_lr chain_ladder_u_l
#> 1 2001  8.920428    4.492365         6998          60         4.970296
#> 2 2002 12.660827    5.165556         7058          69         5.980417
#> 3 2003  8.757757    6.221537         6923          69         6.392572
#> 4 2004  9.852580    5.334078         6916          83         4.400530
#>   chain_ladder_lr
#> 1        69.33936
#> 2        69.06072
#> 3        71.40414
#> 4        70.23848
double_format <- function(x){
  sprintf("%.3f", x)
}
percent_format <- function(x){
  sprintf("%.2f %%", x)
}
ft <- regulartable(
  x, col_keys = c("year", "premium", "latest_eval",
                  "sep_1", "cape_cod_u_l", "cape_cod_lr",
                  "sep_2", "chain_ladder_u_l", "chain_ladder_lr") )
ft <- set_formatter(ft, premium = double_format, latest_eval = double_format,
                    chain_ladder_lr = percent_format )
ft <- set_header_df(ft, mapping = typology, key = "col_keys" )
tabwid(ft)

ft <- merge_h(ft, part = "header")
ft <- merge_v(ft, part = "header", j = 1:3)
ft <- theme_zebra(ft, odd_header = "transparent", even_header = "transparent")
tabwid(ft)

ft <- fontsize(ft, size = 11, part = "all")
ft <- fontsize(ft, i = 1:2, size = 12, part = "header")
ft <- color(ft, i = 1:2, color = "#007FA6", part = "header")
ft <- fontsize(ft, i = 3, size = 9, part = "header")
ft <- color(ft, i = 3, color = "gray", part = "header")
tabwid(ft)

ft <- border(ft, border.bottom = fp_border(width = .75, color = "#007FA6"), part = "body" )

# color last border bottom of header and first border top of body
ft <- border(ft, border.bottom = fp_border(width = 2, color = "#007FA6"), part = "header" )
ft <- border(ft, i = 1, border.top = fp_border(width = 2, color = "#007FA6"), part = "body" )
tabwid(ft)

ft <- empty_blanks(ft)
ft <- autofit(ft)

tabwid(ft)