Authoring R Markdown vignettes

19 January 2018

Abstract

Instructions on enabling Bioconductor style in R Markdown vignettes.

Package

BiocStyle 2.7.8

1 Prerequisites

Bioconductor R Markdown format is build on top of R package bookdown, which in turn relies on rmarkdown and pandoc to compile the final output document. Therefore, unless you are using RStudio, you will need a recent version of pandoc (>= 1.17.2). See the pandoc installation instructions for details on installing pandoc for your platform.

2 Getting started

To enable the Bioconductor style in your R Markdown vignette you need to:

Edit the DESCRIPTION file by adding

VignetteBuilder: knitr
Suggests: BiocStyle, knitr, rmarkdown

Specify BiocStyle::html_document or BiocStyle::pdf_document as output format and add vignette metadata in the document header:

The vignette section is required in order to instruct R how to build the vignette.1 The package field which should contain the package name is used to print the package version in the output document header. It is not necessary to specify date as by default the document compilation date will be automatically included. See the following section for details on specifying author affiliations and abstract.

BiocStyle’s html_document and pdf_document format functions extend the corresponding original rmarkdown formats, so they accept the same arguments as html_document and pdf_document, respectively. For example, use toc_float: true to obtain a floating TOC as in this vignette.

2.1 Use with R markdown v1

Apart from the default markdown engine implemented in the rmarkdown package, it is also possible to compile Bioconductor documents with the older markdown v1 engine from the package markdown. There are some differences in setup and the resulting output between these two engines.

The way of attaching CSS files when using markdown differs from how this is done with rmarkdown. In the former case additional style sheets can be used by providing them to the BiocStyle::markdown function. To include custom.css file use

A list of unique affiliations will be displayed below the authors, similar as in this document.

For clarity, compactness, and to avoid errors, repeated nodes in YAML header can be initially denoted by an anchor entered with an ampersand &, and later referenced with an asterisk *. For example, the above affiliation metadata is equivalent to the shorthand notation

The shorttitle option specifies the title used in running headers instead of the document title.2

4 Style macros

BiocStyle introduces the following macros useful when referring to R packages:

Biocpkg("IRanges") for Bioconductor software, annotation and experiment data packages, including a link to the release landing page or if the package is only in devel, to the devel landing page, IRanges.

CRANpkg("data.table") for R packages available on CRAN, including a link to the FHCRC CRAN mirror landing page, data.table.

Githubpkg("rstudio/rmarkdown") for R packages available on GitHub, including a link to the package repository, rmarkdown.

Rpackage("MyPkg") for R packages that are not available on Bioconductor, CRAN or GitHub; MyPkg.

These are meant to be called inline, e.g., `r Biocpkg("IRanges")`.

5 Code chunks

The line length of output code chunks is set to the optimal width of typically 80 characters, so it is not neccessary to adjust it manually through options("width").

6 Figures

BiocStyle comes with three predefined figure sizes. Regular figures not otherwise specified appear indented with respect to the paragraph text, as in the example below.

plot(cars)

Figures which have no captions are just placed wherever they were generated in the R code. If you assign a caption to a figure via the code chunk option fig.cap, the plot will be automatically labeled and numbered3, and it will be also possible to reference it. These features are provided by bookdown, which defines a format-independent syntax for specifying cross-references, see Section ??. The figure label is generated from the code chunk label4 by prefixing it with fig:, e.g., the label of a figure originating from the chunk foo will be fig:foo. To reference a figure, use the syntax \@ref(label)5, where label is the figure label, e.g., fig:foo. For example, the following code chunk was used to produce Figure 1.

Figure 1: Regular figure The first sentence of the figure caption is automatically emphasized to serve as figure title.

In addition to regular figures, BiocStyle provides small and wide figures which can be specified by fig.small and fig.wide code chunk options. Wide figures are left-aligned with the paragraph and extend on the right margin, as Figure 2. Small figures are meant for possibly rectangular plots which are centered with respect to the text column, see Figure 3.

Figure 2: Wide figure A plot produced by a code chunk with option fig.wide = TRUE.

Figure 3: Small figure A plot produced by a code chunk with option fig.small = TRUE.

7 Tables

Like figures, tables with captions will also be numbered and can be referenced. The caption is entered as a paragraph starting with Table:6, which may appear either before or after the table. When adding labels, make sure that the label appears at the beginning of the table caption in the form (\#tab:label), and use \@ref(tab:label) to refer to it. For example, Table 1 has been produced with the following code.

You may then refer to Equation (1) by \@ref(eq:binom). Note that in HTML output only labeled equations will appear numbered.

9 Cross-references

Apart from referencing figures (Section ??), tables (Section ??), and equations (Section ??), you can also use the same syntax \@ref(label) to reference sections, where label is the section ID. By default, Pandoc will generate IDs for all section headers, e.g., # Hello World will have an ID hello-world. In order to avoid forgetting to update the reference label after you change the section header, you may also manually assign an ID to a section header by appending {#id} to it.

When a referenced label cannot be found, you will see two question marks like ??, as well as a warning message in the R console when rendering the document.

10 Margin notes

Footnotes are displayed as side notes on the right margin8, which has the advantage that they appear close to the place where they are defined.

Session info

Here is the output of sessionInfo() on the system on which this document was compiled: