Writing your report in Word from R

Students and researchers from math/applied math fields prefer writing their reports/papers using Latex. I'm in that case. However, Latex is still not widely used in many fields and the majority of workers in companies and institutions have never heard about it. It may thus happen that writing in Tex is not an option, especially if the team members don't have the time or are not willing to learn this language.

In this case, MS word will most likely be imposed on you in order to enable efficient collaboration with your colleagues. This can be a waste of time for you if you are used to working in Tex.

This post aims to give you a short introduction to how to write your reports using .Rmd files, including some tips and additional resources. It will makes it easier for you to include MS Word into your R workflow, erasing the pain to use it instead of Tex.

This post assumes that you have basic knowledge in both R, Tex, and .Rmd. I urge to start using .Rmd if you are still writing your report manually, this will change your life and make you way more efficient.

Step 1. Set up your projects

I am going to make the assumption that your project is organized in the same way as presented here.

In the files_for_report folders, create two additional folders:

  • draft_history: it aims to contain draft versions of the report;

  • images: it aims to contain additional images that will be included in the document ;

Step 2. Create your bibliography file

To make it short, bibliography management stands on the same as in Tex, using a .bib files.

Once you did the first step, open the MS notepad and save it as references.bib in the folder.

All the references used in the report should be reported in this document. When citing an academic article, an easy way to create the bib entry is to use the DOI to generate the entry using this tool https://doi2bib.org/. For example, plugging the DOI from Ioannidis' 2005 article 'Why Most Published Research Findings Are False' (https://doi.org/10.1371/journal.pmed.0020124) into the tool yields the following bib entry that should be paste into the .bib file:

@article{Ioannidis2005, doi = {10.1371/journal.pmed.0020124}, url = {https://doi.org/10.1371/journal.pmed.0020124}, year = {2005}, month = aug, publisher = {Public Library of Science ({PLoS})}, volume = {2}, number = {8}, pages = {e124}, author = {John P. A. Ioannidis}, title = {Why Most Published Research Findings Are False}, journal = {{PLoS} Medicine}}

Step 3. Create your style document

The last file to be created is the styling document. For example, see MS' tuto here. Especially Title styles and Text styles are those that are the most important to define.

At the end of this step, your files_for_report folder should look like this:

Step 4. Create your .Rmd file

Create a .Rmd script using the R studio console: File>New File>R Markdown

Choose "Word" and let the title and author spaces blank. Click on 'Ok'. Your lines 1 to 4 should look like this:


title: "Untitled"

output: word_document


Replace them with the following code:


title: "Your Title"

author: "Your Name"

date: "`r format(Sys.time(), '%d/%m/%Y')`"

lang: en

bibliography: "files_for_report/references.bib"



fig_caption: yes

number_sections: yes

reference_docx: "files_for_report/reference_styling.docx"

toc_depth: 5


Step 4. Prepare the knitting by importing packages, functions, and data

Load the packages used in the project, the functions that you defined, and the data.

```{r "Load packages, functions, and data", echo=FALSE, message=FALSE, warning=FALSE, include = FALSE, results='hide'}

##Load of the packages


##Load of the functions


##Load of the data

###Load your dataset here


Step 5. Define the default setting for your packages

Define the default settings for your packages. For example, if you want to use GGplot2 for producing your figure, you may want to keep the same theme all along.

```{r "Default parameters choice", echo=FALSE, message=FALSE, warning=FALSE}

###For ggplot


###Chunks size for graphics

knitr::opts_chunk$set(fig.width=9.15, fig.height=7)

knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)


Step 6. Adding tables of contents, figures, and tables and references

If you want to add tables (contents, figures, tables) and references, add the following lines of codes after that you defined default parameters for the packages.

# Table of content{-}

```{r tableofcontents, echo=FALSE, message=FALSE, warning=FALSE}




# Table of figures{-}

```{r tableoffigures, echo=FALSE, message=FALSE, warning=FALSE}

block_toc(style = "Image Caption")



# Table of tables{-}

```{r tableoftable, echo=FALSE, message=FALSE, warning=FALSE}

block_toc(style = "Table Caption perso")





# References{-}

At the end of this step, your report is ready to be written, replacing the "HERE IS YOUR TEXT" text. To produce the MS Word from the .Rmd, you just have to click on the 'knit' button.

How to create a new section ?

  • To define a new section use # (level 1), ## (level 2), ### (level 3), etc. There should be a space between the # and your section name;

  • If you want to add a section without a number, add {-} at the end of your title;

How to make a reference to a table ?

Tables can be displayed using the Flextable package (see here). For example, the code below first use the gtsummary package (see here) and then the flextable package to create a descriptive statistic table for 3 variables (Age, Marker, Grade) both overall and by treatment arms.

```{r referenceofthetable, echo=FALSE, message=FALSE, warning=FALSE}

trial %>%

select(trt, age, marker, grade) %>%


by = "trt",

type = all_continuous() ~ "continuous2",

statistic = list(

all_continuous() ~ c("{N_nonmiss} ({p_nonmiss}%)",

"{mean} ({sd})",

"{median} ({p25};{p75})",


all_categorical() ~ "{n} ({p}%)"),

missing = "no"

) %>%

add_p(test = list(all_continuous() ~ "wilcox.test",

all_categorical() ~ "fisher.test"),

pvalue_fun = ~style_pvalue(.x, digits = 2))%>%

modify_header(c("stat_1", "stat_2") ~ "{level} \n N = {n} ({style_percent(p)}%)") %>%

modify_header(label ~ "**Variables**") %>%

add_overall() %>%

modify_header(stat_0 = "All patients \n N = {N}") %>%

modify_footnote(stat_0 ~ NA)

gtsummary::as_flex_table() %>%

fontsize(size = 10, part = "header") %>%

fontsize(size = 8, part = "body") %>%

fontsize(size = 7, part = "footer") %>%


caption = "An example for descriptive statistics table",

style = "Table caption perso"

###This is a style defined in the style reference document

) %>%

width(width = 1.1)


To make a reference to this table in the text, use the code \@ref(tab:referenceofthetable).

How to make a reference to a figure ?

For example, if we use GGplot2 to make the following figure:

```{r exampleofplot, message=FALSE, warning=FALSE, paged.print=FALSE, fig.cap="An example of plot"}

# create a dataset

data <- data.frame(

name=c( rep("A",500), rep("B",500), rep("B",500), rep("C",20), rep('D', 100) ),

value=c( rnorm(500, 10, 5), rnorm(500, 13, 1), rnorm(500, 18, 1), rnorm(20, 25, 4), rnorm(100, 12, 1) )


# Most basic violin chart

ggplot(data, aes(x=name, y=value, fill=name)) + # fill=name allow to automatically dedicate a color for each group



It is possible to make a reference to the plot using the code \@ref(fig:exampleofplot).

How to make a reference to a .bib input ?

To call a reference from the .bib file, you can use either @ref1 if you want to cite something without brackets or [@ref1] if you want to add them. You can cite multiple references like this [@ref1; @ref2; @ref3].

For example, Ioannidis' paper could be cited using the code @Ioannidis2005.

You can download a working example (including the folder structure) here.