<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-markdown-how-to-number-and-reference-tables/"></div></div><div><a data-url="https://www.r-bloggers.com/r-markdown-how-to-number-and-reference-tables/" data-counturl="https://www.r-bloggers.com/r-markdown-how-to-number-and-reference-tables/" data-count="horizontal" data-text="R Markdown: How to number and reference tables" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascienceplus.com/r-markdown-how-to-number-and-reference-tables/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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 <code>bookdown</code> package makes it very easy to number and reference tables and figures (<a target="_blank" rel="nofollow" href="https://bookdown.org/yihui/bookdown/">Link</a>). However, since <code>bookdown</code> uses LaTex functionality, R Markdown files created with <code>bookdown</code> cannot be converted into MS Word (.docx) files.</p>
<p>In this blog post, I will explain how to number and reference tables and figures in R Markdown files using the <code>captioner</code> package.</p>
<h2>Packages required</h2>
<p>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” (<a target="_blank" rel="nofollow" href="https://github.com/ottlngr/bundesligR">Link</a>).</p>
<pre>
if (!require(&quot;pacman&quot;)) install.packages(&quot;pacman&quot;)
pacman::p_load(knitr, captioner, bundesligR, stringr)
</pre>
<p>In the first code snippet, we create a table using the <code>kable</code> function of the <code>knitr</code> package. With <code>caption</code> 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.</p>
<p>German Bundesliga: Final Table 2015/16, Position 1-6</p>
<table>
<thead>
<tr>
<th align="center">Position</th>
<th align="left">Team</th>
<th align="center">Points</th>
<th align="center">GD</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">1</td>
<td align="left">FC Bayern Muenchen</td>
<td align="center">88</td>
<td align="center">63</td>
</tr>
<tr>
<td align="center">2</td>
<td align="left">Borussia Dortmund</td>
<td align="center">78</td>
<td align="center">48</td>
</tr>
<tr>
<td align="center">3</td>
<td align="left">Bayer 04 Leverkusen</td>
<td align="center">60</td>
<td align="center">16</td>
</tr>
<tr>
<td align="center">4</td>
<td align="left">Borussia Moenchengladbach</td>
<td align="center">55</td>
<td align="center">17</td>
</tr>
<tr>
<td align="center">5</td>
<td align="left">FC Schalke 04</td>
<td align="center">52</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">6</td>
<td align="left">1. FSV Mainz 05</td>
<td align="center">50</td>
<td align="center">4</td>
</tr>
</tbody>
</table>
<h2>Table numbering</h2>
<p>Thanks to Alathea Letaw’s <code><a target="_blank" rel="nofollow" href="https://github.com/adletaw/captioner">captioner</a></code> package, we can number tables and figures.<br>
In a first step, we define a function named <code>table_nums</code> and apply it to the tables’ name and caption. Furthermore, we may also define a prefix (<em>Tab.</em> for tables and <em>Fig.</em> for figures).</p>
<pre>
table_nums &lt;- captioner::captioner(prefix = &quot;Tab.&quot;)

tab.1_cap &lt;- table_nums(name = &quot;tab_1&quot;, 
                        caption = &quot;German Bundesliga: Final Table 2015/16, Position 7-12&quot;)
tab.2_cap &lt;- table_nums(name = &quot;tab_2&quot;, 
                        caption = &quot;German Bundesliga: Final Table 2015/16, Position 12-18&quot;)
</pre>
<p>The next code snippet combines both inline code and a code chunk. With <code>fig.cap = tab.1_cap</code>, we specify the caption of the first table. It is important to separate inline code and code chunk. Otherwise the numbering won’t work.</p>
<p><a title="" data-rel="lightbox-0" target="_blank" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/09/captioner1.png"><img sizes="(max-width: 588px) 100vw, 588px" alt="" class="wp-image-4744" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/09/captioner1.png?resize=456%2C95" data-recalc-dims="1"></a></p>
<p>Tab. 1: German Bundesliga: Final Table 2015/16, Position 7-12</p>
<table>
<thead>
<tr>
<th align="center">Position</th>
<th align="left">Team</th>
<th align="center">Points</th>
<th align="center">GD</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">7</td>
<td align="left">Hertha BSC</td>
<td align="center">50</td>
<td align="center">0</td>
</tr>
<tr>
<td align="center">8</td>
<td align="left">VfL Wolfsburg</td>
<td align="center">45</td>
<td align="center">-2</td>
</tr>
<tr>
<td align="center">9</td>
<td align="left">1. FC Koeln</td>
<td align="center">43</td>
<td align="center">-4</td>
</tr>
<tr>
<td align="center">10</td>
<td align="left">Hamburger SV</td>
<td align="center">41</td>
<td align="center">-6</td>
</tr>
<tr>
<td align="center">11</td>
<td align="left">FC Ingolstadt 04</td>
<td align="center">40</td>
<td align="center">-9</td>
</tr>
<tr>
<td align="center">12</td>
<td align="left">FC Augsburg</td>
<td align="center">38</td>
<td align="center">-10</td>
</tr>
</tbody>
</table>
<h2>Table referencing</h2>
<p>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 </p>
<pre>table_nums('tab_1')</pre>
<p>. </p>
<p>Otherwise, we wi’ll get the following output:</p>
<pre><em>[1] “Tab. 1: German Bundesliga: Final Table 2015/16, Position 7-12”</em></pre>
<p>In order to return the desired output (prefix <em>Tab.</em> and table number), I have written the function <em>f.ref</em>. Using a regular expression, the function returns all characters of the <code>table_nums('tab_1')</code> output located <em>before</em> the first colon.</p>
<pre>
f.ref &lt;- function(x) {
  stringr::str_extract(table_nums(x), &quot;[^:]*&quot;)
}
</pre>
<p>When we apply this function to <em>tab_1</em>, the inline code returns the following result:</p>
<blockquote><p><strong>Inline code</strong>: As we can see in <code>f.ref(&quot;tab_1&quot;)</code>, the Berlin based football club <em>Hertha BSC</em> had position seven in the final table.</p>
<p><strong>Result</strong>: As we can see in Tab. 1, the Berlin based football club <em>Hertha BSC</em> had position seven in the final table.</p>
</blockquote>
<p>Just to make the table complete, Tab. 2 shows positions 13 to 18 of the final Bundesliga table.</p>
<pre>
knitr::kable(bundesligR::bundesligR[c(13:18), c(2,3,11,10)],
             align = c('c', 'l', 'c', 'c'),
             row.names = FALSE)
</pre>
<p>Tab. 2: German Bundesliga: Final Table 2015/16, Position 12-18</p>
<table>
<thead>
<tr>
<th align="center">Position</th>
<th align="left">Team</th>
<th align="center">Points</th>
<th align="center">GD</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">13</td>
<td align="left">Werder Bremen</td>
<td align="center">38</td>
<td align="center">-15</td>
</tr>
<tr>
<td align="center">14</td>
<td align="left">SV Darmstadt 98</td>
<td align="center">38</td>
<td align="center">-15</td>
</tr>
<tr>
<td align="center">15</td>
<td align="left">TSG 1899 Hoffenheim</td>
<td align="center">37</td>
<td align="center">-15</td>
</tr>
<tr>
<td align="center">16</td>
<td align="left">Eintracht Frankfurt</td>
<td align="center">36</td>
<td align="center">-18</td>
</tr>
<tr>
<td align="center">17</td>
<td align="left">VfB Stuttgart</td>
<td align="center">33</td>
<td align="center">-25</td>
</tr>
<tr>
<td align="center">18</td>
<td align="left">Hannover 96</td>
<td align="center">25</td>
<td align="center">-31</td>
</tr>
</tbody>
</table>
<h2>And what about figures?</h2>
<p>Figures can be numbered and referenced following the same principle.</p>
<p>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 <a target="_blank" rel="nofollow" href="https://scriptsandstatistics.wordpress.com/">Scripts and Statistics</a> for more R tutorials.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toRDBL – manipulate data in-database with R code only" target="_blank" rel="bookmark" href="http://datascienceplus.com/rdbl-manipulate-data-in-database-with-r-code-only/">RDBL – manipulate data in-database with R code only</a></li>
<li><a title="Permanent Link toR Markdown: How to format tables and figures in .docx files" target="_blank" rel="bookmark" href="http://datascienceplus.com/r-markdown-how-to-format-tables-and-figures-in-docx-files/">R Markdown: How to format tables and figures in .docx files</a></li>
<li><a title="Permanent Link toR Markdown: How to insert page breaks in a MS Word document" target="_blank" rel="bookmark" href="http://datascienceplus.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/">R Markdown: How to insert page breaks in a MS Word document</a></li>
<li><a title="Permanent Link toWorking on Data-Warehouse (SQL) with R" target="_blank" rel="bookmark" href="http://datascienceplus.com/working-on-data-warehouse-sql-with-r/">Working on Data-Warehouse (SQL) with R</a></li>
<li><a title="Permanent Link toImplementing Apriori Algorithm in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/implementing-apriori-algorithm-in-r/">Implementing Apriori Algorithm in R</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=6BPyeE2JWOY:oR5ieK6os7M:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=6BPyeE2JWOY:oR5ieK6os7M:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=6BPyeE2JWOY:oR5ieK6os7M:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=6BPyeE2JWOY:oR5ieK6os7M:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/r-markdown-how-to-number-and-reference-tables/"></div></div><div><a data-url="https://www.r-bloggers.com/r-markdown-how-to-number-and-reference-tables/" data-counturl="https://www.r-bloggers.com/r-markdown-how-to-number-and-reference-tables/" data-count="vertical" data-text="R Markdown: How to number and reference tables" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascienceplus.com/r-markdown-how-to-number-and-reference-tables/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/_hHsYnfr_wU"></a></div>