• Home   /  
  • Archive by category "1"

Knitr Markdown Bibliography

I am finding myself more and more drawn to markdown rather then tex/Rnw as my standard format (not least of which is the ease of displaying the files on github, particularly now that we have automatic image uploading). One thing I miss from latex is the citation commands. (I understand these can be provided to markdown via Pandoc, but I’d like to simply have to knit the document, and not then run it through pandoc, latex, or another interpreter). I’ve taken a little whack at generating in-text citations using knitr and other R tools.

DOI Approach

I’ve put some simple functions in a package. The functions use the crossref API to grab citation information given a doi, so I don’t have to generate a bibtex file for papers I’m reading, (inspired by the kcite package for Wordpress). One can grab my package from github

and load the package

Then we can generate a citation given a doi with the function:

Bibtex Approach

If we have a bibtex file, we can use this for the citations as well. Let’s start off by getting ourselves a bibtex file from some of R’s packages:

Now we can simply read in the bibtex files:

Francois R (2011). bibtex: bibtex parser. R package version 0.3-1/r332, .

(This would be much more awesome if we could generate keys on write.bib and use those bibtex keys, instead of the index value, , to generate the citation.)

Using the inline citations

Now that we can get citation information from bibtex files or dois, we need a way to insert these citations into the text. I’ve written a simple print inline citations that would just use a given shortened format (e.g. author-year) and add the citation to a object, which we could then use to generate the full citation information at the end. We can generate inline citations by giving a doi, bibentry object, or a list thereof, into inline knitr code block. Thus we can use the line to generate a parenthetical citation, (Halpern et. al. 2006). We can alos generate textual citations with , such as Francois, (2011). Parenthetical citations can take more than one entry, such as , which produces (Xie, 2012; Boettiger, 2012).

Generating the final bibliography

As we go along adding inline citations, R stores the list of citation info. Then at the end of the document, use this command to print the bibliography generated by the use of our inline citations.

Halpern B, Regan H, Possingham H and McCarthy M (2006). “Accounting for uncertainty in marine reserve design.” Ecology Letters, 9. ISSN 1461-023X, .

Francois R (2011). bibtex: bibtex parser. R package version 0.3-1/r332, .

Xie Y (2012). knitr: A general-purpose package for dynamic report generation in R. R package version 0.4.1, .

Boettiger C (2012). knitcitations: Citations for knitr markdown files. R package version 0.0-1.

I hope to add markup to format this a bit more nicely later. For instance, we want the links to appear as real links. Additionally, we may want to add markup around the citations, such as the reason for the citation into the link using the Citation Typing Ontology.

This entry was, of course, produced in knitr with my knitcitations package. See the original source.

Learning Objectives

After completing this tutorial, you will be able to:

  • Add citations to an report.
  • Create a file to store citation data to use with .

What You Need

You need and to complete this tutorial. Also you should have an directory set up on your computer with a directory with it.

To begin, be sure that is already installed on your computer.

Introducing Bookdown

is an extension that can be used to create reports. In this lesson however you will review briefly how to use to create single document reports. The steps are as follows:

Note that this code replaces the that is the default for R Markdown.

This tells to use rather than the conventional . Notice below you use to create html output with inline citations.

To achieve different styling and formats, you can replace with other outputs including:

  • ,
  • ,
  • ,
  • ,
  • and

Learn more about bookdown output options here.

Your R Markdown YAML header will thus look like this:

2. Create a BibTex File Containing References

Next, create a file containing all of the citations that you will use in your report. A file is a text formatted, machine readable reference list. You can create BibTex reference lists in many reference manager tools including Zotero and Mendeley or you can go the hard route and make one on your own. A file should be named with a extension for example and needs to be saved using the UTF-8 encoding.

An example of a formatted citation is below.

Be sure to save that file in the same working directory with your .Rmd file!

Using the code below, add a link to your BibTex file.

Now your entire YAML header looks like this:

You can also add the link-citations: yes argument to your YAML header to ensure that R creates links from your citation to the bibliography below. Like this:

Add In-text Citations / References to Your Report

Finally you can add citations to a report. To do this you use the syntax

Where anderson2015 is the name of the Anderson citation (used an example below). The sign tells to find that particular citation in the file.

Add Unique Styles

If you want - you can also apply custom stylesheets (you have to create the .css file or use someone elses!), add figure captions, apply themes that are built into and and specify a table of contents.

Explore the options below to see what they do when you add them to your YAML output at the top of your document.

R Markdown resources Add images to R Markdown

One thought on “Knitr Markdown Bibliography

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *