Preserve row/column labels from table() using kable and knitr

arvi1000 picture arvi1000 · Oct 17, 2014 · Viewed 7.9k times · Source

The table function in base R adds nice row/column labels, but when I use knitr::kable these go away. Any simple way to keep these, aside from adding them on the html/markdown side?

Reproducible example:

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

Answer

arvi1000 picture arvi1000 · Jan 18, 2015

Neat that someone else posted a bounty on my old question. Anyway, if it's helpful, my solution was a homebrew html generator function

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

Now this markdown 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))
```

Produces the results I had wanted:

enter image description here

The generated html is somewhat readable, too:

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