How to insert references and bibliography into a .Rnw file produce with RStudio R Sweave and knitr?

So far, I have successfully used RStudio and R Markdown to write scientific papers and insert citations from the medical literature without significant problem as shown in the minimal example (MWE) below which produces a .pdf file.

---
title: ""
csl: nature.csl
output: pdf_document
bibliography: BIBLIOGRAPHY_24th_March_2019.bib
---

Meningiomas which are thought to arise from arachnoidal cap cells, are the most common meningeal tumours [@louis_2016_2016].

# References


I am now starting to write a PhD thesis always with RStudio but, this time with R Sweave (knitr to weave .Rnw files) as I want to use the functionalities of Latex. What seems simple in theory is in fact not.

The following MWE producing this error:

using fall-back bibtex(8) backend: functionality may be reduced/unavailable...

\documentclass{article}

\usepackage[backend= bibtex]{biblatex}
% or \usepackage[backend= biber]{biblatex}

% or %\bibliography{BIBLIOGRAPHY_24th_March_2019}

<<setup, eval= TRUE, include=FALSE, cache=FALSE, echo=FALSE>>=
Sys.setenv(TEXINPUTS=getwd(),
BIBINPUTS=getwd(),
BSTINPUTS=getwd())
@

\begin{document}

Meningiomas which are thought to arise from arachnoidal cap cells, are the most common meningeal tumours subtypes \cite{louis_2016_2016}.

\printbibliography

\end{document}


My simple bibliography file (BIBLIOGRAPHY_24th_March_2019.bib) is store in the same folder as the mwe.Rnw file (C:\Users\Charles\Documents\R) and is built as follow:

@article{louis_2016_2016,
title = {The 2016 World Health Organization Classification of Tumors of the Central Nervous System: a summary},
volume = {131},
issn = {1432-0533},
doi = {10.1007/s00401-016-1545-1},
shorttitle = {The 2016 World Health Organization Classification of Tumors of the Central Nervous System},
pages = {803--820},
number = {6},
journaltitle = {Acta Neuropathologica},
shortjournal = {Acta Neuropathol.},
author = {Louis, David N. and Perry, Arie and Reifenberger, Guido and von Deimling, Andreas and Figarella-Branger, Dominique and Cavenee, Webster K. and Ohgaki, Hiroko and Wiestler, Otmar D. and Kleihues, Paul and Ellison, David W.},
date = {2016-06},
pmid = {27157931}
}


I have reviewed countless webpages (https://cimentadaj.github.io/phd_thesis/thesis_template_example/2017-10-24-thesis-template.html; https://texblog.org/2013/08/20/rknitr-automatic-bibliography-generation-with-biblatex-in-rstudio/; https://stackoverflow.com/questions/33332654/with-knitr-and-rnw-for-latex-how-do-you-print-the-full-bibliography-in-pdf-out and so on) and tried different packages without success even by adding the following code chunk into my .Rnw document:

    Sys.setenv(TEXINPUTS=getwd(),
BIBINPUTS=getwd(),
BSTINPUTS=getwd())


My bibliography (BIBLIOGRAPHY_24th_March_2019.bib) file was exported from Zotero.

I am working with Windows 10, MikTeX for LaTeX. All packages and softwares are up-to-date.

My question is: does anyone knows a simple and effective way to insert references, even directly from the web with DOI, into a .Rnw file produce with Rstudio, R Sweave and knitr?

• (1) Without the (reduced as far as possible) .bib file and without know the error messages is just a guessing game try to solve your problem. (2) biblatex is in fact easy to use. Start with a simple bibliography as here, without using the bibtex backend, just as in the last part of this answer. Once you realize it works, try with your bibliography and only if still work, try other biblatex options (otherwise, of course, debug your .bib file) (3) JabRef can make .bib references with the DOI and some other codes (ISBN, PMID.,..) . – Fran Mar 24 at 20:32
• I can compile your files without problem. But RStudio run only pdflatex. You must run biber externally. Once created the .bbl file you can compile from the updates form Rstudio without problems if you do not change the references. – Fran Mar 24 at 21:54
• On the other hand, adding the backend=bibtex and the R chunk work like a charms without need of run bibtex externaly. Just "Compile PDF" and ready. And without the R chunk also in my Linux. According here is because RStudio uses texi2dvi that only knows bibtex. – Fran Mar 24 at 22:20
• You can also try to rum biber from a R chunk, some like system(paste("biber", sub("\\.Rnw$", "", current_input()))) should do the job. – Fran Mar 24 at 22:26 • @Fran Yes, but using bibtex with biblatex is really not recommended these days, so I wouldn't go that route. – Alan Munn Mar 24 at 22:28 1 Answer Following Fran's advice, it works by adding the following chunk code in the preamble (before \begin{document}) and change \usepackage{biblatex} for \usepackage[backend=biber]{biblatex}: <<setup, eval= TRUE, include= FALSE, cache= FALSE, echo= FALSE>>= system (paste ("biber", sub ("\\.Rnw$", "", current_input())))
@


I have no idea whatsoever of what it does, but it solves the problem. Please note that you have to compile (at least) twice the document every time you change its name e.g. mwe_2.Rnw to mwe_3.Rnw. Moreover, don't setup cache = TRUE in the knitr chunk.

My working MWE is the following:

\documentclass{article}

\usepackage[backend= biber]{biblatex}

\bibliography{BIBLIOGRAPHY_24th_March_2019}

<<biber, eval= TRUE, include= FALSE, cache= FALSE, echo= FALSE>>=
system (paste ("biber", sub ("\\.Rnw\$", "", current_input())))
@

\begin{document}

Meningiomas which are thought to arise from arachnoidal cap cells, are the most common meningeal tumours subtypes \cite{louis_2016_2016}.
Malignant meningioma is a highly aggressive and often fatal variant that ... \cite{jenkinson_radiotherapy_2014}.
The behaviour and outcome of WHO Grade II meningiomas also called atypical are intermediate as they show a ... \cite{champeaux_who_2016}.

\printbibliography

\end{document}


• The important thing is that your document is compiled at least with LaTeX, then Biber and then again (at least once, better twice) with LaTeX. tex.stackexchange.com/q/63852/35864 has a great explanation what Biber does and why it is necessary to run the full cycle. AFAIK Rmarkown and friends are able to run LaTeX, BibTeX, LaTeX, LaTeX for you. But since you use biblatex Biber is preferable over BibTeX and apparently Rmarkdown does not automatically run Biber. So you have to do that yourself. This is essentially what your system call does... – moewe Mar 25 at 9:03
• Since you are on Windows, another IDE option is WinEdt with its RManager package <winedt.org/config/modes/RManager.html>. It can knitr your R chunks, compile your tex file, and run Biber to produce a pdf inclusive of the bibliography. While the reporting task is numerically intensive, RStudio is best, but to produce a finished report, switching to WinEdt may offer certain advantages. – Ross Mar 25 at 10:44
• latexmk should be able to do it, but because of the way Rstudio works, it doesn't seem to work properly. – Alan Munn Mar 25 at 15:22