Everybody is talking about making research reproducible. With the integration of knitr in RStudio, reproducible research has been made available to people who are less familiar with the Terminal. All you need is some basics in R and LaTeX and you can generate reports directly from within RStudio. With a few tweaks, RStudio automatically generates the bibliography and you won’t have to manually run latex
or biber/bibtex
.
This post explains how to configure your .Rnw
file to automatically generate the bibliography using the biblatex package in a single run from within RStudio. I assume you are familiar with RStudio and knitr/Sweave. I further assume that knitr is set as the Rnw weave option in RStudio –> Preferences –> Sweave.
The setup
You’ll have to do two things for the bibliography to be generated automatically (see code below),
- Run
biblatex
with thebibtex
backend instead ofbiber
. This is because RStudio uses thetexi2dvi
command to generate the PDF and only knowsbibtex
. Hopefully, this will change in the future. - Set the three environment variables:
TEXINPUTS
,BIBINPUTS
andBSTINPUTS
in order to tell RStudio where to find the.tex
,.bib
and.bst
files. Because my project was small, I had all the files in the same (root) directory. Therefore, I set the environment variables to the R working directory usinggetwd()
.
\usepackage[backend=bibtex]{biblatex}
setwd('/path/to/your/working-directory') Sys.setenv(TEXINPUTS=getwd(), BIBINPUTS=getwd(), BSTINPUTS=getwd())
That’s all you need. Compile PDF will execute all R code chunks and generates the report with bibliography. If you use bibtex
, e.g. with the natbib package, you’ll only have to set the environment variables (point 2).
A minimal working example
\documentclass{article} \usepackage{hyperref} \usepackage[backend=bibtex, sorting=none]{biblatex} \bibliography{references} \begin{filecontents*}{references.bib} @Manual{knitr2013, title = {knitr: A general-purpose package for dynamic report generation in R}, author = {Yihui Xie}, year = {2013}, note = {R package version 1.4.1}, url = {http://yihui.name/knitr/}, } \end{filecontents*} \begin{document} \section*{Automatic biblatex bibliography generation in RStudio using knitr} <<setup, include=FALSE, cache=FALSE, echo=FALSE>>= opts_chunk$set(fig.path='figures/plots-', fig.align='center', fig.show='hold', eval=TRUE, echo=TRUE) options(replace.assign=TRUE,width=80) Sys.setenv(TEXINPUTS=getwd(), BIBINPUTS=getwd(), BSTINPUTS=getwd()) @ <<sample-data-hist-and-box, out.width='0.48\\textwidth'>>= sampleData <- rnorm(1000, 0,1) hist(sampleData) boxplot(sampleData) @ This document was produced in RStudio using the knitr package \cite{knitr2013} by \url{http://texblog.org}. \printbibliography \end{document}
David Lovell
Many thanks Tom for this excellent public service announcement! I’ve just figured I need to move from Rmarkdown to Sweave to make some well-formatted Supplementary Materials. Your post has helped immensely.
tom
Many thanks for the feedback! Tom.
Simon Kiss
Hi this looks really great, but is there *any* way to hack this to make it run Biber? I’ve got a style file that only uses biber
tom
Hi Simon,
Thanks for your question. This is indeed a problem. You’ll either have to do it manually (run biber and pdflatex in the terminal) or use one of the options listed here.
Tom
maja
Hi! Thanks for this, really helpful! I’m having trouble though trying to get it to work with natbib. If I understood you correctly I should be able to simply do
instead of
but this is giving me errors? Any help would be greatly appreciated!
tom
Hi Maja,
You’ll have to change a couple of more lines, because BibTeX uses a different set of macros to generate the bibliography.
Also, I couldn’t get RStudio to automatically generate the bibliography, but had to go to the terminal and type:
bibtex xyz
,pdflatex xyz
,pdflatex xzy
(with xyz being the tex filename).HTH, Tom
maja
Thanks Tom! Not sure what happened, I was having problems yesterday with the second bit like you, but now it seems to work i.e. directly from RStudio (tjhat is using filecontents to create the file)!?
Anyway, sees to be working fine, thanks for your quick reply and great blog!
tom
Thanks for the update! Best regards, Tom
Joyce
Thanks for posting this. Is this still the best way to go about things or are there new options in RStudio?
tom
Hi Joyce,
Thanks for your comment. I assume you’re question is about using
biber
with RStudio. Essentially, it appears that this is still not possible by default. What you can do:– Manually generate the output from the tex-file using
pdflatex
andbiber
.– Configure RStudio to use
latexmk
as described here.Hope this helps,
Tom
Joyce
Hi, Thanks a lot. I don’t need to use biber. For reproducibility purposes I was hoping for something though that didn’t need setwd.
Once again, thanks for posting this. I tried a number of options and this one is the only one that worked for me.
Joyce
Wait… I see now that the setwd is gone… great!
tom
Hi Joyce,
Right, the
setwd()
should not have been in there in the first place. Sorry for that!Best, Tom
cmohamma
I’m trying to compile a .Rnw using sweave and I would like to add a .bib file. When I add this
And it compile it compiles without error but no references section is included. Then I tried adding per your advice:
I added this to the part of my code before the document begins (i tried else where as well) but regardless of where I place that script a litany of errors is produced
tom
I ran this from RStudio using
knitr
and it worked fine. I’m not sure the code is compatible withSweave
though.Best, Tom.
Matthew Son
Kudos to the author. My 2 hours of googling finally payed off. Superb, excellent!!