
Who this post is for: This post is for R users who want to use R Markdown (Rmd) scripts to create Word (docx) documents and would like to programmatically insert headings, outputs and Word cross-references. Automating such routines could be a good way to reduce the likelihood of human errors that often arise while performing numerous repetitive tasks and divert resources from boring copy/paste type of exercises to challenging problem solving. Why using R and R Markdown : As data scientists, we often have repeated analysis to perform (example: validation of analytical methods). An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code. What is R Markdown : R Markdown is a tool designed to help create reproducible, dynamic reports with R. What is R: R language is widely used among statisticians and data miners for developing data analysis software. f.Photo from Maarten van den Heuvel on Unsplash Using a regular expression, the function returns all characters of the table_nums('tab_1') output located before the first colon. and table number), I have written the function f.ref. In order to return the desired output (prefix Tab. 1: German Bundesliga: Final Table 2015/16, Position 7-12” Otherwise, we wi’ll get the following output: “Tab.
RMARKDOWN REFERENCE CODE
However, we can not just use the inline code table_nums('tab_1') Since we have received a numbered table, it should also be possible to reference the table. 1: German Bundesliga: Final Table 2015/16, Position 7-12 Position It is important to separate inline code and code chunk. With fig.cap = tab.1_cap, we specify the caption of the first table.

The next code snippet combines both inline code and a code chunk.

table_nums <- captioner::captioner(prefix = "Tab.")Ĭaption = "German Bundesliga: Final Table 2015/16, Position 7-12")Ĭaption = "German Bundesliga: Final Table 2015/16, Position 12-18") Furthermore, we may also define a prefix ( Tab. In a first step, we define a function named table_nums and apply it to the tables’ name and caption. Thanks to Alathea Letaw’s captioner package, we can number tables and figures. German Bundesliga: Final Table 2015/16, Position 1-6 Position As we can see, the caption will not be numbered and, thus, cannot be referenced in the document. With caption we can specify a simple table caption. In the first code snippet, we create a table using the kable function of the knitr package. Pacman::p_load(knitr, captioner, bundesligR, stringr)

if (!require("pacman")) install.packages("pacman") It contains “all final tables of Germany’s highest football league, the Bundesliga” ( Link).

The dataset I will be using in this blog post is named bundesligR and part of the bundesligR package.
RMARKDOWN REFERENCE INSTALL
The following code will install load and / or install the R packages required for this blog post.
RMARKDOWN REFERENCE HOW TO
In this blog post, I will explain how to number and reference tables and figures in R Markdown files using the captioner package. However, since bookdown uses LaTex functionality, R Markdown files created with bookdown cannot be converted into MS Word (.docx) files. The recently published bookdown package makes it very easy to number and reference tables and figures ( Link). Unfortunately, R Markdown has no “native” method to number and reference table and figure captions. However, in scientific research papers or reports, tables and figures usually need to be numbered and referenced. R Markdown is a great tool to make research results reproducible.
