Creating tables in huxtable

Huxtable is an R library to create tables that can be output to LaTeX, HTML, and Word documents.

These notes are run using R3.6 and pandoc 2.8.0.1.

Requirements

  • To use huxtable requires pandoc version >2.0. If you can’t update the system pandoc you can set the PATH variable to point to a local version when running R.

  • To output to word document huxtable also requires flextable

We can install the R packages like so (if they aren’t already installed)

install.packages("flextable")
install.packages("huxtable")

Caveats

The huxtable package doesn’t support captions for word documents.

Example use

In this example we are going to use the following toy data frame.

example.df <- data.frame(Col1=c('Foo', 'Bar', 'Fizz', 'Buzz'),
                         Col2=c(1, 2, 3, 4),
                         Col3=c(1.12345, 1.54321, 4.12345, 4.54321))
colnames(example.df) <- c("Col 1", "Col 2", "Col 3")

The first thing we need to do is turn the dataframe example.df into a huxtable.

Default output

library(huxtable)

example.huxtable <- as_hux(example.df)

The huxtable example.huxtable by default looks like this.

Foo 1 1.12
Bar 2 1.54
Fizz 3 4.12
Buzz 4 4.54

Adding column names to the table

We can add formatting to this table. Here we use the pipe interface to huxtable. Using the pipe interface requires the magrittr library.

The first thing we want to do is add the column names to the outputted table. To do this we pipe example.huxtable through add_colnames.

library(magrittr)

example.huxtable %>% 
add_colnames
Col 1 Col 2 Col 3
Foo 1 1.12
Bar 2 1.54
Fizz 3 4.12
Buzz 4 4.54

Bolding cells

Next we add bolding to the 1st row by piping to set_bold. The first argument of the pipe interface is the row number to apply bold to. The second argument is the column number where everywhere means every column. The third argument is a boolean value for whether the cell is bold (TRUE) or not (FALSE).

library(magrittr)

example.huxtable %>% 
add_colnames %>%
set_bold(1, everywhere, TRUE)
Col 1 Col 2 Col 3
Foo 1 1.12
Bar 2 1.54
Fizz 3 4.12
Buzz 4 4.54

Cell borders

To add a bottom border to the first column we use set_bottom_border providing the row, columns, and line width of the border. Similarly we use set_top_border.

library(magrittr)

example.huxtable %>% 
add_colnames %>%
set_bold(1, everywhere, TRUE) %>%
set_bottom_border(1, everywhere, 2) %>%
set_top_border(1, everywhere, 2)
Col 1 Col 2 Col 3
Foo 1 1.12
Bar 2 1.54
Fizz 3 4.12
Buzz 4 4.54

Adding rows and merging cells

We can add rows using add_rows and then we can merge cells in a row using merge_cells.

library(magrittr)

hdr1 <- hux("Sub-header for FooBar", "", "")
hdr2 <- hux("Sub-header for FizzBuzz", "", "")

example.huxtable %>% 
add_colnames %>%
set_bold(1, everywhere, TRUE) %>%
set_bottom_border(1, everywhere, 2) %>%
set_top_border(1, everywhere, 2) %>%
add_rows(hdr1, 1) %>%  # the row is added **after** the specified row (new row is row 2)
merge_cells(2, c(1, 3)) %>%
add_rows(hdr2, 4) %>%  # at this point "Bar" is in row 4
merge_cells(5, c(1, 3))
Col 1 Col 2 Col 3
Sub-header for FooBar
Foo 1 1.12
Bar 2 1.54
Sub-header for FizzBuzz
Fizz 3 4.12
Buzz 4 4.54

Adding footnotes

To add footnotes to a huxtable can be done by piping to add_footnote.

library(magrittr)

hdr1 <- hux("Sub-header for FooBar", "", "")
hdr2 <- hux("Sub-header for FizzBuzz", "", "")

example.huxtable %>% 
add_colnames %>%
set_bold(1, everywhere, TRUE) %>%
set_bottom_border(1, everywhere, 2) %>%
set_top_border(1, everywhere, 2) %>%
add_rows(hdr1, 1) %>%  # the row is added **after** the specified row (new row is row 2)
merge_cells(2, c(1, 3)) %>%
add_rows(hdr2, 4) %>%  # at this point "Bar" is in row 4
merge_cells(5, c(1, 3)) %>%
add_footnote("This is the footnote text for the table")
Col 1 Col 2 Col 3
Sub-header for FooBar
Foo 1 1.12
Bar 2 1.54
Sub-header for FizzBuzz
Fizz 3 4.12
Buzz 4 4.54
This is the footnote text for the table

Adding a caption

A caption can be added using set_caption and the position of the caption can be set using set_caption_pos. However this will only work when exporting to LaTeX, HTML, and PDF.

library(magrittr)

hdr1 <- hux("Sub-header for FooBar", "", "")
hdr2 <- hux("Sub-header for FizzBuzz", "", "")

example.huxtable %>% 
add_colnames %>%
set_bold(1, everywhere, TRUE) %>%
set_bottom_border(1, everywhere, 2) %>%
set_top_border(1, everywhere, 2) %>%
add_rows(hdr1, 1) %>%  # the row is added **after** the specified row (new row is row 2)
merge_cells(2, c(1, 3)) %>%
add_rows(hdr2, 4) %>%  # at this point "Bar" is in row 4
merge_cells(5, c(1, 3)) %>%
add_footnote("This is the footnote text for the table") %>%
set_caption("Caption text for table") %>%
set_caption_pos("bottom")
Caption text for table
Col 1 Col 2 Col 3
Sub-header for FooBar
Foo 1 1.12
Bar 2 1.54
Sub-header for FizzBuzz
Fizz 3 4.12
Buzz 4 4.54
This is the footnote text for the table

Setting the column widths

We can set the column widths using set_col_width.

library(magrittr)

hdr1 <- hux("Sub-header for FooBar", "", "")
hdr2 <- hux("Sub-header for FizzBuzz", "", "")

example.huxtable %>% 
add_colnames %>%
set_bold(1, everywhere, TRUE) %>%
set_bottom_border(1, everywhere, 2) %>%
set_top_border(1, everywhere, 2) %>%
add_rows(hdr1, 1) %>%  # the row is added **after** the specified row (new row is row 2)
merge_cells(2, c(1, 3)) %>%
add_rows(hdr2, 4) %>%  # at this point "Bar" is in row 4
merge_cells(5, c(1, 3)) %>%
add_footnote("This is the footnote text for the table") %>%
set_caption("Caption text for table") %>%
set_caption_pos("bottom") %>%
set_col_width(c(2.0, 3.0, 1.5))
Caption text for table
Col 1 Col 2 Col 3
Sub-header for FooBar
Foo 1 1.12
Bar 2 1.54
Sub-header for FizzBuzz
Fizz 3 4.12
Buzz 4 4.54
This is the footnote text for the table

Changing a value of a cell

We can set the text value of a specific cells using set_contents.

library(magrittr)

hdr1 <- hux("Sub-header for FooBar", "", "")
hdr2 <- hux("Sub-header for FizzBuzz", "", "")

example.huxtable %>% 
add_colnames %>%
set_bold(1, everywhere, TRUE) %>%
set_bottom_border(1, everywhere, 2) %>%
set_top_border(1, everywhere, 2) %>%
add_rows(hdr1, 1) %>%  # the row is added **after** the specified row (new row is row 2)
merge_cells(2, c(1, 3)) %>%
add_rows(hdr2, 4) %>%  # at this point "Bar" is in row 4
merge_cells(5, c(1, 3)) %>%
add_footnote("This is the footnote text for the table") %>%
set_caption("Caption text for table") %>%
set_caption_pos("bottom") %>%
set_col_width(c(2.0, 3.0, 1.5)) %>%
set_contents(1, 1, "New Col 1")
Caption text for table
New Col 1 Col 2 Col 3
Sub-header for FooBar
Foo 1 1.12
Bar 2 1.54
Sub-header for FizzBuzz
Fizz 3 4.12
Buzz 4 4.54
This is the footnote text for the table