使用kable和knitr保留table()中的行/列标签

时间:2014-10-17 16:46:00

标签: r markdown knitr xtable

基础R中的table函数添加了不错的行/列标签,但是当我使用knitr::kable时,它们会消失。除了在html / markdown一侧添加它们之外,还有任何简单的方法来保留它们吗?

可重复的例子:

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

4 个答案:

答案 0 :(得分:6)

很遗憾,其他人在我的旧问题上发了一笔赏金。无论如何,如果它有用,我的解决方案是一个自制的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)
}

现在这个降价文档:

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>

答案 1 :(得分: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|

答案 2 :(得分:4)

不是最佳解决方案(因为Pandoc的降价功能不支持col / rowspans),但请尝试pander,它旨在轻松地将R对象转换为markdown(以及一系列选项) ):

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

答案 3 :(得分:1)

虽然有点hacky,但是组合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)