an R package for writing LaTeX and HTML tables (2024)

Table of Contents
Installation The name Trivia News
h
uxta
b
l
e
IntroductionDesignDocumentationGithubBugs

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, paddingand alignment. Cells can span multiple rows and/or columns.Tables can be manipulated with standard R subsetting ordplyr functions. Here are some quick examples:

  • Simple table
  • Pipe interface
  • Conditional formatting
  • Table of regressions
  • Quick document
ht <- hux( Employee = c("John Smith", "Jane Doe", "David Hugh-Jones"), Salary = c(50000, 50000, 40000), add_colnames = TRUE )bold(ht)[1,] <- TRUEbottom_border(ht)[1,] <- 0.4align(ht)[,2] <- "right"right_padding(ht) <- 10left_padding(ht) <- 10width(ht) <- 0.35number_format(ht) <- 2ht
EmployeeSalary
John Smith50000.00
Jane Doe50000.00
David Hugh-Jones40000.00
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)
EmployeeSalary
John Smith50000.00
Jane Doe50000.00
David Hugh-Jones40000.00
mtcars[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)) |> head(12)
Modelmpgcyldisphpdrat
Mazda RX42161601103.9
Mazda RX4 Wag2161601103.9
Datsun 71022.84108933.85
Hornet 4 Drive21.462581103.08
Hornet Sportabout18.783601753.15
Valiant18.162251052.76
Duster 36014.383602453.21
Merc 240D24.44147623.69
Merc 23022.84141953.92
Merc 28019.261681233.92
Merc 280C17.861681233.92
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)
carat1.970 ***1.971 ***
(0.004)(0.004)
depth0.001-0.018 ***
(0.003)(0.001)
N539405394053940
R20.8470.0000.847
*** p < 0.001; ** p < 0.01; * p < 0.05.
quick_pdf(mtcars)quick_docx(mtcars)quick_html(mtcars)quick_xlsx(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 comparisonwith other R packages to create tables.

Installation

From R-Universe:

install.packages("huxtable", repos = c("https://hughjonesd.r-universe.dev", "https://cloud.r-project.org"))

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 randomly generated by huxtable, using hux_logo().The web page background is Piet Mondrian'sComposition A.

News

February 8, 2024: Huxtable 5.5.5 released

This contains bugfixes for quarto 1.4. Unfortunately, usingquarto references is no longer possible with TeX output and quarto 1.4.Huxtable 5.5.4 was not released to CRAN due to a reverse dependencyissue.

December 9, 2023: Huxtable 5.5.3 released

This contains bugfixes for quarto and Word document output.

December 16, 2022: Huxtable 5.5.2 released

This contains one small change for compatibility with dplyr 1.1.0:in by_cases(), when dplyr version 1.1.0 or above is detected,the . variable is now shaped like a vector not a matrix.

November 12, 2022: Huxtable 5.5.1 released

This is a bugfix release.

June 15, 2022: Huxtable 5.5.0 released

This release makes sure huxtable plays nicely with quarto documents. It adds some new convenience functions:

  • stripe_rows() and stripe_columns() adda background color stripe to your table.
  • column_to_header() converts one column to a set of headers.

May 15, 2021: Huxtable 5.4.0 released

This release fixes some bugs. It also contains one change thatmay affect behaviour: when you merge cells (e.g. with merge_cells()or by setting colspan or rowspan), cell contentsare copied from the top left cell to all the other "shadowed" cells. Thismeans that if you later subset or reorder the huxtable, the contents ofthe merged cells won't change unexpectedly. Lastly, there is a newoption "huxtable.latex_siunitx_align", which can be used toalign numbers on the decimal point using the TeX"siunitx" package.

May 1, 2021: Huxtable 5.3.0 released

This release improves how cell alignment is handled in LaTeX whenalign(ht) == ".". Regression tables and other numeric datashould now look much better!

February 15, 2021: Huxtable 5.2.0 released

This release fixes a bug in a test, and adds one new property,table_environment, which sets the table environment touse in LaTeX.

October 28, 2020: Huxtable 5.1.1 released

This release contains some small bugfixes for new versions of R.

September 18, 2020: RStudio table contest

RStudio is running acompetitionto produce nice tables from R. I will bejudging some of the entries. Download huxtable 5.1.0 (just released) and producesomething amazing!

September 18, 2020: Huxtable 5.1.0 released

This release contains several bugfixes, and some backwards-compatiblenew features:

  • as_flextable() now exports markdown in cells to RTF, and to Word with the help of the optional "ftExtra" package.
  • options(huxtable.long_minus = TRUE) will try to use long minus signs before negative numbers. The default is FALSE, for now.
  • as_Workbook() gains start_row and start_col arguments, to write a huxtable into an Excel worksheet starting at a particular row or column.

June 16, 2020: Huxtable 5.0.0 released

This release contains many changes and improvements. It also breaksbackwards compatibility.

  • Better LaTeX output.
  • Support for markdown in cells.
  • Easier borders.
  • New ways to split and reshape huxtables.
  • For full details, see the NEWSfile.
  • For a friendly introduction, seethis article.

January 9, 2020: Huxtable 4.7.1 released

This release added caption support for as_flextable().A new colwise argument for by_quantiles(),by_equal_groups() and by_colorspace() allowscalculating quantiles or colors separately foreach column.

In other news, the expss package now supports export tohuxtables.

October 3, 2019: Huxtable 4.7.0 released

This release improves error messages and adds some new functionality:

  • merge_repeated_rows() merges repeated rows in a huxtableto create a single cell. This is useful for dealing with e.g. groupeddata frames.
  • insert_row() and insert_column() gain twonew arguments, fill and colspan/rowspan. Thesecan be used to quickly create a header cell:
insert_row(ht, "Header Text", fill = "", colspan = ncol(ht))

August 6, 2019: Huxtable 4.6.1 released

This release fixes two bugs, including one where cell borders aroundmerged cells were misplaced in LaTeX.

June 12, 2019: Huxtable 4.6.0 released

This release fixes a few bugs, and adds one new feature: the abilityto use portable font names across HTML and PDF, using the LaTeXfontspec package. To switch this on, setoptions(huxtable.latex_use_fontspec = TRUE).

March 19, 2019: Huxtable 4.5.0 released

This release fixes some CRAN bugs, and adds one new feature: aquick_latex() function to export data to a .tex file.

March 4, 2019: Huxtable 4.4.0 released

This release contains some incremental improvements:

  • Huxtables can now be printed directly in Word documents and Powerpoint presentations, thanks to the flextable package and recent versions of Pandoc. (Powerpoint printing requires Pandoc >= 2.4.0.)
  • New "wrapleft" and "wrapright" options to position() allow text wrapping around tables.
  • When possible, huxtable uses tinytex when available, allowing automatic installation of required latex packages.
  • New set_outer_border_colors() and set_outer_border_styles() functions, like set_outer_borders().

November 7, 2018: Huxtable 4.3.0 released

This release contains several improvements:

  • New map_xxx functions to map values to properties.
  • New grey, blue, green and orange themes, and a new "themes" vignette.
  • Shorter, clearer vignettes and examples.
  • More work ensuring that TeX compiles OK.

Here's an example of using map_ functions to stripe a table, then pick out extremevalues:

data <- matrix(rnorm(25), 5, 5)as_hux(data) |> set_outer_borders(0.4) |> map_background_color(by_rows("grey95", "white")) |> map_text_color(by_quantiles(c(0.1, 0.9), c("red", "black", "green3")))
1.86-1.87-1.320.9441.96
0.386-0.921-0.239-0.64-0.953
-1.471.2-1.14-2.18-1.87
0.7180.267-0.784-0.217-0.968
0.132-1.8-0.8280.580.792

October 26, 2018: Huxtable 4.2.1 released

This patch release works around a bug with recent pandoc versions, which led to huxtable's TeX commands being escaped in some rmarkdown documents.

To apply the workaround, you will need to add:

md_extensions: +raw_attribute

to your YAML header.

If you still have problems, please reporta bug.

October 3, 2018: Huxtable 4.2.0 released

This minor point release adds a few features and bugfixes.

  • LaTeX rendering should again be 2-3 times faster, as_Workbook should be faster too.
  • Simplify LaTeX output by creating and using our own LaTeX commands. Please report any bugs!
  • RTF support: new print_rtf, to_rtf and quick_rtf functions
  • New border_style properties to set "solid", "double", "dotted" or "dashed" borders. (At present, LaTeX only allows "solid" or "double".)
  • New quick_pptxfunction to print data frames and huxtables into Powerpoint.
  • There's a new theme_mondrian theme :-D

August 1, 2018: Huxtable 4.1.0 released

This minor point release focuses on bugfixes, speed and maintainability.

  • HTML rendering should be up to 10 times faster, LaTeX should be up to 4 times faster.
  • Many packages have been moved to "Suggests:". You can now use huxtable without knitr,rmarkdown or dplyr. All the functionality is still there - if a package is needed, huxtableinforms you with a warning message.
  • There are new add_rows() and add_columns() functions to easily insertmultiple rows and columns into a huxtable; and a new tribble_hux(), function based ontibble::tribble(), for readable hand-written data.
  • Huxtable has a new, randomly generated logo :-)

June 2, 2018: Huxtable 4.0.0 released

This release brings some changes. The most important are:

  • Huxtable automatically pretty-prints data frames within knitr documents.
  • Column alignment and number format is assigned automatically for different data types.

These are breaking changes, so you can set options to go back to the old behaviour.

One other change is being introduced gradually: add_colnames will be set toTRUE by default in a future release. To future-proof your code, you can enable thisbehaviour by setting options(huxtable.add_colnames = TRUE).

Other changes include a new theme_plain theme; automatically opening documents created by quick_pdf andfriends; and several bugfixes. Pleasereport any bugs in the usual place.

February 23, 2018: Huxtable 3.0.0 released

The major version bump marks a (small) change in the quick_xxx functionAPI: if called without an explicit file argument, these functions willfail when called non-interactively, and will prompt before overwriting files if calledinteractively. This prevents nasty surprises.

Excel output is now supported using the openxlsx package. Those working in a corporate environment can now create formatted spreadsheets tomake the pointy-haired boss happy.

You can now select columns using dplyr-style select helpers like matches() andstarts_with() in set_* functions.

an R package for writing LaTeX and HTML tables (1)

January 3, 2018: Huxtable 2.0.0 released

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

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

October 2017: Huxtable 1.0.0 released!

New features include:

  • RJava-free export to Word and Powerpoint with officer
  • Variable border widths in LaTeX
  • On-screen color - works in R terminal or latest RStudio daily builds
an R package for writing LaTeX and HTML tables (2)
an R package for writing LaTeX and HTML tables (2024)
Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 5902

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.