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}
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}
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}
This should work.
cat('\\begin{table}[ht]
\\centering')
latex(tabularTable)
cat('\\caption{some caption}')
cat('\\label{tab:table1}')
cat('\\end{table}')
来源:https://stackoverflow.com/questions/12398093/hmisclatex-not-printing-caption-w-tabular-object