library(flextable)
library(charlatan)
use_df_printer()
Nous allons simuler un jeu de données avec le package ‘charlatan’ dédié à cette tâche.
set.seed(007)
n <- 10
people <- data.frame(
name = ch_name(n = n, locale = "fr_FR"),
birthday = as.Date(as.POSIXct(unlist(ch_date_time(n = n)), origin = "1970-01-01")) + 20*365,
n_children = as.integer(ch_integer(n = n, min = 0, max = 4)),
money = rnorm(n = n, mean = 500, sd = 22),
weight = runif(n = n, min = 50, max = 90),
height = rnorm(n = n, mean = 170, sd = 5),
n_peanuts = ch_integer(n = n, min = 500000, max = 1225193),
eye_color = as.factor(sample(c("blue", "green", "dark"), prob = c(.15, .3, .55), size = n, replace = TRUE)),
stringsAsFactors = FALSE
)
people[2:4, 4] <- NA_real_
people
name | birthday | n_children | money | weight | height | n_peanuts | eye_color |
character | Date | integer | numeric | numeric | numeric | numeric | factor |
Tristan Dias L'Mathiéu | 2033-01-13 | 4 | 502.2 | 72.6 | 173.5 | 628,916 | dark |
Raymond Guillet-Begue | 2019-12-10 | 2 | 81.6 | 171.6 | 1,214,582 | blue | |
Alix Bérnard | 2007-05-17 | 4 | 57.4 | 175.5 | 616,049 | green | |
Élodie Normand | 2015-06-21 | 0 | 83.3 | 173.8 | 1,011,908 | green | |
Célina Laporte-Francois | 2027-04-07 | 3 | 508.1 | 71.2 | 175.8 | 555,444 | green |
Aurélie Imbert | 1990-08-02 | 1 | 537.6 | 68.9 | 176.3 | 880,714 | dark |
Augustin Royér | 2016-05-06 | 1 | 515.9 | 72.5 | 173.5 | 986,474 | green |
William Lucas | 2011-08-21 | 4 | 510.6 | 58.8 | 172.2 | 1,137,771 | dark |
Laurence Fernandes | 2003-10-05 | 1 | 465.5 | 78.3 | 165.4 | 760,736 | dark |
Victoire L'Navarro | 2006-06-07 | 4 | 507.0 | 62.8 | 166.9 | 847,084 | green |
Tout d’abord, définissons une opération de post-traitement qui personnalisera l’affichage HTML. Cette fonction sera exécutée systématiquement avant l’impression du tableau.
set_flextable_defaults(
post_process_html = function(x){
theme_tron(x) |> autofit()
}
)
Nous pouvons maintenant nous concentrer sur le formattage
du contenu des cellules. Les fonctions colformat_*
sont
dédiées à cette tâche et sont simples d’utilisation.
ft <- flextable(people) |>
colformat_double(digits = 1,
big.mark = " ", decimal.mark = ",",
na_str = "na", j = ~ . - money) |>
colformat_double(digits = 0,
big.mark = " ",
suffix = "€",
na_str = "unknown", j = "money") |>
colformat_int(j = "n_children", prefix = "# ") |>
colformat_char(j = "eye_color", prefix = "color: ") |>
colformat_date(fmt_date = "%d/%m/%Y")
ft
name | birthday | n_children | money | weight | height | n_peanuts | eye_color |
Tristan Dias L'Mathiéu | 13/01/2033 | # 4 | 502€ | 72,6 | 173,5 | 628 916,0 | color: dark |
Raymond Guillet-Begue | 10/12/2019 | # 2 | unknown | 81,6 | 171,6 | 1 214 582,0 | color: blue |
Alix Bérnard | 17/05/2007 | # 4 | unknown | 57,4 | 175,5 | 616 049,0 | color: green |
Élodie Normand | 21/06/2015 | # 0 | unknown | 83,3 | 173,8 | 1 011 908,0 | color: green |
Célina Laporte-Francois | 07/04/2027 | # 3 | 508€ | 71,2 | 175,8 | 555 444,0 | color: green |
Aurélie Imbert | 02/08/1990 | # 1 | 538€ | 68,9 | 176,3 | 880 714,0 | color: dark |
Augustin Royér | 06/05/2016 | # 1 | 516€ | 72,5 | 173,5 | 986 474,0 | color: green |
William Lucas | 21/08/2011 | # 4 | 511€ | 58,8 | 172,2 | 1 137 771,0 | color: dark |
Laurence Fernandes | 05/10/2003 | # 1 | 466€ | 78,3 | 165,4 | 760 736,0 | color: dark |
Victoire L'Navarro | 07/06/2006 | # 4 | 507€ | 62,8 | 166,9 | 847 084,0 | color: green |