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}
tabular()
come from? – Bustamante