Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/setzler/textables
Customized LaTeX tables in R
https://github.com/setzler/textables
Last synced: about 2 months ago
JSON representation
Customized LaTeX tables in R
- Host: GitHub
- URL: https://github.com/setzler/textables
- Owner: setzler
- License: mit
- Created: 2018-07-14T21:35:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-28T16:06:13.000Z (almost 2 years ago)
- Last Synced: 2024-08-13T07:11:06.607Z (5 months ago)
- Language: R
- Homepage:
- Size: 283 KB
- Stars: 27
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - setzler/textables - Customized LaTeX tables in R (R)
README
---
title: "textables: Customized LaTeX tables in R"
author: Created by Thibaut Lamadon and Bradley Setzler, University of Chicago
output: github_document
---```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```This package produces highly-customized LaTeX tables in R. There are two key functions:
- `TR`: Form a row in a LaTeX tabular environment.
- `TS`: Compile a textable object to produce a PDF file of the table.It supports building a table in blocks, in the spirit of ggplot2, using the `+` and `%:%` operators for concatenation.
To install, run:
```{r echo=T, eval=F}
devtools::install_github("setzler/textables")
```
```{r echo=T, eval=T}
library(textables)
```Here is an example of the type of table that this package can easily construct:
![Example Table](inst/complex_example.png)
# 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
```{r echo=T, eval=T}
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:
- `dec`: control decimal places, for example, `dec=3` displays 3 decimal places;
- `se`: surround numbers with parenthesis with `se=TRUE`;
- `percentage`: add a percentage sign to each number with `percentage=TRUE`;
- `pvalues`: use p-values to add stars to indicating significance, for example, `pvalues=c(0.005,0.05)` would add 3 stars and 2 stars, respectively.
- `cspan`: column span.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`
```{r echo=T, eval=T}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`
```{r echo=T, eval=T}# 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`
```{r echo=T, eval=T}# 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`
```{r echo=T, eval=T}# add stars by providing p-values
TR(vec, dec=c(3,2,1), pvalues=c(.01,.05,.1))```
**General modification of each value:** `surround`
```{r echo=T, eval=T}
# turn all numbers red
TR(vec, surround = "{\\color{red} %s}")```
**Column span:** `cspan`
```{r echo=T, eval=T}# span two columns with the middle number
TR(vec, dec=c(3,2,1), cspan=c(1,2,1))```
# 3. Concatenation
### Purpose
- `+`: binds rows together vertically. It allows us to have multiple rows in a tabular.
- `%:%`: binds columns together horizontally. It allows us to include both character and numeric vectors in the same row.### Examples
```{r echo=T, eval=T}
# 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
- `vspace`: add vertical space between rows.
- `midrule`: add a horizontal line.
- `midrulep`: add a partial horizontal line.### Examples
**Vertical space:** `vspace`
```{r echo=T, eval=T}# bind with another row separated by 3pt vertical space
tab + vspace(3) + TR("\\textbf{World}") %:% TR(c(1,2),se=T)```
**Horizontal rule:** `midrule`
```{r echo=T, eval=T}# bind with another row separated by horizontal line
tab + midrule() + TR("\\textbf{World}") %:% TR(c(1,2),se=T)```
**Partial horizontal rule(s):** `midrulep`
```{r echo=T, eval=T}# 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.
- `file`: name of PDF (must end in `.pdf`)
- `pretty_rules`: includes double-rules at the top and bottom of the table. Default is `TRUE`.
- `header`: character vector with alignment of rows. Must be same length as number of rows.
- `output_path`: Directory path in which to place the PDF. Default is `getwd()`, i.e., the current directory.### Example
**Simple Example:**
```{r echo=T, eval=T}
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:**
```{r echo=T, eval=T}
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.