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')
The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.
Kable is a 3PL Provider Who Helps E-Retailers, Subscription Box Companies, and Consumer Brands Succeed Wherever You Sell-Your Website, Amazon, or Online Marketplaces.
Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.
@Yihui should get the credit for this. Straight from the printr
package:
# 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
Results in:
|a/b | big| small|
|:----|---:|-----:|
|down | 5| 1|
|up | 0| 4|
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With