Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically create BibTex citations for R packages in knitr file?

I am not sure whether this an R, LaTeX, or BibTex problem.

I am trying to automatically generate a .bib file containing citations for R packages and then list them at the end. I am able to generate the BibTex file and I don't see anything wrong with the BibTex file, but the entries don't appear when I compile the PDF.

I'm not sure if R is not producing a BibTex file correctly, if some LaTeX syntax is wrong, or if the BibTex file needs to be pre-compiled or whatever. I noticed that \bibliography{NOT A REAL FILENAME} will produce a References section without complaining, but I don't think that is the problem.

Minimal working example:

\documentclass[10pt]{amsart}
\usepackage[margin=1in, headheight=20pt, footskip=20pt]{geometry}

\begin{document}

<<label='Create References'>>=
require(knitr) # Needed for write_bib()

# Load some packages to the session:
require(xtable)
require(ggplot2)

# Select packages to cite:
citPkgs <- names(sessionInfo()$otherPkgs)
# Write the bibtex file:
write_bib(citPkgs, file="R-Pckgs.bib")
@

\nocite{*}
\bibliographystyle{plain}
\bibliography{R-Pckgs.bib}

\end{document}

Any help or suggestions would be appreciated.

like image 338
Ellis Valentiner Avatar asked Oct 17 '13 16:10

Ellis Valentiner


People also ask

How do you cite R packages in Rmarkdown?

To cite the 'rmarkdown' package in publications, please use: JJ Allaire and Yihui Xie and Jonathan McPherson and Javier Luraschi and Kevin Ushey and Aron Atkins and Hadley Wickham and Joe Cheng and Winston Chang and Richard Iannone (2020). rmarkdown: Dynamic Documents for R. R package version 2.5.

How do I add citations to BibTeX?

Run your search, and export your marked results in BibTeX format. Browse for the . bib file and click Add. In the Content section, choose from the dropdown to select all references or only those that are cited.


2 Answers

Just replace \bibliography{R-Pckgs.bib} with \bibliography{R-Pckgs}, and it should work fine.

On Windows 7, with an up-to-date MikTeX installation and current R and R packages, the following worked:

  1. Put your reproducible example in a file named "eg.Rnw" and edit to remove the extraneous ".bib"
  2. Launch R and navigate to the directory in which "eg.Rnw" is located.
  3. Do library(knitr); knit2pdf("eg.Rnw")

Note: There are obviously many workflows for going from *.Rnw to *.pdf, but if you want to use knit2pdf() (at least), make sure that you run it from the directory containing the *.Rnw to be processed.

like image 133
Josh O'Brien Avatar answered Oct 19 '22 17:10

Josh O'Brien


Add a \nocite{dummycite} to your document - to create a citation so that the bibliography is printed.

Note that this is obsolete if you already have other citation in your document.

like image 29
EDi Avatar answered Oct 19 '22 16:10

EDi