-
Notifications
You must be signed in to change notification settings - Fork 1
Long table
Special assistant to the CEO of pmtables INC
The pmtables package can create tables as single page tabular tables or longtable. This wiki entry describes how to make long table.
For example, use the stable_long()
function to create long table
library(tidyverse)
library(pmtables)
data <- ptdata()
out <- stable_long(data, output_file = "example1.tex")
The result will be an object with classes stable_long
and stable
.
attributes(out)
. $class
. [1] "stable_long" "stable"
.
. $stable_file
. [1] "example1.tex"
head(out)
. [1] "{\\normalsize"
. [2] "\\renewcommand{\\arraystretch}{1.3}"
. [3] "\\setlength{\\tabcolsep}{5pt} "
. [4] "\\setlength{\\extrarowheight}{0em}"
. [5] "\\begin{longtable}{lllllllll}"
. [6] "\\endhead\n\\hline\n\\multicolumn{9}{r}{\\footnotesize{continued on next page}}\n\\endfoot\n\\hline\n\\endlastfoot"
stable_save(out)
Arguments for stable_long
can be found with ?stable_long
To make a long table using the pipe interface, add long = TRUE
to
st_make()
out <-
st_new(data) %>%
st_make(long = TRUE, output_file = "example2.tex")
attributes(out)
. $class
. [1] "stable_long" "stable"
.
. $stable_file
. [1] "example2.tex"
stable_save(out)
To source a long table into your TeX document, simply \input{}
the
file. Using the previous example (output file: example2.tex
):
\input{example2.tex}
IMPORTANT:
- Do not include any \caption{...}
statement; the caption must be
provided in a special way
- Do not warp the input in \begin{table}
/ \end{table}
; the table
will not show up properly that way
stable_long()
has an argument called lt_cap_text
that will allow you
to provide the text for the caption. This text must be passed to the
stable_long()
call (or st_make()
)
out <- stable_long(data, lt_cap_text = "A long table (example 3)")
You can see in the TeX code that a caption is included in the longtable environemt.
head(out, n = 10)
. [1] "{\\normalsize"
. [2] "\\renewcommand{\\arraystretch}{1.3}"
. [3] "\\setlength{\\tabcolsep}{5pt} "
. [4] "\\setlength{\\extrarowheight}{0em}"
. [5] "\\begin{longtable}{lllllllll}"
. [6] "\\endhead\n\\hline\n\\multicolumn{9}{r}{\\footnotesize{continued on next page}}\n\\endfoot\n\\hline\n\\endlastfoot"
. [7] "\\caption{A long table (example 3)} \\\\"
. [8] "\\hline"
. [9] "STUDY & DOSE & FORM & N & WT & CRCL & AGE & ALB & SCR \\\\"
. [10] "\\endfirsthead"
stable_save(out)
Rather than passing the text for the caption, you can name a macro that should (will) be defined at the time the table is rendered in the TeX document.
Use the lt_cap_macro
argument:
out <- stable_long(data, lt_cap_macro = "ltexfourcap", output_file = "example4.tex")
Now, there is a call to that macro in the table
head(out, n = 10)
. [1] "{\\normalsize"
. [2] "\\renewcommand{\\arraystretch}{1.3}"
. [3] "\\setlength{\\tabcolsep}{5pt} "
. [4] "\\setlength{\\extrarowheight}{0em}"
. [5] "\\begin{longtable}{lllllllll}"
. [6] "\\endhead\n\\hline\n\\multicolumn{9}{r}{\\footnotesize{continued on next page}}\n\\endfoot\n\\hline\n\\endlastfoot"
. [7] "\\caption{\\ltexfourcap} \\\\"
. [8] "\\hline"
. [9] "STUDY & DOSE & FORM & N & WT & CRCL & AGE & ALB & SCR \\\\"
. [10] "\\endfirsthead"
stable_save(out)
and you must define that macro prior to sourcing in your tex document
\newcommand{\ltexfourcap}{
Another long table - example 4
}
\input{example4.tex}
To add a label, pass in a caption (either as text or as a macro) and
then use the lt_cap_label
argument:
data %>%
stable_long(lt_cap_label = "tab:example", lt_cap_macro = "example") %>%
head(n = 7)
. [1] "{\\normalsize"
. [2] "\\renewcommand{\\arraystretch}{1.3}"
. [3] "\\setlength{\\tabcolsep}{5pt} "
. [4] "\\setlength{\\extrarowheight}{0em}"
. [5] "\\begin{longtable}{lllllllll}"
. [6] "\\endhead\n\\hline\n\\multicolumn{9}{r}{\\footnotesize{continued on next page}}\n\\endfoot\n\\hline\n\\endlastfoot"
. [7] "\\caption{\\example \\label{tab:example}} \\\\"
If you want to dump a table into an R markdown document, pass it to
st_asis()
. There is a method for stable_long()
and it will wrap the
table properly.
You won’t see the result here, but look at ?st_asis
for details.
out <- stable_long(data) %>% st_asis()