---
title: "Some minimal examples"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Some minimal examples}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r, echo=TRUE, message=FALSE, warning=FALSE}
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. 
```{r message=FALSE, warning=FALSE, echo = T}
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)
```

## Combining tables

You can combine tables with `+`, `/` and `*` operators.
```{r,echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE, out.width = "600px"}
create_table <- function(df,name){
 df %>%
   mmtable(cells = value,table_name = name) +
   header_top(year) +
   header_left(country) +
   header_top_left(var)  +
   header_left_top(continent)
}
```

```{r, echo=FALSE,message=FALSE, warning=FALSE, paged.print=FALSE, out.width = "600px"}
gapminder_tbl <-
 gapminder_mm %>%
 mutate(continent_n = continent, var_n = var) %>%
 group_by(continent_n,var_n) %>% nest() %>%
 ungroup() %>% 
 filter(
   (continent_n == "Oceania" & var_n == "Population") | 
   (continent_n == "Oceania" & var_n == "GDP") |
   (continent_n == "Asia" & var_n == "Population") |
   (continent_n == "Asia" & var_n == "GDP"))  %>% 
 arrange(continent_n,var_n) %>% 
 mutate(name = paste("Table",row_number(), sep =" " )) %>% 
 mutate(table =map2(data,name,create_table))
```

```{r,echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE, out.width = "600px"}
t1 <- gapminder_tbl %>% filter(name == "Table 1") %>% pull(table) %>% .[[1]]
t2 <- gapminder_tbl %>% filter(name == "Table 2") %>% pull(table) %>% .[[1]]
t3 <- gapminder_tbl %>% filter(name == "Table 3") %>% pull(table) %>% .[[1]]
t4 <- gapminder_tbl %>% filter(name == "Table 4") %>% pull(table) %>% .[[1]]
```

The `+` operator places tables side-by-side (sharing headers)  
```{r message=FALSE, warning=FALSE, echo = T}
ex1 <- t1 + t2
print(ex1)
```

The `/` operator places tables on top of one another (sharing headers)  
```{r message=FALSE, warning=FALSE, echo = T}
ex2 <- t1 / t3
print(ex2)
```

The `*` operator "integrates" tables  
```{r message=FALSE, warning=FALSE, echo = T}
ex3 <- t1 * t3 * t4 *  t2
print(ex3)
```

## Formatting tables

mmtable2 outputs tables using the gt package's format.

This means you can alter formatting using many existing gt styling commands.
```{r message=FALSE, warning=FALSE, echo = T}
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)
```

### Merged header columns 

Table headers can be merged with `header_merged_cols()`. 
This supports an aributrary number of column headers. 
```{r message=FALSE, warning=FALSE, echo = T}
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)
```

### Alternative pipe syntax

Adding the `add_` prefix to functions allows use of `%>%` in place of `+`.
```{r message=FALSE, warning=FALSE, echo = T}
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)
```

