Section 10 Citations

In this section of the book you will learn how to include bibliographies into your document, how to generate citations or source them from external places. We will touch on RefMangeR and bibtex packages to help us with references. In addition we will also learn citr package. Finally we will mention external software that can help you manage large number of citations.

For this section of the book we need to install these additional packages

install.packages(c("bibtex", "RefManageR"))

10.1 Introductions

A citation is a reference to a source (from wikipedia). R provides nice function citation() that helps us generating citation blob for R packages that we have used

Let’s try generating citation text for rmarkdown package by using the following command

citation("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. URL
  https://rmarkdown.rstudio.com.

  Yihui Xie and J.J. Allaire and Garrett Grolemund (2018). R Markdown:
  The Definitive Guide. Chapman and Hall/CRC. ISBN 9781138359338. URL
  https://bookdown.org/yihui/rmarkdown.

  Yihui Xie and Christophe Dervieux and Emily Riederer (2020). R
  Markdown Cookbook. Chapman and Hall/CRC. ISBN 9780367563837. URL
  https://bookdown.org/yihui/rmarkdown-cookbook.

To see these entries in BibTeX format, use 'print(<citation>,
bibtex=TRUE)', 'toBibtex(.)', or set
'options(citation.bibtex.max=999)'.

— great

10.2 BibTex

10.2.1 Introduction

BibTex is one of many citation formats, this is a more comprehensive list of all possible citation formats. Rmarkdown works best with BibTex file format and this is the one we are going to cover today. Typical file extention for BibTex is .bib

Let’s try converting our citation text into bibtex format using the following code

#toBibTex(citation("rmarkdown"))

citation("rmarkdown") %>% toBibtex()
@Manual{,
  title = {rmarkdown: Dynamic Documents for R},
  author = {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},
  year = {2020},
  note = {R package version 2.5},
  url = {https://github.com/rstudio/rmarkdown},
}

@Book{,
  title = {R Markdown: The Definitive Guide},
  author = {Yihui Xie and J.J. Allaire and Garrett Grolemund},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2018},
  note = {ISBN 9781138359338},
  url = {https://bookdown.org/yihui/rmarkdown},
}

@Book{,
  title = {R Markdown Cookbook},
  author = {Yihui Xie and Christophe Dervieux and Emily Riederer},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2020},
  note = {ISBN 9780367563837},
  url = {https://bookdown.org/yihui/rmarkdown-cookbook},
}

10.2.2 knitr::write_bib

It’s great to be able to get a BibTex text and we can manually copy and paste it into a file, but instead we can use write_bib() function from knitr package package to do that. Let’s run the following code in order to generate references.bib file

knitr::write_bib("rmarkdown", file = "references.bib")

10.2.3 BibTex keys

It is a bit of a round trip, for some reason citation() function in BibTex section above doesn’t make keys for our bibtex entries, whereas knitr::write_bib() function does. Let’s read our bibtex file back in using the following code to see what our keys are. We will learn how to use those key in the Inline citation section below

library(bibtex)

refs <- read.bib("references.bib")
keys <- names(refs)
keys
[1] "R-rmarkdown"   "rmarkdown2018" "rmarkdown2020"

Let’s open our references.bib file by clicking on it in the “File” pane-tab and compare that bibtex with the one in BibTex section above. Can’t you find location of those keys in the bibtex file?

@Manual{R-rmarkdown,
  title = {rmarkdown: Dynamic Documents for R},
  author = {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},
  year = {2020},
  note = {R package version 2.5},
  url = {https://github.com/rstudio/rmarkdown},
}

@Book{rmarkdown2018,
  title = {R Markdown: The Definitive Guide},
  author = {Yihui Xie and J.J. Allaire and Garrett Grolemund},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2018},
  note = {ISBN 9781138359338},
  url = {https://bookdown.org/yihui/rmarkdown},
}

@Book{rmarkdown2020,
  title = {R Markdown Cookbook},
  author = {Yihui Xie and Christophe Dervieux and Emily Riederer},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2020},
  note = {ISBN 9780367563837},
  url = {https://bookdown.org/yihui/rmarkdown-cookbook},
}

10.3 Where do I get .bib file?

10.3.1 Citation managment software

This a short list of external software that may help you with managing citation. Those four will have an option to export a list of citation as a .bib file. I will leave for you as a home to figure out how to export .bib file from your citation manager of choice.

10.3.2 ZoteroBib service

zbib.org

Zotero provides nice free service for getting citation and exporting them as .bib files

10.3.3 RefMangeR

The RefManageR maybe a useful package to try to source external references

library(RefManageR)

#keywords <- c("FastQC",
#              "BWA-MEM",
#              "featureCounts",
#              "RNAsik")

#citations <- lapply(keywords, ReadCrossRef, limit = 1)

titles <- c("FastQC: A Quality Control Tool for High Throughput Sequence Data",
            "Aligning Sequence Reads, Clone Sequences and Assembly Contigs with BWA-MEM",
            "featureCounts: An Efficient General Purpose Program for Assigning Sequence Reads to Genomic Features",
            "RNAsik: A Pipeline for Complete and Reproducible RNA-Seq Analysis That Runs Anywhere with Speed and Ease")

citations <- lapply(titles, ReadCrossRef, limit = 1)

citations

lapply(citations, WriteBib, file = "references2.bib", append = TRUE)

10.3.4 Journal websites

A lot of journal website as well as Google Scholar will have some export citation button. Look for those “export” button and select .bib file format

10.4 Linking .bib file with Rmarkdown document

Let’s practice linking bibtex file with our Rmarkdown document via YAML header. We are going to use references.bib file that we have generated in the section above. Start by including the following key value pair into your YAML header

---
bibliography: "references.bib"
---

You can include multiple reference files using the following syntax, alternatively you can concatenate two bib files into one.

---
bibliography: ["references1.bib", "references2.bib"]
---

10.5 Inline citation

Now we can start using those bib keys that we have learned just before, using the following syntax

  • [@key] for single citation
  • [@key1; @key2] multiple citation can be separated by semi-colon
  • [-@key] in order to suppress author name, and just display the year
  • [see @key1 p 12; also this ref @key2] is alos a valid syntax

Let’s start by citing the rmarkdown package using the following code and press Knit button. I am also including a new first level header and a short sentence.

# Citation section

I have been using Rmarkdown package [@R-rmarkdown]. It is pretty cool. I should also go and read [@rmarkdown2018; and @rmarkdown2020] books. 

10.6 References section

As you can see references list will be added to the very end of the document. If add a new header called “References” to the bottom of our document that will section our references list out. Let’s see what happens when we include first level header at the bottom of the document and press Knit, use the following code

# References

Alternatively we can use the following code to include references anywhere in the document. Let’s try puting our references list at the very top, just to that we can.

# References

<div id="refs"></div>

— funny

10.7 Citation style

list of csl files on zotero

Most likely you will need to use specific style for your citation. Citation Style Language (CSL) is the language used to define the style in the .csl file. You need to download the .csl file for your desired style and link it with Rmarkdown document via YAML header. Zotero’s website is the most comprehensive collection of styles, but there are other places to source .csl files, for example this github repository.

Let’s programmatically download Nature’s journal citation style from zotero, link it with our Rmarkdown document and Knit using the following code

download.file("https://www.zotero.org/styles/nature?source=1", destfile = "nature.csl")
---
csl: nature.csl
---

10.8 Linking inline citation with the references list

You can also enable internal linking of the citation to the corresponding entry in the references list using the following YAML key

---
link-citations: true
---

10.9 Footnotes

^[Footnote goes here]

10.10 Citr addin

https://github.com/crsh/citr