R:xtable 标题(或注释)

发布于 2024-11-10 10:07:45 字数 254 浏览 5 评论 0原文

我想在 xtable 打印出来的表格下添加注释。 我认为最好的选择是使用“caption”选项: xtable(tablename, Caption="This is a标题”)。但这会以某种方式自动放入“表 1”,因此输出如下所示:

表 1:这是标题。

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

I want to put a comment under a table printed out by xtable. I figured that the best option would be to use the "caption" option: xtable(tablename, caption="This is a caption"). But this is somehow putting in a "Table 1" automatically, so that the output looks like:

Table 1: This is a caption.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぽ尐不点ル 2024-11-17 10:07:45

首先,一些模拟数据:

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

现在这是一个有点笨拙的解决方案,使用 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)

如果您希望将注释放在数据之前,请使用 comment$pos[[1]] <- c(0) 而不是 comment$pos[[1]] <- c(0) - c(nrow(z)) 并相应地调整 hline.after

这是我的输出:

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

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.

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.

Here's my 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}
ぶ宁プ宁ぶ 2024-11-17 10:07:45

这基本上是重新利用这个答案,但这是使用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))

这是 TeX 输出:

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

如果我用 \documentclass{article}\begin{document}\end 包装它,则会得到 .pdf 结果{文档}

在此处输入图像描述

当然,为了准备好发布,还需要添加更多花哨的东西,但这就是关键,您应该可以顺利进行。

This is basically repurposing this answer, but this is the most programmatic way to do this with xtable. It's ugly, mainly because I hate the way xtable's add.to.row argument works.

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:

% 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}:

enter image description here

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.

幸福%小乖 2024-11-17 10:07:45

如果您使用 RMarkdown,请将其添加到标题中:

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

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

编辑:

在与 xtable 包维护者 David(非常容易访问)交谈后,他提出了我在下面发布的解决方案:

我认为这个问题可以通过 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")

然后 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}

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

attr(dfList, "message") <- c("A caption", "Which can have", "multiple lines")

If you are using RMarkdown, add this to the 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:

I think this can be solved with xtableList. Create some data and convert the data frame to 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:

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文