---
title: "Huxtable table demo"
author: "David Hugh-Jones"
date: "2/03/2017"
output:
  pdf_document:
    toc: yes
    includes:
      in_header: placeins-header.tex
    keep_tex: yes
  word_document: 
    toc: yes
  html_document: 
    toc: yes
  rtf_document: default
---


```{r setup, echo = FALSE}

library(huxtable)
knitr::opts_chunk$set(echo = FALSE)
options(huxtable.print = if (guess_knitr_output_format() == "latex") print_latex else print_html)
```


```{r}

make <- function(nrow = 2, ncol = 2, text1 = 'Some text', text2 = 'Some more text', more_text = 'Some text') {
  ht <- hux(a = rep(text1, nrow), b = rep(text2, nrow), add_colnames = FALSE)
  for (i in seq_len(ncol -2)) {
    ht <- cbind(ht, rep(more_text[i], nrow))
  }
  ht <- set_all_borders(ht, 1)
  ht <- set_background_color(ht, odds, everywhere, 'orange')
  ht <- set_background_color(ht, evens, everywhere, 'green')
  ht <- set_tabular_environment(ht, "tabular")
  ht
}

long <- 'A rather long row of text which goes on...'

```

# Width and colwidth

```{r}
ht <- make()
caption(ht) <- 'both unset'
ht

width(ht) <- 0.2
caption(ht) <- 'width 0.2'
ht

width(ht) <- NA
col_width(ht) <- c(.7, .3)
caption(ht) <- 'col\\_width .7, .3'
ht

width(ht) <- 0.2
caption(ht) <- 'width 0.2, col\\_width .7, .3'
ht

```

\FloatBarrier

# Position and width (and caption)

```{r}
ht <- make()
caption(ht) <- 'both unset'
ht

position(ht) <- 'left'
caption(ht) <- 'left pos'
ht

position(ht) <- 'right'
caption(ht) <- 'right pos'
ht

width(ht) <- .8
position(ht) <- 'left'
caption(ht) <- 'width .8, left pos'
ht

position(ht) <- 'right'
caption(ht) <- 'width .8, right pos'
ht

```


# Caption_pos and caption_width

```{r}

for (pos in c('topleft', 'topcenter', 'topright', 'top', 'bottomleft', 'bottomcenter', 'bottomright', 'bottom')) {
  ht <- make(nrow = 1) 
  caption(ht) <- paste0('caption\\_pos: ', pos)
  caption_pos(ht) <- pos
  knitr::knit_print(ht)
  
  caption_width(ht) <- .5
  caption(ht) <- paste(caption(ht), "caption\\_width: .5")
  knitr::knit_print(ht)
}

ht <- make(nrow = 1)


```

\FloatBarrier

# Col_width and align

```{r}
ht <- make(nrow = 3)
ht <- set_align(ht, c('left', 'center', 'right'))
caption(ht) <- 'Align by row: l/c/r, col\\_width unset'
ht

col_width(ht) <- c(.2, .8)
caption(ht) <- 'Align by row: l/c/r, col\\_width .2, .8'
```

# Different kinds of floats

```{r}
ht <- make()
latex_float(ht) <- 't'
caption(ht) <- 'latex\\_float t, top of page'
ht

latex_float(ht) <- 'b'
caption(ht) <- 'latex\\_float b, bottom of page'
ht

latex_float(ht) <- 'h'
caption(ht) <- 'latex\\_float h, here'
ht
```

\FloatBarrier

# Align and wrap

```{r}
ht <- make(nrow = 4, text1 = long, text2 = long)
ht[2:4,2] <- 'Shorter text'
wrap(ht)[,1] <- TRUE
ht <- set_align(ht, c('left', 'left', 'center', 'right'))
caption(ht) <- 'Align by row: l/l/c/r, wrap TRUE on left, width .3'
width(ht) <- 0.3
ht
```

\FloatBarrier

# Valign, row height and wrap

```{r}
ht <- make(nrow = 2, ncol = 4, text1 = long, text2 = 'text', more_text = c('text' , 'text'))
ht <- map_valign(ht, by_cols('top', 'top', 'middle', 'bottom'))
wrap(ht)[1,] <- TRUE
wrap(ht)[,1] <- TRUE
width(ht) <- .4
caption(ht) <- 'valign in columns 1-4: t/t/m/b; wrap TRUE at top (and bot left)'
ht

row_height(ht) <- rep('4\\baselineskip', 2)
caption(ht) <- 'valign in columns 1-4: t/t/m/b; wrap TRUE at top (and bot left); row heights 4\\\\baselineskip'
ht
```

\FloatBarrier

# Colwidth and wrap

```{r}
ht <- make(text1 = long, text2 = long)
wrap(ht)[1,] <- TRUE
col_width(ht) <- c(.8, .2)
caption(ht) <- 'wrap TRUE at top, col\\_width .8, .2'
ht
```

\FloatBarrier

# Width and wrap

```{r}
ht <- make()
width(ht) <- .2
wrap(ht)[1,] <- TRUE
caption(ht) <- 'wrap TRUE at top, width .2'
ht
```

\FloatBarrier

# Padding and wrap

```{r}
ht <- make(nrow = 4, text1 = long, text2 = long)
wrap(ht)[,1] <- TRUE
left_padding(ht)[1,] <- 18
right_padding(ht)[2,] <- 18
top_padding(ht)[3,] <- 18
bottom_padding(ht)[4,] <- 18
caption(ht) <- 'wrap TRUE on left; padding 18 by row l/r/t/b'
width(ht) <- .25
ht
```

\FloatBarrier

# Padding and colwidth

```{r}
ht <- make(nrow = 4, text1 = long, text2 = long)
col_width(ht) <- c(.2, .8)
left_padding(ht)[1,] <- 18
right_padding(ht)[2,] <- 18
top_padding(ht)[3,] <- 18
bottom_padding(ht)[4,] <- 18
caption(ht) <- 'col\\_width .2, .8 padding 18 by row l/r/t/b, wrap FALSE'
ht

wrap(ht) <- TRUE
caption(ht) <- 'col\\_width .2, .8 padding 18 by row l/r/t/b, wrap TRUE'
ht

```

\FloatBarrier

# Text and wrap

```{r}
ht <- make(nrow = 6, text1 = long, text2 = long)
wrap(ht)[,1] <- TRUE
font(ht)[1,] <- 'lmss'
bold(ht)[2:3,] <- TRUE
italic(ht)[3:4,] <- TRUE
text_color(ht)[5,] <- 'yellow'
font_size(ht)[6,] <- 14
caption(ht) <- 'wrap TRUE on left, font lmss at top, then bold/both/italic/yellow/big'
ht

```

# Newlines and wrap
```{r}
ht <- make(text1 = "first line\nsecond line", text2 = "first line\nsecond line")
caption(ht) <- "wrap TRUE on left, width set"
ht <- set_wrap(ht, everywhere, 1, TRUE)
ht <- set_wrap(ht, everywhere, 2, FALSE)
width(ht) <- .5
ht
```

ht <- make()


\FloatBarrier

# Multirow and wrap

```{r}
ht <- make(text1 = long)
wrap(ht)[,1] <- TRUE
rowspan(ht)[1,1] <- 2
rowspan(ht)[1,2] <- 2
caption(ht) <- 'multirow cells, wrap TRUE on left'

```

\FloatBarrier

# Multirow and multicol

```{r}
ht <- make(text1 = long, nrow = 3)
colspan(ht)[1,1] <- 2
rowspan(ht)[2,1] <- 2
caption(ht) <- 'multirow cells and multicol cells'
ht

```

\FloatBarrier


# Borders through merged cells


```{r}
ht <- make()
ht <- set_all_borders(ht, 0)
rowspan(ht)[1, 1] <- 2
bottom_border(ht)[1, 1] <- 1
caption(ht) <- "border at bottom of row 1 col 1"
ht

bottom_border(ht)[1, ] <- 1
caption(ht) <- "border at bottom of all of row 1"
ht
```

```{r}
ht <- make()
ht <- set_all_borders(ht, 0)
colspan(ht)[1, 1] <- 2
right_border(ht)[1, 1] <- 1
caption(ht) <- "border at right of row 1 col 1"
ht

right_border(ht)[, 1] <- 1
caption(ht) <- "border at right of all of column 1"
ht
```


# Multirow/multicol and borders

```{r}
ht <- make(nrow = 3, ncol = 3)
ht <- set_all_borders(ht, 0)
ht <- set_all_borders(ht, 1, 1, 1)
rowspan(ht)[1, 1] <- 2
caption(ht) <- "Col 1 rows 1-2 merged, row 1 col 1 has all borders"
ht

rowspan(ht)[1, 1] <- 1
colspan(ht)[1, 1] <- 2
caption(ht) <- "Row 1 cols 1-2 merged, row 1 col 1 has all borders"
ht

rowspan(ht)[1, 1] <- 2
colspan(ht)[1, 1] <- 2
caption(ht) <- "Rows 1-2 cols 1-2 merged, row 1 col 1 has all borders"
ht

rowspan(ht)[1, 1] <- 1
colspan(ht)[1, 1] <- 1
rowspan(ht)[2, 2] <- 2
colspan(ht)[2, 2] <- 2
ht <- set_all_borders(ht, 0)
ht <- set_all_borders(ht, 2, 2, 1)
caption(ht) <- "Rows 2-3 cols 2-3 merged, row 2 col 2 has all borders"
ht

```

# Border color left and right/top and bottom

```{r}
ht <- make()
left_border_color(ht)[,2] <- 'red'
caption(ht) <- 'Left border of col 2: red'
ht

ht <- make()
top_border_color(ht)[2,] <- 'red'
caption(ht) <- 'Top border of row 2: red'
ht
```

\FloatBarrier

# Text and border color

```{r}
ht <- make()
ht <- set_all_border_colors(ht, everywhere, 1, 'red')
text_color(ht) <- 'blue'
caption(ht) <- 'text color blue. Col 1 border color set to red'
ht
```

\FloatBarrier

# Multirow, multicol and border color

```{r}
ht <- make(nrow = 2)
ht <- set_all_border_colors(ht, 'blue')
caption(ht) <- 'All borders blue'
ht
```

```{r}
ht <- make(nrow = 5)
background_color(ht) <- 'white'
colspan(ht)[1,1] <- 2
rowspan(ht)[2,1] <-2
colspan(ht)[4,1] <- 2
rowspan(ht)[4,1] <- 2
left_border_color(ht) <- 'yellow'
right_border_color(ht) <- 'green'
top_border_color(ht) <- 'blue'
# bottom_border_color(ht) <- 'red'
caption(ht) <- 'multirow, multicol and border color (left: yellow; right: green; top: blue; bottom unset)'
ht

```


\FloatBarrier

# Borders on and off


```{r}
ht <- make(nrow = 2)
ht <- set_all_borders(ht, 1)
top_border(ht)[1, ] <- 0
caption(ht) <- 'No top border'
ht

left_border(ht)[, 1] <- 0
caption(ht) <- 'No left/top border'
ht

right_border(ht)[, 1] <- 0
caption(ht) <- 'No left/right border of left col, no top border of top row'
ht
```


# Horizontal border width

```{r}

ht <- make(nrow = 2)
ht <- set_all_borders(ht, .5)
caption(ht) <- 'Border .5 px'
ht

ht <- make(nrow = 2)
bottom_border(ht)[1, ] <- 2
top_border(ht)[2, ] <- 4 # should win out
caption(ht) <- 'Bottom border of first row 4px'
ht

top_border(ht)[2, 2] <- 1 
caption(ht) <- 'Bottom border of first row first column 4pt; second column 1pt. Gives warning'
ht

bottom_border(ht)[1, 2] <- 0
top_border(ht)[2, 2] <- 0
caption(ht) <- 'Bottom border of first row first column 4pt; second column 0pt. Should work.'
ht

```


# Vertical border width

```{r}
ht <- make(nrow = 2)
left_border(ht)[2, 1] <- 6
left_border(ht)[2, 2] <- 1
right_border(ht)[2, 1] <- 4
right_border_color(ht)[2, 1] <- 'red'
caption(ht) <- 'Left borders second col should be thicker. At present right\\_border has priority'
ht
```

# Border width and multirow/col

```{r}
ht <- make(nrow = 3)
rowspan(ht)[1, 1] <- 2
colspan(ht)[3, 1] <- 2
right_border(ht)[1, ] <- 4
bottom_border(ht)[1, ] <- 4
caption(ht) <- 'Bottom border of top cell should be thicker. In TeX this will continue across row.'
ht
```


# Border style

```{r}
ht <- make(nrow = 5, ncol = 5, more_text = c('more', 'more', 'more'))
ht <- set_all_borders(ht, 3)
left_border_style(ht)[, 1] <- 'double'
right_border_style(ht)[, 1] <- 'dotted'
left_border_style(ht)[, 2] <- 'dotted'
right_border_style(ht)[, 2] <- 'dashed'
left_border_style(ht)[, 3] <- 'dashed'
left_border(ht)[, 4] <- 0
right_border(ht)[, 3] <- 0
left_border_style(ht)[, 5] <- 'double'
top_border_style(ht)[1, ] <- 'double'
bottom_border_style(ht)[1, ] <- 'dotted'
top_border_style(ht)[2, ] <- 'dotted'
bottom_border_style(ht)[2, ] <- 'dashed'
top_border_style(ht)[3, ] <- 'dashed'
top_border(ht)[4, ] <- 0
bottom_border(ht)[3, ] <- 0
top_border_style(ht)[5, ] <- 'double'
caption(ht) <- 'Border styles: double, dotted, dashed, none, double, single'
ht

left_border_color(ht) <- 'blue'
caption(ht) <- 'Same with blue vert borders'
ht

top_border_color(ht) <- 'red'
left_border_color(ht) <- NA
caption(ht) <- 'Same with red horiz borders'
ht

left_border_color(ht) <- 'blue'
caption(ht) <- 'Blue and red borders'
ht

left_border_color(ht) <- NA
top_border_color(ht) <- NA


ht2 <- ht
bottom_border(ht2)[2, 3] <- 0
top_border(ht2)[3, 3]    <- 0
bottom_border(ht2)[1, 4] <- 0
top_border(ht2)[2, 4]    <- 0

left_border(ht2) <- 1
right_border(ht2) <- 1
left_border(ht2)[matrix(rep(1:nrow(ht2), each = 2, byrow = TRUE))] <- 0
right_border(ht2)[matrix(rep(1:nrow(ht2), each = 2, byrow = TRUE))] <- 0

caption(ht2) <- 'Varying borders within rows/cols'
ht2

ht <- set_left_border(ht, 3:7)
ht <- map_bottom_border(ht, by_cols(3:7))
caption(ht) <- 'Varying border widths'
ht
```


# Huxreg 

```{r}
huxreg(lm(iris$Sepal.Length~iris$Sepal.Width))

```

# hux_logo

```{r}
hux_logo(latex = knitr::is_latex_output(), html = knitr::is_html_output())
```
