R:xtable标题(或注释)

R:xtable标题(或注释),r,xtable,R,Xtable,我想在由xtable打印的表格下添加注释。我想最好的选择是使用“caption”选项:xtable(tablename,caption=“This is caption”)。但这是自动放入“表1”,因此输出如下所示: 表1:这是一个标题 是否有任何方法可以抑制这种情况,或者更简单地将注释作为表中的最后一行添加?首先,一些模拟数据: x <- sample(LETTERS, 5, replace = TRUE) y <- sample(LETTERS, 5, replace = TRU

我想在由
xtable打印的表格下添加注释。
我想最好的选择是使用“caption”选项:
xtable(tablename,caption=“This is caption”)
。但这是自动放入“表1”,因此输出如下所示:

表1:这是一个标题

是否有任何方法可以抑制这种情况,或者更简单地将注释作为表中的最后一行添加?

首先,一些模拟数据:

x <- sample(LETTERS, 5, replace = TRUE)
y <- sample(LETTERS, 5, replace = TRUE)
z <- table(x, y)

如果您希望将注释放在数据之前,请使用
comment$pos[[1]]这基本上是重新调整答案的用途,但这是使用
xtable
实现这一点的最有程序性的方法。这很难看,主要是因为我讨厌
xtable
add.to.row
参数的工作方式

样本数据:

set.seed(230)
DF <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5))

#of course, we can pass this directly below; I'm just saving
#  horizontal space for this answer
comm <- paste0("\\hline \n \\multicolumn{4}{l}",
           "{\\scriptsize{Check out these random numbers!}} \n")

print.xtable(xtable(DF, caption = "Describe the table"),
             #adjusting hline.after so that our comment appears
             #  "outside" the table, as defined by its border
             hline.after=c(-1, 0),
             #**NOTE: the first argument to add.to.row must be
             #  a list -- don't ask me why since it strikes me as odd**
             add.to.row = list(pos = list(5),
                               command = comm))
如果我用
\documentclass{article}
\begin{document}
\end{document}
将其包装,则得到.pdf结果:


当然,为了使it出版物做好准备,还有更多的细节需要添加,但这是关键所在,您应该已经准备好了。

如果您使用的是RMarkdown,请在标题中添加以下内容:

---
(other configs here, like title, author, etc.)

header-includes:
    - \usepackage{caption}
    - \captionsetup{labelformat=empty}
---
编辑:

在与xtable软件包维护人员David(非常容易理解)交谈后,他提出了以下解决方案:

我认为这可以通过xtableList解决。创建一些数据并将数据帧转换为xtableList

要处理长标题,您需要拆分行:


attr(dfList,“message”)xtable输出代码(LaTeX或html),所以您的意思一定是LaTeX将“表1:”放在您的标题文本前面。这是正常的乳胶行为;我建议在那里寻找解决方案。尝试在tex.stackexchange.com上搜索“抑制标题标签”。此线程应该会对您有所帮助:这个问题似乎没有得到更多的答案,下面的任何一个问题解决了您的问题吗?如果是这样的话,你可以选择它作为接受,这将提高你的声誉。不幸的是,这将把所有的标题文本放在第一列,这会产生大量的空白。
set.seed(230)
DF <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5))

#of course, we can pass this directly below; I'm just saving
#  horizontal space for this answer
comm <- paste0("\\hline \n \\multicolumn{4}{l}",
           "{\\scriptsize{Check out these random numbers!}} \n")

print.xtable(xtable(DF, caption = "Describe the table"),
             #adjusting hline.after so that our comment appears
             #  "outside" the table, as defined by its border
             hline.after=c(-1, 0),
             #**NOTE: the first argument to add.to.row must be
             #  a list -- don't ask me why since it strikes me as odd**
             add.to.row = list(pos = list(5),
                               command = comm))
% latex table generated in R 3.2.4 by xtable 1.8-2 package
% Mon May 23 18:25:14 2016
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \hline
 & a & b & c \\ 
  \hline
1 & -0.23 & 0.04 & 1.34 \\ 
  2 & 0.10 & 0.57 & -1.62 \\ 
  3 & 0.33 & -0.14 & 0.83 \\ 
  4 & 0.36 & -0.75 & 0.20 \\ 
  5 & 0.44 & 0.13 & -0.49 \\ 
   \hline 
 \multicolumn{4}{l}{\scriptsize{Check out these random numbers!}} 
\end{tabular}
\caption{Describe the table} 
\end{table}
---
(other configs here, like title, author, etc.)

header-includes:
    - \usepackage{caption}
    - \captionsetup{labelformat=empty}
---
set.seed(230)
DF <- data.frame(a = rnorm(5), b = rnorm(5), c = rnorm(5))
library(xtable)
dfList <- list(DF)
attr(dfList, "message") <- c("A caption", "Which can have multiple lines")
print(xtableList(dfList))
## % latex table generated in R 3.2.5 by xtable 1.8-3 package
## % Sat Jul 09 21:52:53 2016
## \begin{table}[ht]
## \centering
## \begin{tabular}{rrrr}
## \hline
## & a & b & c \\
## \hline
## 1 & -0.23 & 0.04 & 1.34 \\
## 2 & 0.10 & 0.57 & -1.62 \\
## 3 & 0.33 & -0.14 & 0.83 \\
## 4 & 0.36 & -0.75 & 0.20 \\
## 5 & 0.44 & 0.13 & -0.49 \\
## \hline
## \multicolumn{4}{l}{A caption}\\
##
## \multicolumn{4}{l}{Which can have multiple lines}\\
## \end{tabular}
## \end{table}
attr(dfList, "message") <- c("A caption", "Which can have", "multiple lines")