Define how the text is displayed

Packages

library(flextable)
library(charlatan)

The dataset

Let’s simulate a fake dataset with package {charlatan}.

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_

Flextable

Setting defaults

First, let’s define a post-processing operation that will customize the HTML display. This function will be executed systematically before printing the table.

set_flextable_defaults(
  post_process_html = function(x){
    theme_tron(x) |> autofit()
  }
)

Define how to display cell content

Now we can focus on how to format the content of the cells. colformat_* functions are dedicated to this task.

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