Hmisc::latex not printing caption w/ tabular object

别说谁变了你拦得住时间么 提交于 2019-12-03 14:41:08

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}
bowen.li

This should work.

cat('\\begin{table}[ht]
    \\centering')
latex(tabularTable)
cat('\\caption{some caption}')
cat('\\label{tab:table1}')
cat('\\end{table}')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!