R Markdown is a great tool to make research results reproducible. However, in scientific research papers or reports, tables and figures usually need to be numbered and referenced. Unfortunately, R Markdown has no “native” method to number and reference table and figure captions. The recently published bookdown package makes it very easy to number and reference tables and figures (Link). However, since bookdown uses LaTex functionality, R Markdown files created with bookdown cannot be converted into MS Word (.docx) files.

In this blog post, I will explain how to number and reference tables and figures in R Markdown files using the captioner package.

Packages required

The following code will install load and / or install the R packages required for this blog post. The dataset I will be using in this blog post is named bundesligR and part of the bundesligR package. It contains “all final tables of Germany’s highest football league, the Bundesliga” (Link).

if (!require("pacman")) install.packages("pacman")
pacman::p_load(knitr, captioner, bundesligR, stringr)

In the first code snippet, we create a table using the kable function of the knitr package. With caption we can specify a simple table caption. As we can see, the caption will not be numbered and, thus, cannot be referenced in the document.

German Bundesliga: Final Table 2015/16, Position 1-6

PositionTeamPointsGD
1FC Bayern Muenchen8863
2Borussia Dortmund7848
3Bayer 04 Leverkusen6016
4Borussia Moenchengladbach5517
5FC Schalke 04522
61. FSV Mainz 05504

Table numbering

Thanks to Alathea Letaw’s captioner package, we can number tables and figures.
In a first step, we define a function named table_nums and apply it to the tables’ name and caption. Furthermore, we may also define a prefix (Tab. for tables and Fig. for figures).

table_nums <- captioner::captioner(prefix = "Tab.")

tab.1_cap <- table_nums(name = "tab_1", 
                        caption = "German Bundesliga: Final Table 2015/16, Position 7-12")
tab.2_cap <- table_nums(name = "tab_2", 
                        caption = "German Bundesliga: Final Table 2015/16, Position 12-18")

The next code snippet combines both inline code and a code chunk. With fig.cap = tab.1_cap, we specify the caption of the first table. It is important to separate inline code and code chunk. Otherwise the numbering won’t work.

Tab. 1: German Bundesliga: Final Table 2015/16, Position 7-12

PositionTeamPointsGD
7Hertha BSC500
8VfL Wolfsburg45-2
91. FC Koeln43-4
10Hamburger SV41-6
11FC Ingolstadt 0440-9
12FC Augsburg38-10

Table referencing

Since we have received a numbered table, it should also be possible to reference the table. However, we can not just use the inline code

table_nums('tab_1')

Otherwise, we wi’ll get the following output:

[1] “Tab. 1: German Bundesliga: Final Table 2015/16, Position 7-12”

In order to return the desired output (prefix Tab. and table number), I have written the function f.ref. Using a regular expression, the function returns all characters of the table_nums('tab_1') output located before the first colon.

f.ref <- function(x) {
  stringr::str_extract(table_nums(x), "[^:]*")
}

When we apply this function to tab_1, the inline code returns the following result:

Just to make the table complete, Tab. 2 shows positions 13 to 18 of the final Bundesliga table.

knitr::kable(bundesligR::bundesligR[c(13:18), c(2,3,11,10)],
             align = c('c', 'l', 'c', 'c'),
             row.names = FALSE)

Tab. 2: German Bundesliga: Final Table 2015/16, Position 12-18

PositionTeamPointsGD
13Werder Bremen38-15
14SV Darmstadt 9838-15
15TSG 1899 Hoffenheim37-15
16Eintracht Frankfurt36-18
17VfB Stuttgart33-25
18Hannover 9625-31

And what about figures?

Figures can be numbered and referenced following the same principle.

I hope you find this post useful and If you have any question please post a comment below. You are welcome to visit my personal blog Scripts and Statistics for more R tutorials.