---
title: "Styling tables"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Styling tables}
  %\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)
```

## 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}
style_list <- list(cell_borders(sides = "top",color = "lightgrey",weight = px(3)))
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(var,scope = "table",style = style_list) +
  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 = style_list) 

print(gm_table_piped)
```


# Working with colors 


mmtable2 uses the style features of gt. This gives you a lot of control over the format of your table. As an example, a rainbow histogram is constructed below. 

### Data generation

The code below generates a set of random numbers and bins them in 10s.

```{r message=FALSE, warning=FALSE, echo = T}
library(mmtable2)
library(tidyverse)

set.seed(123)
df <- 
tibble(data = rnorm(100,100,30) %>% round()) %>% 
  mutate(rounded = floor(data/10)*10) %>% 
  mutate(rounded_chr = paste0(rounded,"'s"))  %>% 
  arrange(rounded,desc(data))

df %>% glimpse
```

### Table construction

The code below constructs a table with these values, and then adds a list of formatting commands. 

```{r message=FALSE, warning=FALSE, echo = T}
# Generate random data 
df <- 
tibble(data = rnorm(100,100,30) %>% round()) %>% 
  mutate(rounded = floor(data/10)*10) %>% 
  mutate(rounded_chr = paste0(rounded,"'s"))  %>% 
  arrange(rounded,desc(data))

# Create table  
table <- 
df %>% 
  mmtable(cells = data) +
  header_top_left(rounded_chr) + 
  header_format(rounded_chr, 
                list(gt::cell_fill(color = "black"),
                     gt::cell_text(color = "white")))

# Format table
colors <-  c("#ff0000","#ffa500","#ffff00","#008000",
             "#0000ff","#4b0082","#ee82ee")
color_list <- 
  map2(unique(df$rounded_chr),rep(colors,2), 
       function(nums,colours){
        cells_format(cell_predicate =nchar(data) %in% c(2,3) & 
                     rounded_chr == nums, 
                     gt::cell_fill(color = colours))
         }
       )

print(append(list(table),color_list) %>% reduce(`+`))
```
