-
Notifications
You must be signed in to change notification settings - Fork 41
/
citations-and-styles.Rmd
124 lines (83 loc) · 4.25 KB
/
citations-and-styles.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Citing Articles & Bibliography Styles
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.
## Overview
* **Teaching**
* **Exercises**
## Questions
* What sort of things can I cite?
* How do I manage my `.bib` file?
* How do I change the citation style?
## Objectives
* Provide a bibliography at the end of the document
* Cite articles and packages during the document
* learn how to manage citation styles
## How to cite things
Citing things in an rmarkdown 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`:
```YAML
---
title:
author:
output: html_document
bibliography: references.bib
---
```
### What is a .bib file?
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},
}
```
### And how do I generate these .bib files?
You can use the `citation` function in R for R itself, and for specific R packages.
We can get the citation for R with:
```{r citation-r}
citation()
```
And for ggplot2 with
```{r citation-ggplot}
citation("ggplot2")
```
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](https://www.zotero.org/), [Mendeley](https://www.mendeley.com/download-desktop/), [papers](https://www.papersapp.com/), or my personal preference [paperpile](https://paperpile.com/). 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.
### Your Turn {.exercise}
1. Generate a references.bib file to place your citations
1. Using the `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
1. Reference these in your document
1. Add a final heading in your file called `#bibliography`
1. Render the document
## How to change the bibliography style
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](https://citationstyles.org/)
Similar to how you referred to your `.bib` file with `bibliography: ref.bib`, you do something similar:
```YAML
---
title:
author:
output: html_document
bibliography: references.bib
csl: my_journal.csl
---
```
### Your Turn {.exercise}
1. select your bibliography style to be one from your favourite journal at the CSL github repo here: https://github.com/citation-style-language/styles (> 1800 citations and counting)
1. place this in your rstudio project
1. refer to it in the YAML
1. Render your document and observe your greatness
## How to move the bibliography location
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](https://stackoverflow.com/questions/51335125/adding-figures-and-tables-after-bibliography-in-rmarkdown?noredirect=1&lq=1). 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
### Your Turn {.exercise}
1. Generate a bibliography and an appendix that follows it