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)
# 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')
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> </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:
The generated html is somewhat readable, too:
<table>
<tr><td> </td><td>b</td><td> </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>