Constructing tables

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.

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 <- 
 gm_df %>% 
  mmtable(cells = value) +
  header_left(year) +
  header_top(country) +
  header_left_top(var)  +
  header_top_left(continent)  +
  header_format(header = var, scope = "table", style = style_list) +
  header_format(continent,style_list3)

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