Chapter 2 Make homogeneous tables

When a flextable is created, some default properties are used to define the formatting - font family, font size, padding, text alignment, etc. These default properties are use each time a ‘flextable’ is created, and when relevant, by the theme functions.

Default properties can be shown with function get_flextable_defaults() and updated with function set_flextable_defaults().

set_flextable_defaults(
  font.color = "red",
  border.color = "red",
  theme_fun = "theme_box")

dat <- data.frame(
  wool = c("A", "B"),
  L = c(44.56, 28.22), 
  M = c(24, 28.77), 
  H = c(24.56, 18.78)
)
flextable(dat)

wool

L

M

H

A

44.56

24.00

24.56

B

28.22

28.77

18.78

It is recommended to specify these properties only once at the beginning of the R script or first code chunk of an ‘R markdown’ document in order for all tables to have consistent formatting.

To make nice tables there are some simple rules that are easily implemented with the set_flextable_defaults() function:

  • use only one font and one font size,
  • use the same padding to facilitate the reading of the contents by adding space between themu,
  • use one border color
set_flextable_defaults(
  font.size = 12, font.family = "Open Sans",
  font.color = "#333333",
  table.layout = "fixed",
  border.color = "gray",
  padding.top = 3, padding.bottom = 3,
  padding.left = 4, padding.right = 4)

flextable(dat)

wool

L

M

H

A

44.56

24.00

24.56

B

28.22

28.77

18.78

Note that it is also possible to set the default theme that will be the last instruction called when creating the flextable. If you don’t want to apply a theme, fill in the default theme with this function: theme_fun = function(x) x.

Properties can be reset to package defaults with function init_flextable_defaults().

set_flextable_defaults(font.color = "red")
flextable(dat)

wool

L

M

H

A

44.56

24.00

24.56

B

28.22

28.77

18.78

wool

L

M

H

A

44.56

24.00

24.56

B

28.22

28.77

18.78

2.1 Important default values

It is recommended used only one font throughout the whole table. To do so, you can call font() function for one-off applications:

flextable(dat) |> font(part = "all", fontname = "Inconsolata")

wool

L

M

H

A

44.56

24.00

24.56

B

28.22

28.77

18.78

Otherwise, to set the default font properties, it is recommended to use font.family parameter of the set_flextable_defaults() function.

set_flextable_defaults(font.family = "Inconsolata")
flextable(dat)

wool

L

M

H

A

44.56

24.00

24.56

B

28.22

28.77

18.78

The following list details the most important parameters of the set_flextable_defaults() function that generally remain the constant throughout a single table, and often for all tables in a script or document.

  • font.family takes a string value representing the desired font. Recommended fonts to use are ‘sans serif’, ‘Helvetica’ and ‘Verdana’ because they are easy to read, quite popular and easy to install on Windows, Linux and MacOS.
  • font.size takes a numeric value, the default value is 11.
  • padding parameters take numeric values, these values should be adjusted to the font size. For example, a size 12 font should use a padding of 3 points; a size 10 font should use padding of 2 points; a size 20 font should use a padding of 6 points.
  • border.color takes a string value, set to ‘black’ by default. Other examples include; ‘gray’, ‘red’, or hexcodes (seen below).
  • line_spacing takes a numeric value. Recommended to set this value between 1.2 and 1.5.
set_flextable_defaults(
  font.family = "Inconsolata",
  font.size = 11,
  padding = 2,
  border.color = "#CCCCCC",
  line_spacing = 1.3
)
flextable(dat)

wool

L

M

H

A

44.56

24.00

24.56

B

28.22

28.77

18.78

2.1.1 Word multi-language fonts

MS Office products offer a feature of setting multi-language fonts for East-Asian users. Users can set both CJK fonts and Western language fonts in the setting pane. In flextable, it is possible to set :

  • font.family, a single character value. When format is Word, it specifies the font to be used to format characters in the Unicode range (U+0000-U+007F).
  • cs.family, optional and only for Word. Font to be used to format characters in a complex script Unicode range. For example, Arabic text might be displayed using the “Arial Unicode MS” font.
  • eastasia.family, optional and only for Word. Font to be used to format characters in an East Asian Unicode range. For example, Japanese text might be displayed using the “MS Mincho” font.
  • hansi.family, optional and only for Word. Font to be used to format characters in a Unicode range which does not fall into one of the other categories.
set_flextable_defaults(
  font.family="Inconsolata", 
  cs.family = "Symbol", 
  eastasia.family = "仿宋")

df = data.frame(
  `标题Title`='微软MS',
  `内容Content`='无题lorem',
  stringsAsFactors = FALSE)

ft = df %>% 
  flextable %>% 
  autofit %>% 
  width(width=1) %>% 
  theme_box()

save_as_docx(ft, path = "reports/df_eastasia.docx")

2.2 Default values for numbers

There are properties dedicated to numbers display, the parameters for function set_flextable_defaults includes:

  • digits
  • decimal.mark sets the decimal seperator, usually ‘,’ or ‘.’
  • big.mark sets the thousands separator, usually ’ ‘,’,’ or ‘.’

digits is not applied when creating a flextable because the default formatter for numerics is function colformat_num() which formats similarly to R console (but scientific mode is disabled and NA are replaced).

set_flextable_defaults(
  digits = 2,
  decimal.mark = ",",
  big.mark = " ",
  na_str = "<na>"
)
set.seed(2)
head(airquality) |> 
  mutate(rnum = runif(6, min = 100000, max = 10000000)) |> 
  qflextable()

Ozone

Solar.R

Wind

Temp

Month

Day

rnum

41

190

7,4

67

5

1

1 930 334

36

118

8,0

72

5

2

7 053 503

12

149

12,6

74

5

3

5 775 931

18

313

11,5

62

5

4

1 763 714

<na>

<na>

14,3

56

5

5

9 444 009

28

<na>

14,9

66

5

6

9 440 402

Use colformat_double() to see the effect of digits.

head(airquality) |> 
  mutate(rnum = runif(6, min = 100000, max = 10000000)) |> 
  flextable() |> 
  colformat_double() |> 
  autofit()

Ozone

Solar.R

Wind

Temp

Month

Day

rnum

41

190

7,40

67

5

1

1 378 673,87

36

118

8,00

72

5

2

8 351 143,27

12

149

12,60

74

5

3

4 733 383,30

18

313

11,50

62

5

4

5 544 839,04

<na>

<na>

14,30

56

5

5

5 571 473,26

28

<na>

14,90

66

5

6

2 465 058,12