John Smith
John Smith

Reputation: 2876

Adding a caption to a Kable Table in R Markdown

I am using RMarkdown to write some university work I have created a table using the code below and need to export it to Microsoft Word

Can anyone tell me how to add a label describing the data in the table or do I have to do this in MS Word afterwards

    ---
    title: "Data"
    author: "foo"
    date: "2 July 2016"
    output:
      word_document:
    bibliography: bibliography.bib
    ---

    kable(table(Rawdata$Var1, Rawdata$Var2))

Upvotes: 10

Views: 45397

Answers (1)

Benjamin
Benjamin

Reputation: 17359

Use the caption argument in kable.

If you want more options for formatting tables in Word output, I would recommend downloading the Gmisc package and using the Gmisc::docx_document format. Just keep in mind that it will save the file as an HTML file that can be opened as a Word Document and preserve the formatting.

For example:

---
title: "Data"
author: "foo"
date: "2 July 2016"
output:  Gmisc::docx_document
---

```{r}
knitr::kable(head(mtcars[, 1:3]),
             caption = "This is the table caption")
```

Upvotes: 20

Related Questions