1

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}

2 Answers 2

1

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):

enter image description here

0

Thank you for sharing, I was looking for exactly this. I can add the caption as above but it still appears just as normal body text and without a table reference when I render the Latex doc. SO in case useful for anyone else

What worked for me was to use the latexTable function from the vignette:

   tab1<-latexTable(tabular((Species + 1) ~ (n=1) + Format(digits=2) 
      (Sepal.Length + Sepal.Width)*(mean + sd),data=iris), caption = "Iris sepal data", label = "sepals")

and then and just treated it as a kable object and could add headers, etc

    tab1  %>% add_header_above(c("Blah" = 1, "Bing" = 2, "Bong" = 2)) %>% save_kable(paste0(resultspath,"tab1.tex"), float = FALSE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.