---
title: "Introduction to formattable package"
author: "Kun Ren"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to formattable package}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo=FALSE, include=FALSE}
set.seed(123)
```

This package is designed for applying formatting on vectors and data frames to make data presentation easier, richer, more flexible and hopefully convey more information.

Atomic vectors are fundamental data structures in R. Some data can be read more easily with formatting. A numeric vector, for example, stores percentage numbers but is printed as typical floating numbers. This package provides functions to create data structures with predefined formatting rules so that these objects store the original data but are printed with formatting.

Several typical formattable numeric vectors are provided such as `percent`, `comma`, `currency`, `accounting` and `scientific`. These functions basically create numeric vectors with pre-defined formatting rules and parameters. For example,

```{r}
library(formattable)
p <- percent(c(0.1, 0.02, 0.03, 0.12))
p
```

The percent vector is no different from a numeric vector but has a percentage representation when printed. It works with arithmetic operations and other common functions and preserves its formatting.

```{r}
p + 0.05
p + percent(0.02)
p * 1.1
```

```{r}
max(p)
mean(p)
```

It also works with subsetting and sub-assignment:

```{r}
p[1:3]
p[[2]]
```

```{r}
p[[3]] <- 0.05
p
```

```{r}
balance <- accounting(c(1000, 500, 200, -150, 0, 1200))
balance
balance + 1000
```

These functions are specialized applications of what `formattable()` is designed to do. `formattable()` applies customizable formatting functions to objects of a wide range of classes like `numeric`, `logical`, `factor`, `Date`, `data.frame`, etc. 

When applied to `Date`, `formattable()` uses `format.Date()` as the default formatter function. The following code creates a `formattable Date` vector that is printed in the format of `%Y%m%d`. However, it is not a plain integer or character vector but of `Date` class and still allows date calculations. 

```{r}
dates <- formattable(as.Date(c("2016-05-01", "2016-05-10")), format = "%Y%m%d")
dates
dates + 30
```

When applied to a logical vector, we can customize how `TRUE` and `FALSE` values are printed.

```{r}
lv <- formattable(c(TRUE, FALSE, FALSE, TRUE), "yes", "no")
lv
!lv
```

Note that `isTRUE()` does not directly work with values of `lv` because `isTRUE()` uses `identical(x, TRUE)` and `lv[[1]]`, as a formattable logical value is not identical to a plain `TRUE`.

```{r}
lv[[1]]
isTRUE(lv[[1]])
```

If `isTRUE()` has to be applied, `lv == TRUE` returns a plain logical vector and works with `isTRUE()`. Other vectorized logical functions directly work with formattable logical vector with the formatting preserved.

```{r}
all(lv)
any(lv)
```

All formattable functions work with matrices and arrays.

```{r}
pm <- matrix(rnorm(6, 0.8, 0.1), 2, 3, 
  dimnames = list(c("a", "b"), c("X", "Y", "Z")))
pm
```

```{r}
fpm <- percent(pm)
fpm
```

```{r}
fpm["a", c("Y", "Z")]
```

```{r}
pa <- array(rnorm(12, 0.8, 0.1), c(2, 3, 2))
pa
```

```{r}
percent(pa)
```

When the formattable vectors are used as columns of a data frame, the formatting of each column is well preserved. A typical data frame may look more friendly with `formattable` column vectors. For example,

```{r}
p <- data.frame(
  id = c(1, 2, 3, 4, 5), 
  name = c("A1", "A2", "B1", "B2", "C1"),
  balance = accounting(c(52500, 36150, 25000, 18300, 7600), format = "d"),
  growth = percent(c(0.3, 0.3, 0.1, 0.15, 0.15), format = "d"),
  ready = formattable(c(TRUE, TRUE, FALSE, FALSE, TRUE), "yes", "no"))
p
```

The subset of a data frame also preserves the formatting of each column:

```{r}
p[1:3, c("name", "balance", "growth")]
```
