library(knitr)
library(pander)
library(flextable)

knitr::opts_chunk$set()

data <- head(iris)

Kable

See Table 1.

kable(data, caption = "Kable table caption.")
Table 1: Kable table caption.
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Pander

See Table 2.

pandoc.table(data, caption = "(\\#tab:pander-table) Pander table caption.")
Table 2: Pander table caption.
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Flextable

See Table 3.

table_caption <- function(tag, caption) {
  if (is.null(knitr::opts_knit$get('rmarkdown.pandoc.to'))) {
    cat("")
  } else if (grepl("html", knitr::opts_knit$get('rmarkdown.pandoc.to'))) {
    cat("<table style=\"width:100%;\"><caption>", 
        "(\\#tab:", tag, ") ", caption, 
        "</caption></table>", sep = "")
  } else {
    cat("Table: (\\#tab:", tag, ") ", caption, sep = "")
    cat("", "", "+---------:+", 
                "|          |", 
                "+----------+", "", "", sep = "\n")
  }
}

table_caption("flextable-table", "Flextable table caption.")
Table 3: Flextable table caption.
flextable(data)

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa