```{r setup, echo=FALSE, message=FALSE, warning=FALSE}
# Set up the environment
library(knitr)
opts_chunk$set(cache.path='cache/', fig.path='img/', cache=F, tidy=T, fig.keep='high', echo=T, dpi=100, warnings=F, message=F, comment=NA, warning=F, results='as.is') #out.width=700, 
library(pander)
panderOptions('table.split.table', Inf)
set.seed(1)
library(dplyr)
options(stringsAsFactors = FALSE)
```

```{r libraries}
library(ggplot2)
# install.packages("plotly")
library(plotly)
```

# Letter combination 

There are `r paste(length(LETTERS))` letters in English alphabet.

We count the number of combinations two, three, ..., five different letters can be selected. Assemble those counts in a list
```{r count_combinations}
max_number_of_combinations <- 5
count_combinations <- list()
for (i in 1:max_number_of_combinations) {
  count_combinations <- c(count_combinations, ncol(combn(length(LETTERS), i)))
}
```

A total of `r paste(count_combinations[[2]])` pairwise combinations of them can be selected. Or, `r paste(count_combinations[[3]])` combinations of three letters can be selected. 

We can plot the bar graph of those counts with `ggplot`.

```{r fig.height=4, fig.width=4}
combination_counts <- data.frame(
  combinations = seq(1, length(count_combinations)), 
  counts = unlist(count_combinations), 
  stringsAsFactors = FALSE)

p <- ggplot(combination_counts, aes(x = combinations, y = counts, fill = factor(combinations))) +
  geom_bar(stat = "identity") +
  ggtitle("Alphabet combinatorics") +
  theme(legend.position="none") +
  labs(x = "Number of selected letters", y = "Number of combinationsin the alphabet")
plot(p)
```

Or, an interactive bar graph with `plotly`

```{r fig.height=4, fig.width=4}
ggplotly(p)
```

# Tables

## knitr::kable

Check different table representations with `?islands` data

```{r}
knitr::kable(head(islands))
```

## pander::pander

Or, with `?state.x77` data

```{r}
pander::pander(head(state.x77))
```

## xtable::xtable

Or, with `?WorldPhones` data

```{r results='asis'}
tab <- xtable::xtable(WorldPhones)
print(tab, type = "html")
```

## DT::datatable

Or, with `?mtcars` data

```{r}
DT::datatable(mtcars)
```


```{r session_info, results='asis'}
diagnostics <- devtools::session_info()
platform <- data.frame(diagnostics$platform %>% unlist, stringsAsFactors = FALSE)
colnames(platform) <- c("description")
pander(platform)

packages <- as.data.frame(diagnostics$packages)
pander(packages[ packages$`*` == "*", ])
```
