I am trying to add a caption to a table output from the package "tables" in R followed by additional formatting using kableExtra.
Although other threads have found ways to add caption headers by using additional LaTeX code (Caption not appearing for LaTeX table when knitting using Hmisc LaTeX Function and Hmisc::latex not printing caption w/ tabular object), these solutions do not appear compatible with the more recent toKable()
function which allows additional formatting with kableExtra.
Typically a caption would be added during when kable(x, caption = "mycaption")
is used. However when it is genereated as following, an error will occur (Error in toKable(., booktabs = T) : 'table' must be a 'tabular' object.
). It appears if I try to add any additional formatting through latex()
, such as adding a caption, the object type will change making it unusable with the toKable()
function. Any insight into how to use toKable()
with additional LaTeX formatting that has been passed through latex()
would be greatly appreciated!
library(tables)
library(magrittr)
library(kableExtra)
tabular((Species + 1) ~ (n=1) + Format(digits=2)* (Sepal.Length + Sepal.Width)*(mean + sd), data=iris) %>%
latex(., options = list(tabular = "longtable",
toprule = "\\caption{Table 1. My favorite caption}\\\\\\toprule")) %>%
toKable(., booktabs = T)
The LaTeX output before being passed to toKable()
:
\begin{longtable}{lccccc}
\caption{Table 1. My favorite caption}\\\toprule
& & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\
Species & n & mean & sd & mean & \multicolumn{1}{c}{sd} \\
\hline
setosa & $\phantom{0}50$ & $5.01$ & $0.35$ & $3.43$ & $0.38$ \\
versicolor & $\phantom{0}50$ & $5.94$ & $0.52$ & $2.77$ & $0.31$ \\
virginica & $\phantom{0}50$ & $6.59$ & $0.64$ & $2.97$ & $0.32$ \\
All & $150$ & $5.84$ & $0.83$ & $3.06$ & $0.44$ \\
\hline
\end{longtable}
After tinkering around a bit with the code and trying to understand how each works... I tried pasting the options list directly into toKable
. This seemed to work and it appears toKable
shared similar options to those of latex()
.
tabular((Species + 1) ~ (n=1) + Format(digits=2)* (Sepal.Length + Sepal.Width)*(mean + sd),
data=iris) %>%
toKable(., booktabs = T,
options = list(tabular = "longtable",
toprule = "\\caption{My favorite caption}\\\\\\toprule"))
This spits out the following LaTex code correctly, as desired above:
\begin{longtable}{lccccc}
\caption{Table 1. My favorite caption}\\\toprule
& & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\ \cmidrule(lr){3-4}\cmidrule(lr){5-6}
Species & n & mean & sd & mean & \multicolumn{1}{c}{sd} \\
\midrule
setosa & $\phantom{0}50$ & $5.01$ & $0.35$ & $3.43$ & $0.38$ \\
versicolor & $\phantom{0}50$ & $5.94$ & $0.52$ & $2.77$ & $0.31$ \\
virginica & $\phantom{0}50$ & $6.59$ & $0.64$ & $2.97$ & $0.32$ \\
All & $150$ & $5.84$ & $0.83$ & $3.06$ & $0.44$ \\
\bottomrule
\end{longtable}
The LaTeX code can then be presented as needed in reports or otherwise. In a Rmarkdown document converted to PDF this can look like (remember to call the tables
andkableExtra
packages):