Hmisc::latex无法打印带有表格对象的标题

6
首先,我会告诉你我试图做什么,以便在我做错时进行更正。我有一个嵌套表格,想使用knitr在RStudio中输出为LaTeX表格。没有标题的情况下一切正常,但是添加标题后就不行了。我尝试了tables文档((LINK)第9页)上的示例,但没有成功。对于非表格对象,它也能正常工作。有趣的是,latex.default可以正常工作,但会导致RStudio/knitr的编译PDF出现错误,并且根据我的阅读,它被latex调用;此外,表格的四舍五入也不适当了。我尝试了latexTabular,但这也不适当地四舍五入。
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 :(

理想情况下,我希望能在输出中使用\caption{de},但我无法弄清楚我的错误在哪里。
如果有帮助的话,这是输入和输出:
> 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() 函数来自哪里? - Josh O'Brien
@Josh,抱歉,tabular来自tables包。 - Tyler Rinker
1
@Brandon 我认为xtable无法处理嵌套表格,但如果我错了,我会很高兴的,因为xtable非常好用。通常我会清理表格,但我正在尝试向完全可重复的研究方向发展,并将所有内容作为一个文件在knitr中运行。 - Tyler Rinker
这似乎从来都不像最初看起来的那么容易,是吧 :) - Brandon Bertelsen
3个回答

9

我很尴尬地承认,整个问题都是因为我试图在代码块中强制插入不属于它的内容。我愿意放下自尊来帮助未来的搜索者。LaTeX的内容应该放在外面。所以,如果你想将上面的表格绘制成一个漂亮格式的表格,这就是你要找的东西:

\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}

+1 不需要为此感到尴尬。这是一个很好的答案,如果你无法在 R 中完成所有工作,这也是一个很好的备选方案。 - Andrie

7

tabular()函数返回的x对象属于“tabular”类,将被分派到没有标题参数的latex.tabular中。我猜测它的预期用例是在Sweave中使用,Sweave将负责提供标题。

然而,在tables文档的第22页上有一个示例,可以在选项中使用"\\caption{.}"参数。这似乎取得了成功:

 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}

找到并不困难。我搜索了单词:“caption”。让它在我有限的LaTeX理解下工作才是值得的“+”。 - IRTFM

0

这应该可以工作。

cat('\\begin{table}[ht]
    \\centering')
latex(tabularTable)
cat('\\caption{some caption}')
cat('\\label{tab:table1}')
cat('\\end{table}')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接