Custom formatting

diamonds %>% 
  select(-clarity) %>% 
  mutate(relative_price = price / max(price), .before = "x") %>% 
  group_by(across(where(is.ordered))) %>% 
  summarize(across(where(is.double), mean), .groups = "drop") %>% 
  format_percent(relative_price) %>% 
  rename_with(~str_c("category_", .), cut:color) %>% 
  rename_with(~str_c("property_", .), carat:table) %>% 
  rename_with(~str_c("dimension_", .), x:z) -> diamonds_summary

diamonds_summary %>% head
#> # A tibble: 6 × 9
#>   category_cut category_color property_carat property_depth property_table
#>   <ord>        <ord>                   <dbl>          <dbl>          <dbl>
#> 1 Fair         D                       0.920           64.0           59.0
#> 2 Fair         E                       0.857           63.3           59.4
#> 3 Fair         F                       0.905           63.5           59.5
#> 4 Fair         G                       1.02            64.3           58.8
#> 5 Fair         H                       1.22            64.6           58.7
#> 6 Fair         I                       1.20            64.2           59.2
#> # … with 4 more variables: relative_price <formttbl>, dimension_x <dbl>,
#> #   dimension_y <dbl>, dimension_z <dbl>

Header words are automatically identified by the following regular expression: "^.*(?=(_|\\.))". Leaving the header_word argument blank produces the same result as providing the commented out argument below. The last id column controls merging and greyscaling. Numeric columns are given some automatic formatting within the make_flextable function.

diamonds_summary %>% 
  make_flextable(last_id_col = 2,
                 # header_words = c("category", "property", "dimension"), 
                 theme = "zebra_gold")

Automatic coloring

tibble(x = letters[1:10],
       y = -5:4,
       z = -c(-.5, -.2, -.1, 0, .1, .2, .3, .4, .6, .9)) %>% 
  format_percent(z) %>% 
  rename_with(~str_c("sample_", .)) %>% 
  make_flextable()
tibble(x = letters[1:10],
       y = -5:4,
       z = -c(-.5, -.2, -.1, 0, .1, .2, .3, .4, .6, .9)) %>% 
  format_percent(z) %>% 
  make_flextable()