Some minimal examples

library(gapminder)
library(dplyr)
library(tidyr)
library(stringr)
library(purrr)
library(gt)
library(mmtable2)

Adding headers

You can add headers using data from a column in your data frame.

Its placement will depend on your your choice of header_* functions.

Header options include: top, top_left, left, and left_top.

Here’s an example.

gm_df <- gapminder_mm %>% filter(var != "Life expectancy")
style_list <- list(cell_borders(sides = "top",color = "grey"))

gm_table <- 
 gm_df %>% 
  mmtable(cells = value) +
  header_left(year) +
  header_top(country) +
  header_left_top(var)  +
  header_top_left(continent) + 
  header_format(var, scope = "table", style = style_list)

print(gm_table)
Africa Americas Asia Europe Oceania
Botswana Gabon Canada United States Kuwait Singapore Ireland Norway Australia New Zealand
GDP 1992 8.0 13.5 26.3 32.0 34.9 24.8 17.6 34.0 23.4 18.4
1997 8.6 14.7 29.0 35.8 40.3 33.5 24.5 41.3 27.0 21.1
2002 11.0 12.5 33.3 39.1 35.1 36.0 34.1 44.7 30.7 23.2
2007 12.6 13.2 36.3 43.0 47.3 47.1 40.7 49.4 34.4 25.2
Population 1992 1.3 1.0 28.5 256.9 1.4 3.2 3.6 4.3 17.5 3.4
1997 1.5 1.1 30.3 272.9 1.8 3.8 3.7 4.4 18.6 3.7
2002 1.6 1.3 31.9 287.7 2.1 4.2 3.9 4.5 19.5 3.9
2007 1.6 1.5 33.4 301.1 2.5 4.6 4.1 4.6 20.4 4.1

Combining tables

You can combine tables with +, / and * operators.

The + operator places tables side-by-side (sharing headers)

ex1 <- t1 + t2
print(ex1)
Table 1 Table 2
GDP Population
1992 1997 2002 2007 1992 1997 2002 2007
Asia Kuwait 34.9 40.3 35.1 47.3 1.4 1.8 2.1 2.5
Singapore 24.8 33.5 36.0 47.1 3.2 3.8 4.2 4.6

The / operator places tables on top of one another (sharing headers)

ex2 <- t1 / t3
print(ex2)
GDP
1992 1997 2002 2007
Table 1 Asia Kuwait 34.9 40.3 35.1 47.3
Singapore 24.8 33.5 36.0 47.1
Table 3 Oceania Australia 23.4 27.0 30.7 34.4
New Zealand 18.4 21.1 23.2 25.2

The * operator “integrates” tables

ex3 <- t1 * t3 * t4 *  t2
print(ex3)
GDP Population
1992 1997 2002 2007 1992 1997 2002 2007
Asia Kuwait 34.9 40.3 35.1 47.3 1.4 1.8 2.1 2.5
Singapore 24.8 33.5 36.0 47.1 3.2 3.8 4.2 4.6
Oceania Australia 23.4 27.0 30.7 34.4 17.5 18.6 19.5 20.4
New Zealand 18.4 21.1 23.2 25.2 3.4 3.7 3.9 4.1

Formatting tables

mmtable2 outputs tables using the gt package’s format.

This means you can alter formatting using many existing gt styling commands.

gm_table_formatted <- 
gapminder_mm %>% 
  filter(var != "Life expectancy") %>% 
  mmtable(cells = value) +
  header_top(year) +
  header_left(country) +
  header_top_left(var)  +
  header_left_top(continent)  +
  cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) +
  header_format(header = year, style = list(cell_text(align = "right"))) +
  header_format("all_cols", style = list(cell_text(weight = "bolder"))) +
  header_format("all_rows", style = list(cell_text(weight = "bolder"))) +
  header_format(continent, scope= "table", 
                style = list(cell_borders(sides = "top",color = "grey"))) 

print(gm_table_formatted)
GDP Population
1992 1997 2002 2007 1992 1997 2002 2007
Africa Botswana 8.0 8.6 11.0 12.6 1.3 1.5 1.6 1.6
Gabon 13.5 14.7 12.5 13.2 1.0 1.1 1.3 1.5
Americas Canada 26.3 29.0 33.3 36.3 28.5 30.3 31.9 33.4
United States 32.0 35.8 39.1 43.0 256.9 272.9 287.7 301.1
Asia Kuwait 34.9 40.3 35.1 47.3 1.4 1.8 2.1 2.5
Singapore 24.8 33.5 36.0 47.1 3.2 3.8 4.2 4.6
Europe Ireland 17.6 24.5 34.1 40.7 3.6 3.7 3.9 4.1
Norway 34.0 41.3 44.7 49.4 4.3 4.4 4.5 4.6
Oceania Australia 23.4 27.0 30.7 34.4 17.5 18.6 19.5 20.4
New Zealand 18.4 21.1 23.2 25.2 3.4 3.7 3.9 4.1

Merged header columns

Table headers can be merged with header_merged_cols(). This supports an aributrary number of column headers.

row_list <- cells_body(rows = c(1,5))
col_list <- cells_body(columns = c(3,5,7,9,11))
style_list <- list(cell_borders(sides = "top",color = "grey"))
style_list2<- list(cell_borders(sides = "left",color = "grey"))
gm_df <- gapminder_mm %>% filter(var != "Life expectancy")
style_list3 = list(cell_text(align = "left"))

gm_table_merged <- 
 gm_df %>% 
  mmtable(cells = value) +
  header_left(year) +
  header_top(country) +
  header_left_top(var)  +
  header_top_left(continent)  +
  header_format(continent,style_list3 ) +
  header_merged_cols()
 
print(gm_table_merged)
Botswana Gabon Canada United States Kuwait Singapore Ireland Norway Australia New Zealand
GDP 1992 8.0 13.5 26.3 32.0 34.9 24.8 17.6 34.0 23.4 18.4
1997 8.6 14.7 29.0 35.8 40.3 33.5 24.5 41.3 27.0 21.1
2002 11.0 12.5 33.3 39.1 35.1 36.0 34.1 44.7 30.7 23.2
2007 12.6 13.2 36.3 43.0 47.3 47.1 40.7 49.4 34.4 25.2
Population 1992 1.3 1.0 28.5 256.9 1.4 3.2 3.6 4.3 17.5 3.4
1997 1.5 1.1 30.3 272.9 1.8 3.8 3.7 4.4 18.6 3.7
2002 1.6 1.3 31.9 287.7 2.1 4.2 3.9 4.5 19.5 3.9
2007 1.6 1.5 33.4 301.1 2.5 4.6 4.1 4.6 20.4 4.1

Alternative pipe syntax

Adding the add_ prefix to functions allows use of %>% in place of +.

gm_table_piped <- 
gapminder_mm %>% 
  filter(var != "Life expectancy") %>% 
  mmtable(cells = value, use_default_formats = T) %>% 
  add_header_top(year) %>% 
  add_header_left(country) %>% 
  add_header_top_left(var)  %>% 
  add_header_left_top(continent)  %>% 
  add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>% 
  add_header_format(header = year, style = list(cell_text(align = "right"))) %>% 
  add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>% 
  add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>% 
  add_header_format(continent, scope= "table", 
                style = list(cell_borders(sides = "top",color = "grey"))) 

print(gm_table_piped)
GDP Population
1992 1997 2002 2007 1992 1997 2002 2007
Africa Botswana 8.0 8.6 11.0 12.6 1.3 1.5 1.6 1.6
Gabon 13.5 14.7 12.5 13.2 1.0 1.1 1.3 1.5
Americas Canada 26.3 29.0 33.3 36.3 28.5 30.3 31.9 33.4
United States 32.0 35.8 39.1 43.0 256.9 272.9 287.7 301.1
Asia Kuwait 34.9 40.3 35.1 47.3 1.4 1.8 2.1 2.5
Singapore 24.8 33.5 36.0 47.1 3.2 3.8 4.2 4.6
Europe Ireland 17.6 24.5 34.1 40.7 3.6 3.7 3.9 4.1
Norway 34.0 41.3 44.7 49.4 4.3 4.4 4.5 4.6
Oceania Australia 23.4 27.0 30.7 34.4 17.5 18.6 19.5 20.4
New Zealand 18.4 21.1 23.2 25.2 3.4 3.7 3.9 4.1