Learn R Programming

inlmisc (version 0.4.3)

PrintTable: Print as LaTeX Table

Description

This function prints the LaTeX code associated with the supplied data table. The applied output format attempts to adhere to the design recommendations for tables in United States Geological Survey (USGS) publications.

Usage

PrintTable(d, colheadings = NULL, align = NULL, digits = NULL,
  label = NULL, title = NULL, headnotes = NULL, footnotes = NULL,
  nrec = nrow(d), hline = NULL, na = "--", rm_dup = NULL,
  landscape = FALSE, ...)

Arguments

d

'data.frame'. Data table to print.

colheadings

'character'. Vector of length equal to the number of columns in the table, containing the column headings. Use \\\\ to code a line break.

align

'character'. Vector of length equal to the number of columns in the table, indicating the alignment of the corresponding columns. Use "l", "r", and "c" to denote left, right, and center alignment, respectively.

digits

'integer'. Vector of length equal to the number of columns in the table, indicating the number of digits to display in the corresponding columns.

label

'character'. String containing the LaTeX label anchor. Specifying this argument allows you to easily reference the table within the LaTeX document. For example, when label = "id", use \ref{id} to reference the table within a sentence.

title

'character'. String containing the table caption.

headnotes

'character'. String placed below the table caption to provide information pertaining to the caption, to the table as a whole, or to the column headings.

footnotes

'character'. String placed at the end of the table to provide explanations of individual entries in the table.

nrec

'integer'. Vector of length equal to 2, indicating the maximum number of records to show on the first page, and every subsequent page, respectively. Value is recycled as necessary.

hline

'integer'. Vector of numbers between 1 and nrow(d) - 1, indicating the table rows after which a horizontal line should appear.

na

'character'. String to be used for missing values in table entries.

rm_dup

'integer'. End value of a sequence of column indexes (1:rm_dup). Duplicate values contained in these columns will be set equal to an empty string. Where duplicates in a column are determined from the 'character' vector formed by combining its content with the content from all previous columns in the table.

landscape

'logical'. If true, conforming PDF viewers will display the table in landscape orientation. This option requires \usepackage[pdftex]{lscape} in the LaTeX preamble.

...

Additional arguments to be passed to the print.xtable function. The arguments type, hline.after and add.to.row should not be included.

Value

Invisible NULL

Details

Requires \usepackage{caption}, \usepackage{booktabs}, and \usepackage{makecell} in the LaTeX preamble.

See Also

xtable

Examples

Run this code
# NOT RUN {
d <- datasets::iris[, c(5, 1:4)]
colheadings <- c("Species of Iris",
                 "Sepal length \\\\ (cm)", "Sepal width \\\\ (cm)",
                 "Petal length \\\\ (cm)", "Petal width \\\\ (cm)")
align <- c("l", "c", "c", "c", "c")
digits <- c(0, 1, 1, 1, 1)
title <- "Measurements of sepal length and width and petal length and width,
          for three species of Iris flower."
headnotes <- "\\textbf{Species of Iris}: includes setosa, versicolor, and virginica.
              \\textbf{Abbreviations}: cm, centimeters"
levels(d[[1]]) <- sprintf("%s\\footnotemark[%d]", levels(d[[1]]), 1:3)
footnotes <- paste(sprintf("\\footnotemark[%d] Common name is %s iris.", 1:3,
                           c("Wild Flag", "Blue Flag", "Virginia")), collapse = "\\\\")
hline <- utils::tail(which(!duplicated(d[[1]])), -1) - 1L
PrintTable(d, colheadings, align, digits, title = title, headnotes = headnotes,
           footnotes = footnotes, hline = hline, nrec = c(41, 42), rm_dup = 1)

# }
# NOT RUN {
sink("table-example.tex")
cat("\\documentclass{article}",
    "\\usepackage[labelsep=period,labelfont=bf]{caption}",
    "\\usepackage{booktabs}",
    "\\usepackage{makecell}",
    "\\usepackage[pdftex]{lscape}",
    "\\makeatletter",
    "\\setlength{\\@fptop}{0pt}",
    "\\makeatother",
    "\\begin{document}", sep = "\n")
PrintTable(d, colheadings, align, digits, title = title, headnotes = headnotes,
           footnotes = footnotes, hline = hline, nrec = c(41, 42), rm_dup = 1)
cat("\\clearpage\n")
PrintTable(datasets::CO2[, c(2, 3, 1, 4, 5)], digits = c(0, 0, 0, 0, 1),
           title = "Carbon dioxide uptake in grass plants.", nrec = 45, rm_dup = 3)
cat("\\clearpage\n")
PrintTable(datasets::mtcars, title = "Motor trend car road tests.",
           landscape = TRUE, include.rownames = TRUE)
cat("\\end{document}\n")
sink()
tools::texi2pdf("table-example.tex", clean = TRUE)  # requires TeX installation
system("open table-example.pdf")

file.remove("table-example.tex", "table-example.pdf")
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab