---
title: "Tables (PDF output)"
author: "Katia Bulekova"
date: "`r Sys.Date()`"
output: 
  pdf_document:
    extra_dependencies: ["float"]
    keep_tex: true
header-includes:
    - \usepackage[table]{xcolor}
    - \usepackage{caption}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, cache = TRUE, fig.pos = "!h", out.extra = "")
options(knitr.table.format = "latex", 
        knitr.kable.NA = '',
        kableExtra.latex.load_packages = FALSE)
library(tidyverse)
```

Here we will explore the functions that we can use to generate well-formatted tables when the output is a pdf document.


# knitr

*knitr* package includes a handy *kable()* function that produces simple tables:

```{r}
data(mtcars)
knitr::kable( head(mtcars))
```

\newpage


# pander

*pander* package is a relatively simple to use package. It can produce well-formatted tables for the outputs of summary() function for the datasets and models:
```{r}
library(pander)
pander( summary(mtcars[, 1:4]) )
```

```{r}
model <- lm( mpg ~ cyl, data=mtcars )    # a simple lenear regression
pander( summary(model) )  
```


\newpage

# kableExtra 

*kableExtra* package extends functionality of the *kable* package.


```{r, warning=FALSE, message=FALSE}
library(kableExtra)
df <- mtcars[1:5, 1:6]

df %>%
  kbl() %>%
  kable_styling(latex_options = c("hold_position"))
```


To make the output to look more like an HTML output:
```{r}
df %>%
  kbl(booktabs=TRUE) %>%
  kable_styling(latex_options = c("hold_position"))
```

*kable_styling()* in LaTeX uses the same syntax and structure as in HTML. However, instead
of bootstrap_options, we should specify latex_options instead:

```{r}
df %>%
  kbl(booktabs=TRUE) %>%
  kable_styling(latex_options = c("striped", "hold_position"), stripe_index = c(1,2, 5:6))
```

We want to make sure the table is placed in the position where we want it to be. 
Notice, how the previous 2 tables 
To force this behavior - use "hold_position" option ( a stronger version is "HOLD_position"):

```{r}
df %>%
  kbl(booktabs=TRUE) %>%
  kable_styling(latex_options = c("striped", "hold_position"))
```

\newpage

If the table is long, use *repeat_header* value for *latex_options* to insure that the table header is repeated if the table is split between multiple pages.

## kable_styling

*kable_styling()* allows us to control the table width and position: `kable_styling(full_width = T)` or `kable_styling(position = "center")`.

See "Notebook_2_Tables_HTML.Rmd" for more options for this function.

## column_spec

This function can be used to format specific columns, e.g:
```{r}
df %>%
  kbl(booktabs = T) %>%
  kable_styling(latex_options = c("hold_position")) %>%
  column_spec(1, bold = T) %>%
  column_spec(2, background="yellow")
```



We can also use a *conditional* formatting for the values in a column based on a value of another variable:
```{r}
df %>%
  kbl(booktabs = T) %>%
  kable_styling(latex_options = c("hold_position")) %>%
  column_spec(1, bold = T) %>%
  column_spec(5, color=ifelse(df$hp==110, "red", "black"))
```

## Grouped Columns

For the cases when table columns are grouped, we can use *add_header_above()* function.
The value that follows the name of the group, indicates how many columns are included in this group:
```{r}
df %>%
  kbl(booktabs = T) %>%
  kable_styling(latex_options = c("hold_position")) %>%
  column_spec(1, bold = T) %>%
  add_header_above(c(" " = 1, "Group 1" = 3, "Group 2" = 3))
```

\newpage

## Grouped Rows

We can group rows using *pack_rows() function:

```{r}
df <- mtcars[1:10, 1:6]
df %>%
  kbl(booktabs = T) %>%
  kable_styling(latex_options = c("hold_position")) %>%
  column_spec(1, bold = T) %>%
  pack_rows("Group 1", 4, 7, latex_gap_space = "1em") %>%
  pack_rows("Group 2", 8, 10, latex_gap_space = "1em")
```




For more examples see [Create Awesome LaTeX Table with knitr::kable and kableExtra](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf)
