Basic R markdown template for writing a scientific manuscript

Last update on Sept. 6, 2017.

RStudio is a great environment for writing and R Markdown reports, manuscripts, blogs and other types of publications. It is particularly attractive for writing manuscripts that are reproducible (see for example: https://www.r-bloggers.com/composing-reproducible-manuscripts-using-r-markdown/).

However, it can be a bit daunting at first, so here's a simple template to get you started on a scientific paper. It produces a document with lines numbered, double line-spacing, references, a list of figures, figures on separate pages at the end of the document, a list of tables and finally tables on separate pages at the end of the document.


You will first need the YAML part as follows:

---
title: Your title here!


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: Path to your .bib file
csl: Path to your .csl file #sets the style of your bibliography

output:
bookdown::pdf_document2:
toc: no
fig_caption: yes

always_allow_html: yes

# These are LaTex settings to take care of floating figures/tables, line spacing, etc
header-includes:
- \usepackage{endfloat}    
- \usepackage{setspace}\doublespacing
- \usepackage{lineno}
- \linenumbers
---


Some additional settings to start your document (add a R snippet after the YAML):

  ```{r Setup..., message=FALSE, warning=FALSE, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'p') # Places figures on their own pages
knitr::opts_chunk$set(out.width = '100%', dpi=300)

# I usually load my libraries up front to keep things organized
library(bookdown)
library(knitr)
library(kableExtra)
library(ggplot2)
library(ggthemes)
library(ggExtra)
library(dplyr)
library(stringr)

```

From here it's normal R markdown, but with a bookdown flavour. Here are some pointers:

  • Headers are specified with #, ##, ###, etc. for H1, H2, H3, etc.
  • When you would like to cite a paper, your citation looks like this: [@Key] - e.g. [@author2017]
  • If you would like to add a figure, this can be done with an R snippet:

    ```{r figRef, echo=FALSE, message=FALSE, warning=FALSE, fig.cap="Figure caption here"} ## R code here, with plot output at end, for example using ggplot2

    ```

  • To cross-reference figures in your text, simply write something like: As shown in Figure  \@ref(fig:figRef) ...
  • Tables can be added using the kable function from knitr, for example:

    ```{r tabRef, echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE, results="asis"} df <- read.csv(...)

    knitr::kable(df, caption = "Table caption here...", booktabs = TRUE) ```

  • Cross-referencing a Table is similar to that of a Figure. For example: Table \@ref(tab:tabRef) shows the data...
  • Put #References at the end of your document to generate the bibliography.

And don't forget to have a look here for more resources: http://rmarkdown.rstudio.com/

Previous entry

Related entries

Similar entries

Pingbacks

Pingbacks are closed.

Comments

No comments yet.

Post your comment