knitr::opts_chunk$set(echo = TRUE)

This package produces highly-customized LaTeX tables in R. There are two key functions:

It supports building a table in blocks, in the spirit of ggplot2, using the + and %:% operators for concatenation.

To install, run:

devtools::install_github("setzler/textables")
library(textables)

Here is an example of the type of table that this package can easily construct:

Example Table

1. Character Rows with TR

Purpose

When applied to a character vector, TR produces the row of a LaTeX tabular.

TR supports LaTeX math mode, fonts, etc., but requires a double-escape (two back slashes instead of the usual one in LaTeX).

Example

vec <- c('Hello','\\textbf{World}','$\\alpha$','$\\frac{1}{2}$')

# produce a LaTeX tabular row
TR(vec)

2. Numeric Rows with TR

Purpose

When applied to a numeric vector, TR produces the row of a LaTeX tabular. Numeric formatting options include:

We also provide the surround argument for a general syntax to modify values. We could use it to change the font of each number in a row, as shown in the example below.

Note that dec can be combined with any other options.

Examples:

Decimal places: dec

vec <- c(1.0, 1.01, 1.001)

# round to the 2nd decimal place
TR(vec, dec=2)

# different rounding for each value
TR(vec, dec=c(3,2,1))

Standard errors: se

# treat all values as standard errors
TR(vec, se=T)

# treat some values as standard errors
TR(vec, dec=c(3,2,1), se=c(T,T,F))

Percent symbols: percentage

# treat all values as percentages
TR(vec, percentage=T)

# treat some values as percentages
TR(vec, dec=c(3,2,1), percentage=c(T,T,F))

Stars for statistical significance: pvalues

# add stars by providing p-values
TR(vec, dec=c(3,2,1), pvalues=c(.01,.05,.1))

General modification of each value: surround

# turn all numbers red
TR(vec, surround = "{\\color{red} %s}")

Column span: cspan

# span two columns with the middle number
TR(vec, dec=c(3,2,1), cspan=c(1,2,1))

3. Concatenation

Purpose

Examples

# include character and numeric values in the same row
tab <- TR("Hello") %:% TR(c(1,2),percentage=T)

# bind with another row
tab + TR("\\textbf{World}") %:% TR(c(1,2),se=T)

4. Other Formatting Functions

Purpose

Examples

Vertical space: vspace

# bind with another row separated by 3pt vertical space
tab + vspace(3) + TR("\\textbf{World}") %:% TR(c(1,2),se=T)

Horizontal rule: midrule

# bind with another row separated by horizontal line
tab + midrule() + TR("\\textbf{World}") %:% TR(c(1,2),se=T)

Partial horizontal rule(s): midrulep

# bind with another row separated by a partial horizontal line
tab + midrulep(list(c(2,3))) + TR("\\textbf{World}") %:% TR(c(1,2),se=T)

5. Export to PDF with TS

Purpose

TS calls pdflatex to convert the textables object into a PDF file.

Example

Simple Example:

tab <- TR("hello") %:% TR(0.1)

TS(tab, file="simple_example", pretty_rules=T, header=c('l','c'), output_path=paste0(getwd(),"/inst"))

Complex Example:

library(data.table)

dd <- data.table(
    sample = c("Full Sample", "Full Sample", "Subsample", "Subsample"), 
    controls = c("No", "Yes", "No", "Yes"), 
    coef = c(1.17, 1.59, 1.105, 1.69),
    SEs = c(.6, .481, .789, .8), 
    pvals = c(.051, .001, .16, .091), 
    N = c(1234567, 1234567, 891011, 891011)
  )


tab <- TR(c("","Full Sample","Subsample"),cspan=c(1,2,2)) +
  vspace(5) +
  midrulep(list(c(2,3),c(4,5))) +
  TR("Controls") %:% with(dd, TR(controls)) +
  midrule()

tab <- tab +  TR("Coefficient ($\\tilde{\\beta}_\\nu$)") %:% 
      with(dd, TR(coef, pvalues=pvals, dec=2)) +  
  TR("Std. Error") %:% with(dd, TR(SEs, se=T, dec=2, surround = "{\\color{blue} %s}"))  + 
  midrule() + TR("Sample Size",surround="\\textbf{ %s}") %:% 
      with(dd, TR(unique(N), cspan=c(2,2), dec=0))


TS(tab, file="complex_example", pretty_rules=T, header=c('r',rep('c',4)), output_path=paste0(getwd(),"/inst"))

The complex_example result is shown at the beginning of this document.



setzler/textables documentation built on April 6, 2023, 7:40 p.m.