Talking R Markdown, probably the main distinction between writing a reproducible report of your analysis or a journal article is the journal template and layout which is often provided as a LaTeX template by the editors. In my view, if you want to write your robust and reproducible paper in R today, you are faced with two options based on whether the journal you want to submit to has a LaTex template already built in R Markdown.

Ideal options: the journal you want has already a LaTex template in R Markdown

The rticles package

Have a look at the rticles package to see if your journal is listed there. This package is a collaborative effort to create Rmd LaTeX templates for various journals.

install.packages("rticles")

# Or the dev version
# devtools::install_github("rstudio/rticles")

library(rticles)

The BiocWorkflowTools package: F1000 LaTex template for Bioconductor workflows

If you work in Bioinformatics and you developed a Bioconductor workflow then you might want to submit your paper to F1000 and the BiocWorkflowTools is exactly what you need!

if (!requireNamespace("BiocManager", quietly=TRUE))
    install.packages("BiocManager")
BiocManager::install('BiocWorkflowTools')
library(BiocWorkflowTools)

Load your template in a click and start writing!

Once you found your journal, create a new Rmd and choose your template as in Figure 1!

Choose your `rticles` template.

Figure 1: Choose your rticles template.

The journal template will offer all the necessary examples to show you how to cross reference sections, figures, tables and equations as well as you can check the final result by knitting the document.

Let’s try an example!

My case: the journals I wanted to submit did not have an Rmd LaTex template. No drama!

My mind-workflow to setup myself to write a paper in Rmd

The first thing that I did was to decide what I wanted.

  • I had a few journals in mind but not one defined: I needed a general structure (main manuscript with sections, additional files)

  • I wanted a robust way to cross-reference figures/tables/euqations/sections

  • I wanted to be able to output the article in both pdf, word and LaTex to come towards my co-authors and how they are used to give feedbacks

  • I wanted to include a bibliography files

  • I wanted to be able to create a file of additional information where tables and figures would be numbered distinctly from the main manuscript (e.g. S1, S2 etc…)

bookdown is the answer!

Reports, articles and books can be easily generated using the library(bookdown). This package was written to allow a flexible and comprehensve environment to author books in R and the author, Yihui Xie, made the same framework available to write single documents. Writing everyday reproducible documents or journal articles requires a very similar infrastructure and, at the end of the day, there is very little substantial difference between them. Yihui wrote a super comprehensive book https://bookdown.org/yihui/bookdown/ about all things bookdown! Once you master one type of document, you are pretty well set to master them all!

Minimal article with bookdown::pdf_document2:

In the bookdown-article-minimal-example/ folder I created a minimal example to create an article with bookdown.

YAML setup

The YAML is what you need to define the layout or theme of your documents. You can also add plain LaTex code in here and tell bookdown how you want your Rmd files to be knitted together. By default, bookdown bind together files in numerical order. For example, see the Rmd files in the bookdown-article-minimal-example/ folder. The firt part would be index.Rmd, followed by 02-Additional_files and 03-References.Rmd.

Allow the bookdown cross-referencing approach

---
title: "A minimal paper"
author: "Anna Quaglieri"
output: 
  bookdown::pdf_document2:
    toc: no
    keep_tex: true
---

Add abstract, authors and bibliography

---
title: "A minimal paper"
author: "Anna Quaglieri"
output: 
  bookdown::pdf_document2:
    toc: no
    keep_tex: true
    
author: |
  | Author 1^[Corresponding author: email@email.com] $^1$, Author 2 $^1$, Author 3 $^2$
  | $^1$Affiliation1,  $^2$Affiliation2

abstract: |
  Your abstract goes here...

bibliography: biblio.bib

---

Allow to split the article in separate files and combine them when knitting: bookdown::bookdown_site

---
title: "A minimal paper"
author: "Anna Quaglieri"

site: bookdown::bookdown_site

output: 
  bookdown::pdf_document2:
    toc: no
    keep_tex: true
    
author: |
  | Author 1^[Corresponding author: email@email.com] $^1$, Author 2 $^1$, Author 3 $^2$
  | $^1$Affiliation1,  $^2$Affiliation2

abstract: |
  Your abstract goes here...

bibliography: biblio.bib

---

Allow re-numbering of files in supplementary files

I found the following LaTex code on this StackOverflow thread.

---
title: "A minimal paper"
author: "Anna Quaglieri"
output: 
  bookdown::pdf_document2:
    toc: no
    keep_tex: true
    
author: |
  | Author 1^[Corresponding author: email@email.com] $^1$, Author 2 $^1$, Author 3 $^2$
  | $^1$Affiliation1,  $^2$Affiliation2

abstract: |
  Your abstract goes here...

bibliography: biblio.bib

fontsize: 12pt
header-includes: 
  \usepackage{float} \floatplacement{figure}{H} 
  \newcommand{\beginsupplement}{\setcounter{table}{0}  \renewcommand{\thetable}{S\arabic{table}} \setcounter{figure}{0} \renewcommand{\thefigure}{S\arabic{figure}}}
  \usepackage{setspace}\doublespacing
  \usepackage{lineno}
  \linenumbers
---

Some extra LaTex options: line numbering and spacing!

  • Try removing the following from the YAML!
---
\usepackage{setspace}\doublespacing
\usepackage{lineno}
\linenumbers
---
  • Try adding the LaTex command \usepackage{endfloat} after header-includes: in the YAML

The body of your article

Now that you are setup with the YAML stucture you can start populating your manuscript with for figures, tables, equations, code and texts! In the minimal example that I created I created two separated Rmd files for the main manuscript and the additional files. This is because the manuscript could become really long and you might be better of splitting the two parts.

Approaches to real-time collaboration with co-authors

  • Setup a GitHub account and push/pull changes with your collaborators
  • Checkout Overleaf for real-time collaboration on LaTex file. Have a read at this blog post that discuss working with Git, RStudio and Overleaf at the same time. The drawback is that, while it is easy to go from Rmd to .tex with the YAML option keep_tex: true, it is not the same on the other way around. However, it is worth checking it out since it offers plenty of journal LaTex templates!

Aknowledgements

 

Written by Anna Quaglieri