-
Notifications
You must be signed in to change notification settings - Fork 15
/
2.04-Rmd-notebooks-etc.Rmd
128 lines (97 loc) · 6.59 KB
/
2.04-Rmd-notebooks-etc.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
125
126
---
output:
word_document: default
html_document: default
---
# Authoring reproducibly with Rmarkdown
## Notebooks
Here is a pro-tip. First, number your notebooks and have outputs and intermediates directories
associated with them. And second, always save the R object that is a ggplot in the outputs so that if
you want to tweak it without re-generating all the underlying data, you can do that easily.
## References
Science, as an enterprise, has proceeded with each new generation of researchers building
upon the discoveries and achievements of the previous. Scientific publication honors this
tradition with the stringent requirement of diligent citation of previous work. Not only
that, but it is incumbent upon every researcher to identify all current work by others that
is related to their own, and discuss its similarities and differences. As recently as the early
90s, literature searches involved using an annual index _printed on paper!_ And, if you found
a relevant paper you had to locate it in a bound volume in the library stacks and copy it page by page
at a Xerox machine (or send an undergraduate intern to do that...)
Today, of course, the Internet, search services like Google Scholar, and even Twitter,
have made it far easier to identify related work and to keep abreast of the latest
developments in your field. But, this profusion of new literature leads to new challenges
with managing all this information in a way that makes it easy for you to access, read, and
cite papers while writing your own publications. There are many reference management
systems available today, to help you with this task. Some of these are proprietary and paid
products like EndNote. Many institutions (like Colorado State University) have licenses that provide
their students a no-cost way to obtain EndNote, but the license will not extend to updates to
the program, once the student has graduated. (CHECK THIS!!!).
An alternative citation manager is Zotero. It is an open source project that has been funded
not by publishing companies (like its non-open-source competitors, Mendeley and ReadCube) but
by the non-profit Corporation for Digital Scholarship. As an open-source project, outside
contributors have been enabled to develop workflows for integrating Zotero with reproducible
research authoring modalities like Rmarkdown, including RStudio integration that lets you
drop citations from Zotero directly into your Rmarkdown document where they will be cited
and included in the references list in the format of just about any journal you might
want to choose from. Accordingly, I will describe how to use Zotero as a citation manager
while writing Rmarkdown documents.
_Install Zotero and be sure to install the connector for Chrome, Firefox, or Safari_
### Zotero and Rmarkdown
Zotero has to be customized slightly to integrate with Rmarkdown and Rstudio, you must
install the R package `citr`, and you should make some configurations:
1. First, you have to get a Zotero add-on that can translate your Zotero
library into a different format called BibTeX format (which is used with the
TeX typesetting engine and the LaTeX document preparation system). Do this by
following the directions at [https://retorque.re/zotero-better-bibtex/installation/](https://retorque.re/zotero-better-bibtex/installation/).
2. When you restart Zotero, you can choose all the default configurations
in the BetterBibTeX start-up wizard.
3. Then, configure the BetterBibTex preferences by going to the Zotero preferences,
choosing BetterBibTex, and then selecting "Export" button. That yields a page
that gives you a place to omit certain reference fields. You life will be easier
if you omit fields that are often long, or which are not needed for citation. I recommend
filling that field with:
```{r, eval=FALSE}
abstract,copyright,file,pmid
```
And, you probably should restart Zotero after doing that.
4. Install the R package `citr`. It is on CRAN, but it is probably best
to first try the latest development version. Install it from within R using:
```{r, eval=FALSE}
devtools::install_github("crsh/citr")
```
For more information about this package check out [https://github.com/crsh/citr](https://github.com/crsh/citr).
5. Once that package is installed. Quit and re-open RStudio. Now, if you go to the
"Addins" menu (right under the name panel at the top of the RStudio window) you will
see the option to "Insert citations." Choosing that brings up a dialog box. You can choose
the Zotero libraries to connect to. It might take a while to load your Zotero library if it
is large. Once it is loaded though, you just start typing the name of the author or part
of an article title, and _boom!_ if that article is in your library it appears as an option.
If you select it, you get a markdown citation in your text.
6. To avoid having to go to the "Addins" menu, you can set a keyboard shortcut for
"Insert citations" by choosing the "Code" section of RStudio's preferences and, under the
"Editing" tab, clicking the "Modify Keyboard Shortcuts" command, searching for
"Insert citations" and then selecting the keyboard shortcut area of the row and
keying in which keys you would like to give you the shortcut (for example,
Shift-CMD-I).
After those steps, you are set up to draw from your Zotero library or libraries
to insert citations into your R markdown document.
Pretty cool, but there are some things that are sort of painful---namely
the Title vs. Sentence casing. Fortunately, citr just adds things to your references.bib, it doesn't re-overwrote references.bib each time,
so you can edit references.bib to put titles in sentence case. Probably want to export without braces protecting capitals. Then it should all work. See [this discussion](https://forums.zotero.org/discussion/61715/prevent-extra-braces-in-bibtex-export). Just be sure to version control references.bib and commit it often. Though, you might want to go back and edit stuff in your Zotero library.
[@BarsonSexdependentdominancesingle2015]
## Bookdown
Whoa! Bookdown has figured out how to do references to sections and tables and things in a reasonable
way that just isn't there for the vanilla Rmarkdown. But you can use the bookdown syntax for a non-book
too. Just put something like this in the YAML:
```yaml
output:
bookdown::word_document2: default
bookdown::pdf_document2:
number_sections: yes
bookdown::html_document2:
df_print: paged
```
## Google Docs
This ain't reproducible research, but I really like the integration with Zotero. Perhaps
I need a chapter which is separate from this chapter that is about disseminating results and
submitting stuff, etc.