Chapter 2 Make homogeneous tables
When a flextable is created, some default values are used as the font family, the font size, padding, text alignment… These default properties will be used when creating the flextable, and when relevant, by the theme functions.
Default values are frequently used when creating tables with ‘flexable’. It is recommended to specify them only once in the R session in order to consistent tables.
They can be read them with function get_flextable_defaults()
and can be 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 makes possible to set some global options that apply to all tables. In an ‘R Markdown’ document, they should be defined in the first chunk; in an R script the should be defined at the beginning of the script.
To make a nice table, 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
.
Values can be reset 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 |
init_flextable_defaults()
flextable(dat)
wool |
L |
M |
H |
---|---|---|---|
A |
44.56 |
24.00 |
24.56 |
B |
28.22 |
28.77 |
18.78 |
2.1 Major default values
It’s recommanded to have one single font used in the whole table.
To do so, you can call font()
function:
wool |
L |
M |
H |
---|---|---|---|
A |
44.56 |
24.00 |
24.56 |
B |
28.22 |
28.77 |
18.78 |
Rather than systematically writing this call, we recommend that you set this as a default setting using 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 default values that seem important to us because they are supposed to not vary too much for different tables.
-
font.family
, a single character value. We recommand to use a sans serif, ‘Helvetica’ and ‘Verdana’ are easy to read, quite populars and easy to install on Windows, Linux and MacOS. -
font.size
is also very important. It is set to 11 by default. It should not vary too much in a table. -
padding
should also be set globally, use values related to the font size. For example, for a size of 12, use a padding of 3 points; for a size of 10, use a padding of 2 points; for a size of 20, use a padding of 6 points. -
border.color
, default to black, can be set to gray for example. -
line_spacing
, we recommand to set its default 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. It is possible in flextable 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 some dedicated parameters for numbers display:
- digits
- decimal.mark
- big.mark
You won’t see digits
applied when creating a table because
the default formatter for numerics is function colformat_num()
that formats things roughly as what you see on the 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 |