[Tex/LaTex] How to insert references and bibliography into a .Rnw file produce with RStudio R Sweave and knitr

biblatexknitrrstudiosweave

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}

\addbibresource{BIBLIOGRAPHY_24th_March_2019.bib}
% 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?

Best 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}
% or \addbibresource{BIBLIOGRAPHY_24th_March_2019.bib}

<<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}

Answers to this problem was already discuss here: knitr and biblatex.

Apparently, this problem can also be managed by latexmk https://mg.readthedocs.io/latexmk.html#, but it looks too complicated for me.

Thank you everyone.