How to make boss looking tables in Rmarkdown using xtable.

Ugly

head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10

Pretty

library(xtable)

pretty_table <- function(df, ...) {
  print(xtable(df, ...), 
      type="html",
      include.rownames = FALSE,
      html.table.attributes = getOption("xtable.html.table.attributes", "border=0"), 
      ...)  
}

pretty_table(head(cars))
speed dist
4.00 2.00
4.00 10.00
7.00 4.00
7.00 22.00
8.00 16.00
9.00 10.00

Why it works

xtable produces html table markup (<table...>). When the {r results='asis'} markdown option is set, then this will render as actual html.

Rock on, rockstar.