h u x
table

 

Introduction Design Documentation Github Bugs

Huxtable is an R package for creating HTML and LaTeX tables. It provides similar functionality to xtable, but more, with a simpler interface. Features include control over text styling, number format, background color, borders, padding and alignment. Cells can span multiple rows and/or columns. Tables can be manipulated with standard R subsetting or dplyr functions. Here are some quick examples:

ht <- hux(
        Employee = c('John Smith', 'Jane Doe', 'David Hugh-Jones'),
        Salary = c(50000, 50000, 40000),
        add_colnames = TRUE
      )

bold(ht)[1,]           <- TRUE
bottom_border(ht)[1,]  <- 1
align(ht)[,2]          <- 'right'
right_padding(ht)      <- 10
left_padding(ht)       <- 10
width(ht)              <- 0.35
number_format(ht)      <- 2

ht
Employee Salary
John Smith 50000.00
Jane Doe 50000.00
David Hugh-Jones 40000.00
library(magrittr)
ht <- hux(
        Employee = c('John Smith', 'Jane Doe', 'David Hugh-Jones'),
        Salary = c(50000, 50000, 40000),
        add_colnames = TRUE
      )

ht                                        %>%
      set_bold(1, everywhere, TRUE)       %>%
      set_bottom_border(1, everywhere, 1) %>%
      set_align(everywhere, 2, 'right')   %>%
      set_right_padding(10)               %>%
      set_left_padding(10)                %>%
      set_width(0.35)                     %>%
      set_number_format(2)
Employee Salary
John Smith 50000.00
Jane Doe 50000.00
David Hugh-Jones 40000.00
library(psych)
data(attitude)
att_corr <- corr.test(as.matrix(attitude))

as_hux(att_corr$r)                                                             %>%
      set_background_color(where(att_corr$p < 0.05), 'yellow')                 %>%
      set_background_color(where(att_corr$p < 0.01), 'orange')                 %>%
      set_background_color(where(row(att_corr$p) == col(att_corr$p)), 'white') %>%
      huxtable::add_rownames(colname = '')                                     %>%
      huxtable::add_colnames()                                                 %>%
      set_all_borders(1)                                                       %>%
      set_number_format(2)
rating complaints privileges learning raises critical advance
rating 1.00 0.83 0.43 0.62 0.59 0.16 0.16
complaints 0.83 1.00 0.56 0.60 0.67 0.19 0.22
privileges 0.43 0.56 1.00 0.49 0.45 0.15 0.34
learning 0.62 0.60 0.49 1.00 0.64 0.12 0.53
raises 0.59 0.67 0.45 0.64 1.00 0.38 0.57
critical 0.16 0.19 0.15 0.12 0.38 1.00 0.28
advance 0.16 0.22 0.34 0.53 0.57 0.28 1.00
data(diamonds, package = 'ggplot2')

lm1 <- lm(log(price) ~ carat, diamonds)
lm2 <- lm(log(price) ~ depth, diamonds)
lm3 <- lm(log(price) ~ carat + depth, diamonds)

huxreg(lm1, lm2, lm3)
( 1) ( 2) ( 3)
(Intercept) 6.215 *** 7.749 *** 7.313 ***
(0.003)    (0.188)    (0.074)   
carat 1.970 ***          1.971 ***
(0.004)             (0.004)   
depth          0.001    -0.018 ***
         (0.003)    (0.001)   
N 53940         53940         53940        
R 2 0.847     7.398e-07 0.847    
logLik -26728.969     -77321.478     -26617.649    
AIC 53463.939     154648.956     53243.298    
*** p < 0.001; ** p < 0.01; * p < 0.05.
quick_pdf(mtcars)
quick_docx(mtcars)
quick_pdf(mtcars)
# Files are produced in the current working directory.

To learn more, check out the vignette in HTML or PDF format, or the original R Markdown. Or, read the design principles behind huxtable, including a comparison with other R packages to create tables.

News

January 3, 2018: Huxtable 2.0.0 released

The major version bump marks a change in how numbers are shown. Numeric formatting via number_format now applies to any number found in a cell, even if there are non-numeric characters. This means that e.g. cells like "2.71 (1.4) **" will have both numbers formatted.

There are also new quick_pdf, quick_html and quick_docx functions, for quick output of data frames or similar objects in different formats.

October 2017: Huxtable 1.0.0 released!

New features include:

On-screen color
Going back to the 80s with on-screen color

Installation

From CRAN:

install.packages('huxtable')

Development version from github:

library(devtools)
install_github('hughjonesd/huxtable')

The name

Huxtable could stand for "Happy User Xtable", or if you prefer, "Hyped Up Xtable", or for Germans maybe "Html Und teX Table".

Trivia

The logo above was also generated by a huxtable, using hux_logo(). The web page background is a statistical table from Mayhew's London Labour and the London Poor, a classic of Victorian social science.