---
title: "Tables (HTML output)"
author: "Katia Bulekova"
date: "`r Sys.Date()`"
output:
html_document:
toc: true
number_sections: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
# knitr
*knitr* package includes a handy *kable()* function that produces simple tables:
```{r}
data(mtcars)
knitr::kable( head(mtcars))
```
***********
# 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) )
```
***********
# DT
*DT* package can display matrices and data frames as tables and provides filtering and sorting. It is especially useful to display long tables that do not fit on a single page:
```{r}
DT::datatable(data=mtcars)
```
***********
# kableExtra
*kableExtra* package extends functionality of the *kable* package.
```{r, warning=FALSE, message=FALSE}
library(kableExtra)
df <- mtcars[1:5, 1:6]
df %>%
kbl()
```
***
This package has a few themes:
- kable_paper("hover", full_width = F)
- kable_classic(full_width = F, html_font = "Cambria")
- kable_classic_2(full_width = F)
- kable_minimal()
- kable_material(c("striped", "hover"))
- kable_material_dark()
```{r}
df %>%
kbl() %>%
kable_minimal()
```
***
## kable_styling
Function *kable_styling()* allows other ways to customize the output table:
```{r}
df %>%
kbl() %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
```
***
*kable_styling()* includes the following options:
- *full_width* - TRUE or FALSE (default - TRUE)
- *position* - "left", "center", "right", "float_left", "float_right"
- *font_size* - a numeric value
- *fixed_thead* - T or F (option to fix the header row on top)
## Column_spec
This function can be used to format specific columns, e.g:
```{r}
df %>%
kbl() %>%
kable_minimal() %>%
column_spec(1, bold = T) %>%
column_spec(2, background="lightyellow")
```
***
We can also use a *conditional* formatting for the values in a column based on a value of another variable:
```{r}
df %>%
kbl() %>%
kable_minimal() %>%
column_spec(1, bold = T) %>%
column_spec(5, color=ifelse(df$hp==110, "red", "black"))
```
***
## Row_spec
Similar to column_spec, we can use *row_spec()* function to format rows:
```{r}
df %>%
kbl() %>%
kable_paper(full_width = F) %>%
column_spec(1, bold = T) %>%
row_spec(3, background="brown", color="white", bold = TRUE)
```
***
## Sparklines
We can include inline plots into this table. The following types of plots are available:
- spec_hist
- spec_boxplot
- spec_plot
First let's prepare the data:
```{r}
(mpg_list <- split(mtcars$mpg, mtcars$cyl))
```
Then we need to define a table with the desired number of rows and columns and give columns names:
```{r}
df_plot <- data.frame(cyl = c(4, 6, 8),
mpg_box = "",
mpg_hist = "",
min = unname(sapply(mpg_list, min)),
max = unname(sapply(mpg_list, max)),
mean = unname(sapply(mpg_list, mean)))
```
Now we can add the plots into the first two columns:
```{r}
df_plot %>%
kbl(booktabs = TRUE) %>%
kable_paper(full_width = FALSE) %>%
column_spec(2, image = spec_boxplot(mpg_list)) %>%
column_spec(3, image = spec_hist(mpg_list))
```
For more examples see [Create Awesome HTML Table with knitr::kable and kableExtra](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html)