PrintTable: Print as LaTeX Table

Description Usage Arguments Details Value Author(s) Examples

View source: R/PrintTable.R

Description

Print 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
PrintTable(
  d,
  colheadings = NULL,
  align = NULL,
  digits = NULL,
  label = NULL,
  title = NULL,
  headnotes = NULL,
  footnotes = NULL,
  nrec = nrow(d),
  hline = NULL,
  na = "\\textemdash",
  rm_dup = NULL,
  landscape = FALSE,
  ...
)

Arguments

d

'data.frame' or 'matrix'. Data table to print.

colheadings

'character' vector, 'matrix', or 'data.frame'. Column headings. For table objects, rows represent layers of headings (stacked headings). The number of columns (or vector length) must equal the number of columns in argument d. A column heading can span multiple columns by repeating adjacent headings. Use \\\\ to code a line break.

align

'character' vector. Column alignment. Specify "l" to left align, "r" to right align, "c" to center align, and "S" to align on the decimal point.

digits

'integer' vector. Number of digits to display in the corresponding columns.

label

'character' string. 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. Table caption

headnotes

'character' string. Label 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. Label placed at the end of the table to provide explanations of individual entries in the table.

nrec

'integer' vector of length 1 or 2, value is recycled as necessary. Maximum number of records to show on the first page, and every subsequent page, respectively.

hline

'integer' vector. Numbers between 1 and nrow(d) - 1 indicating the table rows after which a horizontal line should appear.

na

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

rm_dup

'integer' count. 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' flag. 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.

Details

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

Value

Invisible NULL

Author(s)

J.C. Fisher, U.S. Geological Survey, Idaho Water Science Center

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
d <- datasets::iris[, c(5, 1:4)]
colheadings <- rbind(c("Species \\\\ type", rep("Sepal", 2), rep("Petal", 2)),
                     c("", rep(c("Length", "Width"), 2)))
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{Iris Species}: setosa, versicolor, and virginica.
              \\textbf{Abbreviations}: cm, centimeters"
levels(d[[1]]) <- sprintf("%s\\footnotemark[%d]", levels(d[[1]]), 1:3)
footnotes <- sprintf("\\footnotemark[%d] Common name is %s iris.",
                     1:3, c("Wild Flag", "Blue Flag", "Virginia"))
footnotes <- paste(footnotes, 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("test-table.tex")
cat("\\documentclass{article}",
    "\\usepackage{geometry}",
    "\\usepackage[labelsep = period, labelfont = bf]{caption}",
    "\\usepackage{siunitx}",
    "\\sisetup{input-ignore = {,}, input-decimal-markers = {.},",
    "          group-separator = {,}, group-minimum-digits = 4}",
    "\\usepackage{booktabs}",
    "\\usepackage{makecell}",
    "\\usepackage{multirow}",
    "\\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")
digits <- c(1, 0, 1, 0, 2, 3, 2, 0, 0, 0, 0)
PrintTable(datasets::mtcars, digits = digits,
           title = "Motor trend car road tests.",
           landscape = TRUE, include.rownames = TRUE)
cat("\\clearpage\n")
x <- c(1.2, 1.23, 1121.2, 184, NA, pi, 0.4)
d <- data.frame(matrix(rep(x, 4), ncol = 4),
                stringsAsFactors = TRUE)
d[, 1] <- prettyNum(d[, 1])
d[, 4] <- formatC(d[, 4], digits = 2, format = "e")
colheadings <- paste("Wide heading", 1:ncol(d))
align <- c("S", "S",
           "S[round-mode = places, round-precision = 2]",
           "S[scientific-notation = true, table-format = 1.2e+1]")
PrintTable(d, colheadings, align)
cat("\\end{document}\n")
sink()
tinytex::pdflatex("test-table.tex")  # requires LaTeX
system("open test-table.pdf")

file.remove("test-table.tex", "test-table.pdf")

## End(Not run)

inlmisc documentation built on Jan. 25, 2022, 1:14 a.m.