Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R markdown table with a caption

Tags:

I'm trying to create a table outside of a code chunk using normal markdown notation and to add a caption to it. Here's an example file (taken from here:

--- output: pdf_document ---  | First Header  | Second Header | Third Header         | | :------------ | :-----------: | -------------------: | | First row     | Data          | Very long data entry | | Second row    | **Cell**      | *Cell*               | | Third row     | Cell that spans across two columns  || [Table caption, works as a reference][section-mmd-tables-table1] 

This unfortunately produces a rather sad string: Non-table sadness with caption

Removing the caption line in square brackets produces the table fine (but without the caption, obviously): Nice table no caption

This can be achieved if I made my data into an actual data.frame and used kable as shown here but I'm looking to avoid having to do this.

like image 316
James Owers Avatar asked Nov 27 '15 22:11

James Owers


People also ask

How do you put a title on a Kable table?

You can add a caption to the table via the caption argument, e.g. (see Table 10.1 for the output), knitr::kable(iris2, caption = "An example table caption.")

How do I insert a table in r markdown?

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”.


1 Answers

The linked guide refers to MultiMarkdown, while RMarkdown uses Pandocs. Captions work a little bit differently in Pandoc. The following should do the trick. The syntax is Table: followed by your caption; Pandocs numbers automatically. Leave one line blank between the end of the table and the caption line.

--- output: pdf_document ---  | First Header  | Second Header | Third Header         | | :------------ | :-----------: | -------------------: | | First row     | Data          | Very long data entry | | Second row    | **Cell**      | *Cell*               | | Third row     | Cell that spans across two columns  ||  Table: Your Caption 
like image 146
Chris C Avatar answered Oct 24 '22 19:10

Chris C