Hmisc::latex not printing caption w/ tabular object
Asked Answered
T

3

6

First I will tell you what I'm attempting to do big picture in case I'm going about it wrong. I have a nested table that I'd like to out put as a LaTeX table within RStudio using knitr. I am fine until I try to add a caption. I tried the example on page 9 in the tables vignette (LINK).

It works without the caption but when I add the caption it doesn't. It also works with a non tabular object. The funny thing is that latex.default works but causes an error in RStudio/knitr's Compile PDF and from what I read is called by latex anyway; plus the table isn't rounded appropriately anymore. I tried latexTabular but that isn't rounded appropriately either.

library(Hmisc); library(tables)
latex(head(mtcars), file="", caption="de")   #works

x <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
         (Sepal.Length + Sepal.Width)*(mean + sd), data=iris )

latex(x, file="", caption="de") #no caption :(

Ideally I'd want to be able to have \caption{de}in the output but can't figure out where I'm going wrong.

In case it's helpful here's the input and output:

> latex(x, file="", caption="de", label="tab1") 
\begin{tabular}{lccccc}
\hline
 &  & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\ 
Species  & n & mean & sd & mean & 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{tabular}
Tartuffe answered 13/9, 2012 at 1:5 Comment(4)
Where does tabular() come from?Bustamante
@Josh my apologies tabular comes from the tables package.Tartuffe
@Brandon I don't think xtable can handle nested tables but I'd love to be wrong, as xtable has been great to use. Normally I'd cleant he table up but I'm trying to move toward fully reproducible research and running everything as one file with knitr.Tartuffe
It's never as easy as it initially seems is it :)Robinrobina
S
7

The x object from tabular() is of class 'tabular' and is being dispatched to latex.tabular which has no caption argument. I'm guessing that it's intended use case is within Sweave which would be tasked with supplying the caption.

There is , however, an example on page 22 of using a "\\caption{.}" argument to options in the tables vignette. This seems to yield success:

 x <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
          (Sepal.Length + Sepal.Width)*(mean + sd), data=iris )

 latex(x, file="", options = list( tabular="longtable", toprule="\\caption{This is a sample caption.}\\\\   \\toprule",  midrule="\\midrule\\\\[-2\\normalbaselineskip]\\endhead\\hline\\endfoot"))
\begin{longtable}{lccccc}
\caption{This is a sample caption.}\\   \toprule
 &  & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\ 
Species  & n & mean & sd & mean & sd \\ 
\midrule\\[-2\normalbaselineskip]\endhead\hline\endfoot
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}
Strachan answered 13/9, 2012 at 1:30 Comment(1)
Finding wasn't difficult. I searched on the word: 'caption'. Getting it to work with my limited understanding of LaTeX is what was worth the "+'.Strachan
T
9

I'm embarrassed to admit this but the whole problem was me trying to force something inside the code chunk that didn't belong. I'm choking on my pride to help future searchers. The latex stuff goes on the outside. So if you're trying to plot the table above as a nicely formatted table this is what you're looking for:

\begin{table}[ht]
\caption{This is a sample caption. \label{guy}}
<<desc, echo = FALSE, results = 'asis'>>=
x <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
     (Sepal.Length + Sepal.Width)*(mean + sd), data=iris )
latex(x)
@
\end{table}
Tartuffe answered 13/9, 2012 at 3:51 Comment(1)
+1 No need to be embarrassed about this. This is a good answer and a good fallback if you can't get it all done in R.Chalco
S
7

The x object from tabular() is of class 'tabular' and is being dispatched to latex.tabular which has no caption argument. I'm guessing that it's intended use case is within Sweave which would be tasked with supplying the caption.

There is , however, an example on page 22 of using a "\\caption{.}" argument to options in the tables vignette. This seems to yield success:

 x <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
          (Sepal.Length + Sepal.Width)*(mean + sd), data=iris )

 latex(x, file="", options = list( tabular="longtable", toprule="\\caption{This is a sample caption.}\\\\   \\toprule",  midrule="\\midrule\\\\[-2\\normalbaselineskip]\\endhead\\hline\\endfoot"))
\begin{longtable}{lccccc}
\caption{This is a sample caption.}\\   \toprule
 &  & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\ 
Species  & n & mean & sd & mean & sd \\ 
\midrule\\[-2\normalbaselineskip]\endhead\hline\endfoot
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}
Strachan answered 13/9, 2012 at 1:30 Comment(1)
Finding wasn't difficult. I searched on the word: 'caption'. Getting it to work with my limited understanding of LaTeX is what was worth the "+'.Strachan
M
0

This should work.

cat('\\begin{table}[ht]
    \\centering')
latex(tabularTable)
cat('\\caption{some caption}')
cat('\\label{tab:table1}')
cat('\\end{table}')
Meerschaum answered 29/8, 2015 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.