Write replicable papers using the R package “Knitr”

3 minute read

Published:

In this section I will describe my take on literate programming: not the best, not the only one. Just the one I use.

  • Text processor and necessary dependencies: First you will need a software to handle code and syntax highlighting. I use Sublime Text 3. Have Sublime Text 3 installed.

    Of course you can just use R-Studio to compile all these, of course, under the knitr mode: Preferences -> Sweave -> Weave Rnw files using -> knitr.

  • Inside Sublime Text:

install.packages(c("knitr"), repos = "http://cran.rstudio.com")
  • Modify user settings in LaTeXing. First line adds knitr capabilities, while the second line allows BibTeX compilation.
{"knitr": true,
"quick_build": [
    {
        "name": "Primary Quick Build: latexmk",
        "primary": true,
        "cmds": ["latexmk"]
    }
                ]
}
  • Writing R and LaTeX code together:

Create a .rnw file. Here is a small example I modified from Yihui Xie, the developer of knitr.

Example: Lets try an example. See the output below.


Here is a code chunk.

<<label, fig.cap='Your Title Goes Here', tidy=TRUE>>=
# Speaking of reproducibility, don't forget to set a seed so others can generate the SAME sequence!
set.seed(602)
library(MASS) # to generate multi-var distribution
e <-  as.numeric(mvrnorm(n = 100, mu = 0, Sigma = 1))
plot(e)
@

Example Plot 1

The function tidy=TRUE keeps your code within the page margins. See how the line Speaking of reproducibility… behaves.

You can also write inline expressions, e.g. \( \pi \) = 3.141592. Here I printed the number \( \pi \) by calling the \Sexpr{pi} function.

Also, you can use information specified in your code such as the mean of \( \epsilon \) which is -0.1682144, which I computed calling the mean function inside the Sexpr expression, like so \Sexpr{mean(e)}.

Editors and (most) readers are not interested in your coding. That’s fine. The important thing is to make your code available to the audience that needs it. You can hide it by calling the echo = FALSE function in the preamble, i.e. within the <<>>= symbols. Below, you will only see the plot, not the coding that generated it.

<<histogram, echo = FALSE, fig.cap='Your Title Goes Here'>>=
hist(e)
@

Example Plot 2

Finally, following the standard LaTeX tools, you can call the first figure using the \autoref{fig:label} function. Similarly, you can call the second one by typing the \autoref{fig:histogram} function.


Here below is the LaTeX code. Copy and paste it into a .rnw file.

\documentclass{article}
\usepackage{hyperref} % to generate figure and (sub)section hyperlinks within document.
\begin{document}

Here is a code chunk.

<<label, fig.cap='Your Title Goes Here', tidy=TRUE>>=
# Speaking of reproducibility, don't forget to set a seed so others can generate the SAME sequence!
set.seed(602)
library(MASS) # to generate multi-var distribution
e <-  as.numeric(mvrnorm(n = 100, mu = 0, Sigma = 1))
plot(e)
@

The function \texttt{tidy=TRUE} keeps your code within the page margins. See how the line \emph{Speaking of reproducibility...} behaves.

You can also write inline expressions, e.g. $\pi=\Sexpr{pi}$. Here I printed the number $\pi$ by calling the \texttt{Sexpr} function.

Also, you can use information specified in your code such as the mean of {\bf e} which is {\bf -0.1682144}, which I computed calling the \texttt{mean} function inside the \texttt{Sexpr} expression.

Editors and ({\bf most}) readers are {\bf not} interested in your coding. That's fine. The important thing is to make your code available to the audience that needs it. You can hide it by calling the \texttt{echo = FALSE} function in the preamble, i.e. within the \texttt{<<>>=} symbols. Below, you will only see the plot, not the coding that generated it.

<<histogram, echo = FALSE, fig.cap='Your Title Goes Here'>>=
hist(e)
@

Finally, following the standard  \LaTeX tools, you can call the first figure using the \autoref{fig:label} function. Similarly, you can call the second one by typing the \texttt{\autoref{fig:histogram}} function.

\end{document}