---
title: "tableHTML"
author: "Theo Boutaris"
date: "`r Sys.Date()`"
output:
html_document
vignette: >
%\VignetteIndexEntry{tableHTML Themes}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here we are exploring the different themes that can be used with `tableHTML`
```{r load_package}
library(tableHTML)
```
### Specifying a theme
Although the package has been designed so that it gives the utmost freedom to the user to style the
HTML table as they please, some themes have been included for those who need something quick and nice.
The package offers three themes for now: scientific, rshiny-blue, and colorize. To use them, you need to use the
`add_theme` function.
**Notice**: When working with themes you can still add extra css (using the add\_css\_* family from
below) but you will not be able to overwrite the styling that is there e.g. to change the width of
the lines.
## scientific
This is the scientifc theme where the table design resembles scientific tables for publishing.
```{r theme-scientific}
tableHTML(mtcars, widths = c(140, rep(50, 11))) %>%
add_theme('scientific')
```
\
```{r scientific_2}
tableHTML(mtcars,
rownames = FALSE,
widths = c(140, rep(50, 11)),
row_groups = list(c(10, 10, 12), c('Group 1', 'Group 2', 'Group 3')),
second_headers = list(c(3, 4, 5), c('col1', 'col2', 'col3'))) %>%
add_theme('scientific')
```
## rshiny-blue
This theme matches the color of the standard shiny apps.
```{r theme-rshiny-blue}
tableHTML(mtcars, widths = c(140, rep(50, 11))) %>%
add_theme('rshiny-blue')
```
\
```{r theme_rshiny_blue_2}
tableHTML(mtcars,
rownames = FALSE,
widths = c(140, rep(50, 11)),
row_groups = list(c(10, 10, 12), c('Group 1', 'Group 2', 'Group 3')),
second_headers = list(c(3, 4, 5), c('col1', 'col2', 'col3'))) %>%
add_theme('rshiny-blue')
```
## colorize
This theme is used to create MS Excel-like tables (with any color you like). You can also use it to highlight
specific rows (usually corresponding to some aggregation function, for example totals or averages). The arguments that can be
used with the colorize theme are `color`, `total_rows`, and `id_column`. The default color is
steelblue and by default no rows are chosen to be highlighted. You can also highlight the first column
with the `id_column` argument. The documentation can be found at `?add_theme_colorize`.
```{r totals_1}
df <- mtcars[, 1:6]
df %>%
tableHTML(widths = c(150, rep(70, ncol(df))), rownames = TRUE) %>%
add_theme('colorize')
```
\
```{r totals_2}
df <- mtcars[, 1:6]
df %>%
tableHTML(widths = c(150, rep(70, ncol(df))), rownames = TRUE) %>%
add_theme('colorize', color = 'darkgreen')
```
\
```{r totals_3}
df <- mtcars[, 1:6]
df['Mean', ] <- (df %>% apply(2, mean))
df %>%
tableHTML(widths = c(150, rep(70, ncol(df))), rownames = TRUE) %>%
add_theme('colorize', color = c('steelblue', 'red'))
```
\
#### Here we are hightlighting the last row which contains the average of the columns.
```{r totals_4}
df <- mtcars[, 1:6]
df['Mean', ] <- (df %>% apply(2, mean))
df %>%
tableHTML(widths = c(150, rep(70, ncol(df))), rownames = TRUE) %>%
add_theme('colorize', color = c('steelblue', 'red'), total_rows = nrow(df))
```
## Totals with add_theme_colorize
Instead of using the umbrella function `add_theme`, you could also explicitly use `add_theme_colorize`. The two functions are identical in
terms of the output. To see the documentation visit `?add_theme_colorize`.
```{r total_theme_1}
# one total row
x1 <- sample(1:100, 12)
x2 <- sample(1:100, 12)
x3 <- sample(1:100, 12)
df <- data.frame(Month = month.abb, x1, x2, x3,
stringsAsFactors = FALSE)
df[nrow(df) + 1, ] <- c('Total', sum(x1), sum(x2), sum(x3))
df %>%
tableHTML(widths = rep(50, 4), rownames = FALSE) %>%
add_theme_colorize(color = 'darkred', total_rows = nrow(df))
```
\
```{r total_theme_2}
df_q <- rbind(
df[1:3, ],
c('Sum1', sum(x1[1:3]), sum(x2[1:3]), sum(x3[1:3])),
df[4:6, ],
c('Sum2', sum(x1[4:6]), sum(x2[4:6]), sum(x3[4:6])),
df[7:9, ],
c('Sum3', sum(x1[7:9]), sum(x2[7:9]), sum(x3[7:9])),
df[10:12, ],
c('Sum4', sum(x1[10:12]), sum(x2[10:12]), sum(x3[10:12])))
# Two colors and an id_column
df_q %>%
tableHTML(widths = rep(50, 5),
rownames = FALSE,
row_groups = list(c(4, 4, 4, 4),
c('Q1', 'Q2', 'Q3', 'Q4'))) %>%
add_theme_colorize(color = c('pink3', 'yellow2'),
total_rows = c(4, 8, 12, 16), id_column = TRUE)
```