print_tabular: Helper function for printing tabular data in knitted PDFs

Description Usage Arguments Details See Also

Description

This function makes it a little more convenient to print a data frame as a nicely-formatted table in a knitted PDF. Use this function in a chunk with the results="asis" option set. The default values should be enough to get along with when you're starting out, so you can simply use print_tabular(x).

Usage

1
2
3
print_tabular(x, digits = 0, alignment = paste(ifelse(sapply(x, is.numeric),
  "r", "l"), collapse = ""), include.colnames = T, floating = F,
  caption = NULL, label = NULL)

Arguments

x

A data frame. Row names are ignored. Other objects must be converted manually. To print a two-way contingency table tab, you might try as.data.frame(as.matrix(tab)).

digits

The number of decimal places to keep for numbers. Numbers are rounded to integers by default.

alignment

A string specifying column formats. "l", "r", "c" mean left-aligned, right-aligned, or center-aligned respectively. There should be one specifier for each column. If this parameter is a single character, it is applied to all columns. To wrap the text of a column, use a specifier like "p{0.5 in}". By default, numeric columns are right-aligned and everything else is left-aligned.

include.colnames

Logical: should the colnames of the x be used as column headers? You may wish to make the column names more verbose (they can, in fact, include spaces and punctuation). TRUE, by default.

floating

Logical. If TRUE, the table will "float" to a good position for page layout. In that case, make sure to caption and label the table, and to refer to it in your text by label. FALSE, by default.

caption

Only applied to floating tables. Set to NULL for no caption (the default).

label

Only applied to floating tables. The LaTeX label for the table. Should be letters, hyphens, and colons only, with no spaces. If the label is, say, "tbl:laureates", then you can use \ref{tbl:laureates} in your markdown to represent the table number. So you typically write things like See table \ref{tbl:laureates} and the result is "See table 2" (or whatever number the table is). Set to NULL for no label (the default).

Details

It is not as easy as it should be to format tabular data nicely in R markdown. Your R data structure has to be converted into LaTeX commands for typesetting a table. This is not a profound computational challenge, but it involves a bunch of fiddly LaTeX parameters. The R package that helps with this, xtable, is itself fiddly and a bit confusing. This function is meant to simply matters somewhat.

Unlike the xtable function, the alignment parameter here must always be given as a single string. Row names are never used, so the number of columns to specify is the same as the number of columns in the data frame.

To typeset successfully, the resulting LaTeX requires the booktabs and longtable packages to be installed.

See Also

xtable, print.xtable


agoldst/litdata documentation built on May 10, 2019, 7:34 a.m.