在xtable中添加一个表格标题和表格下方的说明。

7
我希望在Rnw文档的xtable上放置一个标题。以下是代码。不幸的是,我无法在表格下添加标题。我尝试了\caption{}函数,但它不会打印PDF。
我看过R:xtable标题(或注释),但对于从R中的lm()函数创建的表格不起作用。你有什么线索吗?
<<yoman,echo=FALSE,results=tex>>=
library(xtable)

pop5lm <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(pop5lm,
             caption = c("Estimates of linear model for father Muro CB"), 
             label = "tab:one", digits = c(0,2, 2, 2,3)), 
             table.placement = "tbp", 
             caption.placement = "top")
@

好的,标题已经打印出来了,但是如果我想在表格下面添加描述,就不起作用了。我不知道该怎么做。例如,我希望添加:“在这个表格中,我使用了线性模型,blablabla...”。简而言之,将会有标题、表格和与表格相关联的描述。顺便说一句,谢谢你的快速回复! - M. Beausoleil
1个回答

9

我在xtable中没有找到快速选项来在表格底部添加文本(这并不意味着没有这样的选项),因此我使用了这里和您问题中的链接中的想法。这是一个相当粗糙的解决方案,缺点是需要指定要添加的文本的宽度(等于表格的宽度)- 如果太长,它会拉伸最后一列(更改8.5为10以查看变化)。

\documentclass{article}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<yoman,echo=FALSE,results=tex>>=
library(xtable)

mod <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(mod,
             caption = "Estimates of linear model for father Muro CB ", 
             #label = "tab:one", 
             digits = c(0,2, 2, 2,3)), 
             table.placement = "h!", 
             caption.placement = "top",
             add.to.row = list(list(2),  
             "\\hline  \\multicolumn{5}{L{8.5cm}}{\\textbf{Note: }
             This is a description, blah, blah, blah, blah,  blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah} \\\\"))

@

\end{document}

图片描述

我认为latex有很多可选方案可以实现这一点,但可能会让你开始。


来自评论:我尝试将其输出到html,但没有成功。 有什么想法吗?

您可以在print.tableadd.to.row参数中更改latex命令multicolumn以使用html表函数。 (使用Rmarkdown的html输出)

```{r,echo=FALSE, results='asis'}
library(xtable)

mod <- lm(mpg ~ wt, data=mtcars) #my linear model

print(xtable(mod,
             caption = "Estimates of linear model for father Muro CB ", 
             digits = c(0,2, 2, 2,3)), 
             type="html",
             caption.placement = "top",
             add.to.row = list(list(2),  
             '<tr><td colspan="5"><b>Note: </b>
             This is a description, blah, blah, blah, blah,  blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah, blah, blah, blah, blah, 
             blah, blah, blah, blah, blah, blah</td></tr>'))

```

非常感谢!它正在工作。如果您知道,为什么它会在末尾创建一个新行。是否可以将其删除?这是设计目的! - M. Beausoleil
不客气。您是指文本下方的水平线(\hline)吗?如果是这样,要删除它,请指定您想要的行位置;使用hline.after来指定您想要的\hlines [..., blah}"), hline.after = c(-1, 0))]。 - user20650
1
哇!这正是我想要的。你太棒了! - M. Beausoleil
我尝试将其输出到HTML,但没有成功。有什么想法吗? - Fernando Hoces De La Guardia
1
@user20650 太棒了!谢谢! - Fernando Hoces De La Guardia

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