Good times for table makers

The gt package is out on CRAN. If you want to produce presentation tables in R, you should definitely check it out – it’s written by the guys at RStudio and has the high quality you would expect.

Meanwhile, the huxtable package still might be useful. In particular, if you want to output to PDF (or HTML, Word, Powerpoint, Excel, RTF or Markdown), huxtable can do that.

Version 5.0.0 of huxtable is coming out shortly. This article describes some of its new features. Click the links on the left to learn more.

         
h   u  
    x   t  
  a       b
  l    
          e

Markdown in cells

You can now use markdown to format text in cell contents.

Type of jam

Price of jam

Strawberry 1.90     
Raspberry

2.10 Sale! 1.50

Plum 1.80     

The set_markdown_contents() is a shortcut function that sets the markdown property to TRUE.

For now, markdown is supported in HTML and LaTeX, with basic support for on-screen output.

Improved LaTeX output

Many tweaks have been made to how huxtables are displayed in LaTeX. In particular, the “adjustbox” TeX package is now used to centre tables, meaning that – at last – tables now stay robustly in the middle of the page.

Old version

Old version

New version

New version

This change needs a recent version of the “adjustbox” package from CTAN. huxtable::check_latex_dependencies() will check this for you, and install_latex_dependencies() will try to update you automatically.

There are other improvements too. Padding now works better, and cell wrapping is more sensible by default.

Better borders

To make a coloured or styled border in huxtable, you used to have to write:

Type Price
Strawberry 1.90
Raspberry 2.10
Plum 1.80

This could get laborious. Huxtable 5.0.0 has a new brdr() class to set all these features at once.

Type Price
Strawberry 1.90
Raspberry 2.10
Plum 1.80

Border internals have also been reworked, fixing some long-standing bugs. The following code used to give a nasty surprise:

 Wait, why has my border moved?  
  Multirow cell   Cell    Cell   
                ─────────────────
                  Cell    Cell   
─────────────────                
  Cell            Cell    Cell   

Column names: V1, V2, V3

It now does what you would expect:

                                    That's better                                    
                            Multirow cell   Cell    Cell   
                                          ─────────────────
                                            Cell    Cell   
                            Cell            Cell    Cell   

Column names: V1, V2, V3

Terminal printing now also respects the huxtable position, incidentally.

Lastly, there are new functions to quickly set vertical and horizontal borders:

Type Price
Strawberry 1.90
Raspberry 2.10
Plum 1.80

Splitting tables

Here’s a common problem: your table is too wide or long for the page.

Model Miles per gallon Cylinders Displacement Horsepower Drat, my table is too wide
Mazda RX4 21   6 160 110 3.9
Mazda RX4 Wag 21   6 160 110 3.9
Datsun 710 22.8 4 108 93 3.85
Hornet 4 Drive 21.4 6 258 110 3.08
Hornet Sportabout 18.7 8 360 175 3.15
Valiant 18.1 6 225 105 2.76

Huxtable now has some nice ways to deal with this. First, you can split your table in two:

Model Miles per gallon Cylinders
Mazda RX4 21   6
Mazda RX4 Wag 21   6
Datsun 710 22.8 4
Hornet 4 Drive 21.4 6
Hornet Sportabout 18.7 8
Valiant 18.1 6
Model Displacement Horsepower Drat, my table is too wide
Mazda RX4 160 110 3.9
Mazda RX4 Wag 160 110 3.9
Datsun 710 108 93 3.85
Hornet 4 Drive 258 110 3.08
Hornet Sportabout 360 175 3.15
Valiant 225 105 2.76

Notice that both of the split tables still have the model names on the left. That’s because we set them as a header column, using some more new functionality. Header columns don’t change style by themselves, but you can apply your own styles easily.

Restacking tables

Another way to change the table size is to restack it. Here, we restack our table of cars to go down the page.

Model Miles per gallon Cylinders Displacement
Mazda RX4 21   6 160
Mazda RX4 Wag 21   6 160
Datsun 710 22.8 4 108
Hornet 4 Drive 21.4 6 258
Hornet Sportabout 18.7 8 360
Valiant 18.1 6 225
Model Horsepower Drat, my table is too wide
Mazda RX4 110   3.9
Mazda RX4 Wag 110   3.9
Datsun 710 93   3.85
Hornet 4 Drive 110   3.08
Hornet Sportabout 175   3.15
Valiant 105   2.76

style_header_rows() can be used to give header rows their own look. Notice that header rows don’t have to be on top of the table.

As well as split_down() and restack_down(), there’s split_across() and restack_across(), for when your tables are too long. To explain how restacking works, a picture will help.

Original table
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Restacked across
1 2 3 4 9 10 11 12
5 6 7 8 13 14 15 16
Restacked down
1 2
5 6
9 10
13 14
3 4
7 8
11 12
15 16

By default, huxtable will warn you if your restacked table doesn’t fit evenly.

Automatic labels

Huxtable now sets labels from the knitr chunk header by default.

This makes referencing from bookdown easy:

Some iris species are shown in \@ref(tab:mytable):

```{r mytable}
hux(head(iris))
```

Intuitive defaults

Huxtable 5.0.0 sets sensible defaults for some arguments. So, instead of writing

you can just write

(Why else would you use set_bold() in the first place?)

Type Price
Strawberry 1.90
Raspberry 2.10
Plum 1.80

Easy number formatting

Huxtable’s number formatting is powerful, but hasn’t been easy to use. fmt_pretty() and fmt_percent() help you print nice looking numbers.

Type Price Sugar content Monthly sales
Strawberry 1.90 40.0% 35,000
Raspberry 2.10 35.0% 55,500
Plum 1.80 45.0% 2,000

Try it out

To try out the new features in 5.0.0, run:

If you find a bug, report it on github.

You can go back to the CRAN version with:

Happy table making!