[英]R: xtable caption (or comment)
I want to put a comment under a table printed out by xtable.
我想在xtable.
I figured that the best option would be to use the "caption" option: xtable(tablename, caption="This is a caption")
.我认为最好的选择是使用“标题”选项: xtable(tablename, caption="This is a caption")
。 But this is somehow putting in a "Table 1" automatically, so that the output looks like:但这以某种方式自动放入“表 1”,因此 output 看起来像:
Table 1: This is a caption.表 1:这是一个标题。
Is there any way to suppress this or any simpler way of putting in a comment simply as an additional last row in the table?有什么方法可以抑制这种情况,或者有任何更简单的方法将评论简单地作为表格中的最后一行添加?
First, some mock data:首先,一些模拟数据:
x <- sample(LETTERS, 5, replace = TRUE)
y <- sample(LETTERS, 5, replace = TRUE)
z <- table(x, y)
Now here's a somewhat clumsy solution, using print.xtable
's add.to.row
argument.现在这是一个有点笨拙的解决方案,使用print.xtable
的add.to.row
参数。
comment <- list()
comment$pos <- list()
comment$pos[[1]] <- c(nrow(z))
comment$command <- c(paste("\\hline \n", # we`ll replace all default hlines with this and the ones below
"your footnote, caption or whatever. \n",
sep = ""))
print(xtable(z),
add.to.row = comment,
hline.after = c(-1, 0)) # indicates rows that will contain hlines (the last one was defined up there)
If you want your comment to be placed before the data, use comment$pos[[1]] <- c(0)
instead of comment$pos[[1]] <- c(nrow(z))
and adjust hline.after
accordingly.如果您希望将评论放在数据之前,请使用comment$pos[[1]] <- c(0)
而不是comment$pos[[1]] <- c(nrow(z))
并调整hline.after
相应hline.after
。
Here's my output:这是我的 output:
% latex table generated in R 2.14.1 by xtable 1.7-0 package
% Mon Feb 20 02:17:58 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
\hline
& B & C & P & V \\
\hline
A & 0 & 0 & 0 & 1 \\
D & 1 & 0 & 0 & 0 \\
I & 0 & 0 & 0 & 1 \\
P & 0 & 0 & 1 & 0 \\
Z & 0 & 1 & 0 & 0 \\
\hline
your footnote, caption or whatever.
\end{tabular}
\end{center}
\end{table}
This is basically repurposing this answer, but this is the most programmatic way to do this with xtable
.这基本上是重新利用这个答案,但这是使用xtable
执行此操作的最程序化的方式。 It's ugly, mainly because I hate the way xtable
's add.to.row
argument works.这很丑,主要是因为我讨厌xtable
的add.to.row
参数的工作方式。
Sample data:样本数据:
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))
Here's the TeX output:这是 TeX output:
% 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}
And the.pdf result if I wrap it with \documentclass{article}
, \begin{document}
, and \end{document}
:如果我用\documentclass{article}
、 \begin{document}
和\end{document}
包装它,那么 .pdf 结果:
Of course, there are much more bells and whistles to add to get it publication-ready, but this is the crux and you should be well on your way.当然,还有更多花里胡哨的东西要添加以使其可以出版,但这是症结所在,您应该一切顺利。
If you are using RMarkdown, add this to the header:如果您使用的是 RMarkdown,请将其添加到 header:
---
(other configs here, like title, author, etc.)
header-includes:
- \usepackage{caption}
- \captionsetup{labelformat=empty}
---
Edit:编辑:
After talking with xtable package maintainer, David (that was very accessible), he came with this solution I post below:在与 xtable package 维护者 David(这很容易理解)交谈后,他提出了我在下面发布的这个解决方案:
I think this can be solved with xtableList.我认为这可以通过 xtableList 解决。 Create some data and convert the data frame to xtableList.创建一些数据并将数据框转换为 xtableList。
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")
Then xtable produces the following:然后 xtable 产生以下内容:
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}
To deal with long captions you will need to split lines:要处理长标题,您需要拆分行:
attr(dfList, "message") <- c("A caption", "Which can have", "multiple lines")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.