2014-10-17 31 views
12

基本R中的table函數添加了漂亮的行/列標籤,但是當我使用knitr::kable時,這些消失。任何簡單的方法來保持這些,除了在html/markdown端添加它們?使用kable和knitr保存表()中的行/列標籤

重複的例子:

library(knitr) 

# reproducibility 
set.seed(123) 

# here's a df 
some_data <- 
    data.frame(a=sample(c('up','down'), 10, replace=T), 
      b=sample(c('big','small'), 10, replace=T)) 

# using table() you get nice labels ('a' and 'b', in this case) 
table(some_data) 

enter image description here

# that goes away with kable, in either markdown or html format (I care about html) 
kable(table(some_data)) 
kable(table(some_data), format='html') 

enter image description here

+0

你可以看到,如果printr包看起來挺有意思的你:http://yihui.name/printr/ – 2014-10-19 04:39:37

回答

1

雖然有點哈克,結合tablesxtable包可以讓你列聯表的HTML與行/列名稱。這對你有用嗎?

require(xtable) 
require(tables) 

some_data <- 
    data.frame(a=sample(c('up','down'), 10, replace=T), 
      b=sample(c('big','small'), 10, replace=T)) 

tab <- as.matrix(tabular(Factor(a)~Factor(b), data=some_data)) 

print(xtable(data.frame(tab)), type="html", include.rownames=F, include.colnames=F) 
+0

謝謝您的回答。我覺得這是多行/多隻與'kable'堅持,並通過'名字(dimnames(...))'在降價文檔顯示,儘管 – arvi1000 2014-10-17 18:10:12

+0

@ arvi1000同意,這是什麼工作訪問標籤的複雜性最適合你。 – cdeterman 2014-10-17 18:12:49

4

不是最佳解決方案(如Pandoc的降價不支持COL/rowspans),但給一個嘗試pander,其旨在變換ř對象到降價輕鬆(和一堆選項):

> library(pander) 
> pander(ftable(some_data)) 

------ --- ----- ------- 
     "b" "big" "small" 

"a"      

"down"  5  1 

"up"  0  4 
------ --- ----- ------- 

> pander(ftable(some_data), style = 'rmarkdown') 

|  |  |  |   | 
|:------:|:---:|:-----:|:-------:| 
|  | "b" | "big" | "small" | 
| "a" |  |  |   | 
| "down" |  | 5 | 1 | 
| "up" |  | 0 | 4 | 
5

整潔,別人貼在我的老問題賞金。無論如何,如果它是有幫助的,我的解決方案是一個自制的HTML生成功能

table_label <- function(tbl) { 

    # table dimensions 
    rows <- dim(tbl)[1] 
    cols <- dim(tbl)[2] 

    # get started 
    html_out <- '<table>\n' 

    # first row: label only 
    blank_cell <- '<td>&nbsp;</td>' 
    html_out <- 
    paste0(html_out, 
      '\t<tr>', 
      blank_cell, 
      '<td>', names(dimnames(tbl))[2], '</td>', # column label 
      rep(blank_cell, cols-2), 
      '</tr>\n') 

    # second row: 
    html_out <- 
    paste0(html_out, 
      '\t<tr>', 
      # label... 
      '<td>', names(dimnames(tbl))[1], '</td>', 
      # ...and headers 
      paste0('<td>', dimnames(tbl)[[2]], '</td>', collapse=''), 
      '</tr>\n') 

    # subsequent rows 
    for (i in 1:rows) { 
    html_out <- 
     paste0(html_out, 
      '\t<tr>', 
      # header... 
      '<td>', dimnames(tbl)[[1]][i], '</td>',       
      # ...and values 
      paste0('<td>', tbl[i,], '</td>', collapse=''), 
      '</tr>\n') 
    } 

    # last row 
    html_out <- paste0(html_out, '</table>') 
    return(html_out) 
} 

現在這個降價DOC:

Produce table 
```{r} 
set.seed(123) 

some_data <- 
    data.frame(a=sample(c('up','down'), 10, replace=T), 
      b=sample(c('big','small', 'medium'), 10, replace=T)) 

tbl <- table(some_data) 
``` 

Now display 
```{r, results='asis'} 
cat(table_label(tbl)) 
``` 

可生產我想要的結果:

enter image description here

的生成html有些可讀,也:

<table> 
    <tr><td>&nbsp;</td><td>b</td><td>&nbsp;</td></tr> 
    <tr><td>a</td><td>big</td><td>medium</td><td>small</td></tr> 
    <tr><td>down</td><td>4</td><td>0</td><td>2</td></tr> 
    <tr><td>up</td><td>0</td><td>4</td><td>0</td></tr> 
</table> 
+0

這隻適用於二維表格,但可容納任意數量的行/列。我發現,任何表我想顯示的通常是2D的,無論如何,但有人可能會延長這一若再變暗所需 – arvi1000 2015-01-18 04:49:21

+0

嗯似乎並沒有在pdf_document工作。它只是輸出HTML代碼。感謝分享! (編輯:實際上它顯示爲代碼甚至html_document我不知道爲什麼我們的結果不同。) – Jeff 2015-01-18 21:54:21

+0

設法得到它的HTML工作通過移除'\ t's,但遺憾的是仍然沒有在pdf_document – Jeff 2015-01-18 23:07:48

6

@Yihui應該獲得榮譽,這一點。直接從printr包:

# BEGINNING of Rmd file: 

```{r echo=FALSE} 
# devtools::install_github("yihui/printr") 
require(printr) 

# reproducibility 
set.seed(123) 

# here's a df 
some_data <- 
    data.frame(a=sample(c('up','down'), 10, replace=T), 
      b=sample(c('big','small'), 10, replace=T)) 

table(some_data) 
``` 

# End of Rmd file 

結果:

|a/b | big| small| 
|:----|---:|-----:| 
|down | 5|  1| 
|up | 0|  4| 
+0

對不起,我正在尋找實際的軸標籤,而不是標題行中的另一個條目。它也恰好在我的數據上看起來[bad](http://i.imgur.com/PujdBWl.png),並且只允許我使用data.table列名(而不是字符串) – Jeff 2015-01-18 21:50:34

相關問題