h | u | X |
|
||||
|
t | a | b | l | e |
Huxtable is an R package to create LaTeX and HTML tables, with a friendly, modern 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.
Quick Examples
Simple table
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,] <- 0.4
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 |
Pipe interface
library(magrittr)
ht <- hux(
Employee = c("John Smith", "Jane Doe", "David Hugh-Jones"),
Salary = c(50000, 50000, 40000)
)
ht |>
set_bold(1, everywhere) |>
set_bottom_border(1, everywhere) |>
set_align(everywhere, 2, "right") |>
set_lr_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 |
Conditional formatting
mtcars[1:5, 1:5] |>
as_huxtable(add_rownames = "Model") |>
set_bold(1, everywhere, TRUE) |>
set_all_borders(1) |>
map_text_color(everywhere, "mpg", by_colorspace("navy", "red", "yellow")) |>
map_background_color(everywhere, "hp", by_quantiles(0.8, c("white", "yellow"))) |>
map_italic(everywhere, "Model", by_regex("Merc.*" = TRUE))
Model | mpg | cyl | disp | hp | drat |
---|---|---|---|---|---|
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 |
Table of regressions
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, statistics = c("N" = "nobs", "R2" = "r.squared"))
(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 |
R2 | 0.847 | 0.000 | 0.847 |
*** p < 0.001; ** p < 0.01; * p < 0.05. |
Installation
From R-Universe:
install.packages("huxtable", repos = c(
"https://hughjonesd.r-universe.dev",
"https://cloud.r-project.org"
))
From CRAN:
Development version from github:
Learning more
Check out the vignette.
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 randomly generated by huxtable, using hux_logo()
. The web page background uses Piet Mondrian’s Composition A.