R markdown table with a caption

拥有回忆 提交于 2019-12-04 16:26:19

问题


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:

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

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.


回答1:


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


来源:https://stackoverflow.com/questions/33965560/r-markdown-table-with-a-caption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!