Now that you are near the end of your data analysis, you want to make sure that you’ve plugged in the gaps of REF1 REF2 and so on correctly cited the articles and software you wanted to mention.
.bib
file?Citing things in an R Markdown document is straightforward, you refer to articles you want to cite using [@article-handle]
. Here, article-handle
matches the article handle in your .bib
file.
This .bib
file is referred to in the YAML of your document, under the option bibliography: filename.bib
:
---
title:
author:
output: html_document
bibliography: references.bib
---
Good question. .bib
is a format for storing references from the haydey of LaTeX. It contains scripts with reference information for the article. Here’s an example one
@Book{ggplot2,
author = {Hadley Wickham},
title = {ggplot2: Elegant Graphics for Data Analysis},
publisher = {Springer-Verlag New York},
year = {2016},
isbn = {978-3-319-24277-4},
url = {http://ggplot2.org},
}
You can use the citation
function in R for R itself, and for specific R packages.
We can get the citation for R with:
citation()
##
## To cite R in publications use:
##
## R Core Team (2019). R: A language and environment for statistical
## computing. R Foundation for Statistical Computing, Vienna, Austria.
## URL https://www.R-project.org/.
##
## A BibTeX entry for LaTeX users is
##
## @Manual{,
## title = {R: A Language and Environment for Statistical Computing},
## author = ,
## organization = {R Foundation for Statistical Computing},
## address = {Vienna, Austria},
## year = {2019},
## url = {https://www.R-project.org/},
## }
##
## We have invested a lot of time and effort in creating R, please cite it
## when using it for data analysis. See also 'citation("pkgname")' for
## citing R packages.
And for ggplot2 with
citation("ggplot2")
##
## To cite ggplot2 in publications, please use:
##
## H. Wickham. ggplot2: Elegant Graphics for Data Analysis.
## Springer-Verlag New York, 2016.
##
## A BibTeX entry for LaTeX users is
##
## @Book{,
## author = {Hadley Wickham},
## title = {ggplot2: Elegant Graphics for Data Analysis},
## publisher = {Springer-Verlag New York},
## year = {2016},
## isbn = {978-3-319-24277-4},
## url = {https://ggplot2.tidyverse.org},
## }
For journals or books, you’ll need to get a specific .bib file. Yes, this can be a bit of a pain, but this is where you need to use a reference management software like Zotero, Mendeley, papers, or my personal preference paperpile. The important thing to to use something. These all allow you to get .bib files of your articles, which you can then placec in your references.bib
file.
citation()
function, generate citations for the packages we have used, “dplyr”, “ggplot2”, “gapminder”, and for the R software, place these in your references.bib
file#bibliography
OK so now you’ve got your bibliography, but you now need to change it to a specific journal format. Luckily, this is now pretty easy. You can change your citation style from the citation style language
Similar to how you referred to your .bib
file with bibliography: ref.bib
, you do something similar:
---
title:
author:
output: html_document
bibliography: references.bib
csl: my_journal.csl
---
The bibliography is typically placed at the end of the document, so your last heading should be something like # References
. However, if you want to move it, you need to use the bookdown::html_document2()
output option, and then in your document, place the following piece of text in the reference section. For example.
# Introduction
# References {-}
<div id="refs"></div>
# Appendix
Note the reference section, the code: <div id="refs"></div>
This is taken from this SO thread. Note that the answer also states:
Note: this will only work if you use pandoc’s built-in citation package and won’t work if you set citation_package: natbib in the YAML