Last active
April 29, 2018 13:16
-
-
Save tomhopper/5c0f1e22805c4498113c584d84352377 to your computer and use it in GitHub Desktop.
Example of creating captions and automatic numbering in an RMarkdown document.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The _captioner_ library makes this work. | |
```{r libraries} | |
library(captioner) | |
``` | |
Before captioning any tables, we have to set up the table captions and the numbering using `captioner::captioner()`. _captioner_ numbers tables in the order they appear in this code block, so _tab_curve_ will be table 1, and _tab_comp_ will be table 2, wherever they appear in the document. | |
```{r setup_captions} | |
table_nums <- captioner(prefix = "Table") | |
tab_curve_cap <- table_nums(name = "tab_curve", | |
caption = "Displayed caption for curve table.") | |
tab_comp_cap <- table_nums(name = "tab_comp", | |
caption = "Displayed caption for comp table.") | |
``` | |
We can then begin captioning tables using the _captioner_ function that we created. Here, we just caption a Markdown table. I've added the `<span>` tag to format the caption differently than the rest of the body text. The css class is added at the end of the RMarkdown document to ensure the page loads as quickly as possible. | |
<span class="caption">`r table_nums('tab_curve')`</span> | |
| header | 0 | 1 | 2 | 3 | 4 | | |
|:-------|:-:|:--:|:--:|:--:|:---:| | |
| value | 0 | 20 | 40 | 60 | 80 | | |
Here's another table, this one generated by _R_. | |
<span class="caption">`r table_nums('tab_comp')`</span> | |
```{r, fig.cap=tab_comp_cap, echo=FALSE} | |
result_df %>% | |
mutate(mean = round(mean, 2)) %>% | |
kable() | |
``` | |
Finally, the css defining formatting for the captions. | |
<style type = "text/css"> | |
span.caption { color: #999999; font-size: 90% } | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment