2011-05-28 87 views
11

我想在xtable.打印的表格下發表評論我認爲最好的選擇是使用「標題」選項:xtable(tablename, caption="This is a caption")。但是,這會自動將「表1」放入「表1」中,以便輸出如下所示:R:xtable標題(或評論)

表1:這是一個標題。

是否有任何方法可以抑制這種或任何簡單的方式將簡單的方式放入表中作爲附加的最後一行?

+0

xtable輸出代碼(LaTeX或html),所以您必須表示LaTeX將「表1:」放在您標題文本的前面。這是LaTeX的正常行爲;我建議尋找解決方案。 – joran 2011-05-28 19:18:46

+0

任何想法在哪裏看? – user702432 2011-05-28 19:21:20

+1

嘗試在tex.stackexchange.com上搜索「抑制標題標籤」 – joran 2011-05-28 19:27:55

回答

12

首先,一些模擬數據:

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

現在,這裏是一個有些笨拙的解決方案,使用print.xtableadd.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(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} 
+2

不幸的是,這將所有標題文本放在第一列中,這會創建大量的空白區域。 – MichaelChirico 2015-02-15 18:36:58

0

這基本上是重新利用this的答案,但這是xtable做到這一點的最編程方法。這很醜,主要是因爲我討厭xtableadd.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}包裹它的.PDF結果,並\end{document}

enter image description here

當然,還有更多的花裏胡哨的東西需要添加才能使其發佈,但這是關鍵,你應該順利完成。

2

如果您正在使用RMarkdown,這增加了頭:

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

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

編輯:

與xtable包維護者交談後,大衛(這是非常接近),他想出了這個解決方案我post below:

我認爲這可以用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")