<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/writing-manuscripts-in-rstudio-easy-citations/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rmhogervorst.nl/cleancode/blog/2017/03/14/writing-manuscripts-in-rstudio.html"> Clean Code</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Intro and setup</h2>
<p>This is a simple explanation of how to write a manuscript in RStudio. Writing a manuscript in RStudio is not ideal, but it has gotten better over time. It is now relatively easy to add citations to documents in RStudio.</p>
<p>**The goal is not think about formatting, and citations, but to write the manuscript and add citations on the fly with a nice visual help. **</p>
<p>This tutorial explains how to link Zotero (a reference manager) to your project folder and how to easily add citations.<br>
In this tutorial I assume you have <a target="_blank" rel="nofollow" href="http://rmhogervorst.nl/cleancode/blog/2017/03/14/www.zotero.org">Zotero</a> installed.<br>
You will need to have the zotero extension <a target="_blank" rel="nofollow" href="https://github.com/retorquere/zotero-better-bibtex/wiki">betterBibtex</a> installed and I will<br>
use RStudio, with the packages knitr, shiny and other basic tooling in place.<br>
To search and insert citations in a file you will need to install the package<br>
<code>citr</code> install the latest <a target="_blank" rel="nofollow" href="https://github.com/crsh/citr"><em>dev-version from github</em></a> or the stable version from <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=citr">cran</a>.</p>
<h2>Why would I write my manuscript in RStudio?</h2>
<p>The main reasons to write in RStudio ( and not in f.i. Word ) are reproducibity, and version control. Writing in plain text as you do in RStudio makes it very easy to keep track of your work with version control and the plain files are easily shared. Working from RStudio also allows you to combine the code and text in one document. This is called <a title="wikipedia link" target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Literate_programming">literate programming</a>. Literate programming makes it incredibly easy to share your code (it’s in the document) and compiling the document runs the code again, ensuring reproducibility.</p>
<p>I use RStudio projects so everything<br>
important to a project is in the same folder and the working directory is automatically correct. I create a new RMarkdown document <img alt="rmarkdown doc" src="https://i0.wp.com/rmhogervorst.nl/cleancode/images/example_rstudio_manuscript.PNG?w=456" data-recalc-dims="1"></p>
<h4>Earlier workflows</h4>
<p>It was always possible to intersperse code and text in R, previously people used the <code>Sweave</code> package to combine code and text. You had to write the text in Latex and markup the code. Latex prints very pretty, but in raw form it is horrible to read, as you can see in the example:</p>
<div>
<pre><code><span>\documentclass</span><span>[12pt]</span><span>{</span>article<span>}</span>
<span>\usepackage</span><span>{</span>lingmacros<span>}</span>
<span>\usepackage</span><span>{</span>tree-dvips<span>}</span>
<span>\begin{document}</span>

<span>\section*</span><span>{</span>Notes for My Paper<span>}</span>

Don't forget to include examples of topicalization.
They look like this:

<span>{</span><span>\small</span>
<span>\enumsentence</span><span>{</span>Topicalization from sentential subject:<span>\\</span> 
<span>\shortex</span><span>{</span>7<span>}{</span>a John<span>$_</span><span>i</span><span>$</span> [a <span>&amp;</span> kltukl <span>&amp;</span> [el <span>&amp;</span> 
  <span>{</span><span>\bf</span> l-<span>}</span>oltoir <span>&amp;</span> er <span>&amp;</span> ngii<span>$_</span><span>i</span><span>$</span> <span>&amp;</span> a Mary]]<span>}</span>
<span>{</span> <span>&amp;</span> <span>{</span><span>\bf</span> R-<span>}</span>clear <span>&amp;</span> <span>{</span><span>\sc</span> comp<span>}</span> <span>&amp;</span> 
  <span>{</span><span>\bf</span> IR<span>}</span>.<span>{</span><span>\sc</span> 3s<span>}</span>-love   <span>&amp;</span> P <span>&amp;</span> him <span>&amp;</span> <span>}</span>
<span>{</span>John, (it's) clear that Mary loves (him).<span>}}</span>
<span>}</span>
</code></pre>
</div>
<p><em>I copied this from a example page.^[http://mally.stanford.edu/~sr/computing/latex-example.html]</em></p>
<p>The output looks beautiful, but it’s damn near unworkable in plain text. And even worse, mistakes are easily made and very hard to find (since you don’t know where the trouble started, and the error messages are quite cryptic).</p>
<h4>From latex / Sweave to RMarkdown</h4>
<p>Markdown is a very simple markup language that is readable for humans and can be translated into html / latex / word / etc by computers. In stead of \section{name fo the section} we can just use a pound symbol (a.k.a. hashtag: #) to mark a heading. One pound for a heading 1, 2 for heading 2 etc. The difference between markdown and RMarkdown is R-code in the text. There are plenty good tutorials out there so I will only give you one^[http://rmarkdown.rstudio.com/lesson-1.html] and a search online will give you more.</p>
<h4>Citations</h4>
<p>Citations connect your paper to the past, linking your research to earlier research. It is essential that people can find the correct paper or book you are referring to. For that reason (and vanity I think) there are many styles of citations. I’ve used the APA and Chicago style in the past and there are probably about 500. Every single one has different rules; cite inline by author name and year, or by number, etc.</p>
<p>If you resubmit your paper to a journal with a different citation style you have to rewrite every citation in the manuscript. Because we have better things to do, there are reference managers that automatically use the correct styling. And that change all the references in the text. I have used this in Word and LibreOffice in the past. But changing styles works a bit different in plain text formats such as markdown and latex. In general you cite research by a <strong>key</strong> that refers to a <strong>bibliography</strong>. In the build process the keys are replaced with the citations in the style that you want, so that the endproduct has the correct citations.<br>
This is also implemented in Latex and RStudio. In general the workflow is as follows:</p>
<ol>
<li>add the citations in the correct ‘language’ (bibtex) to your document or to a different document and provide a link to.</li>
<li>Use <strong>keys</strong> that refer to the correct entry in the bibtex.</li>
<li>The citation manager inserts the correctly styled reference in the correct location while it writes the final version.</li>
</ol>
<p>You write and cite like this:</p>
<p>“The ggplot2 package is used to plot images in layers [@wickham_ggplot2_2009].”</p>
<p>The key (a at-sign and name) refers to a bibtex entry. For example:</p>
<div>
<pre><code>@book{wickham_ggplot2_2009,
  address = {New York, NY},
  title = {Ggplot2},
  isbn = {978-0-387-98140-6 978-0-387-98141-3},
  language = {en},
  timestamp = {2017-02-19T10:19:27Z},
  urldate = {2015-11-09},
  url = {http://ggplot2.org},
  publisher = ,
  author = {Wickham, Hadley},
  year = {2009},
  note = {04749},
  file = {ggplot2 - hadley wickham.pdf:C\:\\Users\\roel\\AppData\\Roaming\\Zotero\\Zotero\\Profiles\\aw40nx7l.default\\zotero\\storage\\AADU9XVP\\ggplot2 - hadley wickham.pdf:application/pdf}
}
</code></pre>
</div>
<p>Which will resolve to (if you use APA formatting):</p>
<blockquote>
<p>The ggplot2 package is used to plot images in layers (Wickham, 2009).</p>
</blockquote>
<h3>Bibtex files creation</h3>
<p>Now, making your own bibtex files is a serious pain in the backside. So we don’t do that. We also don’t try to remember all the keys, but first about the creation of the bibtex file.</p>
<p>I don’t want to keep track of the correct way to cite sources, that is just a waste of time. We have excellent reference managers for this goal. I use Zotero. I just throw the pdfs in the correct folder and Zotero finds the correct metadata using the pdfs DOI (digital object identifier).<br>
This is how it looks on my computer<br>
<img alt="a snapshot of zotero on my pc" src="https://i1.wp.com/rmhogervorst.nl/cleancode/images/example_zotero_look.PNG?w=456" data-recalc-dims="1"></p>
<p>I use the extension <a target="_blank" rel="nofollow" href="http://rmhogervorst.nl/cleancode/blog/2017/03/14/link">betterbibtex</a>, that adds extra functionality to Zotero. In this case it also takes care of synchronizing the bibtext file that you export to the folder you’re working in and the corresponding collection in Zotero. (see further on).</p>
<p>I export the collection from zotero into the rstudio folder. <img alt="zotero keep updated image" src="https://i2.wp.com/rmhogervorst.nl/cleancode/images/example_zotero_keep_updated.PNG?w=456" data-recalc-dims="1"></p>
<h4>The RMarkdown document</h4>
<p>If you open a new rmarkdown document from the RStudio console a basic document with metadata is already opened.<br>
The top of your manuscript should contain metadata such as the title output and in this case we also need to append it to show where to find the bibliography.:<br>
For example we make a html document called sample_Document with a bibliography based on a document bibliography.bib found in the same folder.</p>
<div>
<pre><code>---
title: &quot;Sample Document&quot;
output: html_document
bibliography: bibliography.bib
---
</code></pre>
</div>
<p>While writing the manuscript we cite like so <img alt="image of citing" src="https://i2.wp.com/rmhogervorst.nl/cleancode/images/example_adding_citations_rmarkdown.PNG?w=456" data-recalc-dims="1"></p>
<p>For more information about citations and bibliographies look at the RStudio website^[<a target="_blank" rel="nofollow" href="http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html">http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html</a>]</p>
<h4>Making citing easier with the help of CITR</h4>
<p>Then use the citr package <a target="_blank" rel="nofollow" href="https://github.com/crsh/citr">https://github.com/crsh/citr</a>.<br>
<em>Citr works quite nicely, but unfortunately it has recently acquired default behavior that is frankly annoying. If Zotero is active (open in a window on your computer), citr will ignore the bibliography you mentioned in the top of the document and just try to read in the entire zotero library. (which will fail on my computer) I had to change a setting to change this.</em>^[ you have to change <code>options(citr.use_betterbiblatex = FALSE)</code> adding this to .Renviron works].</p>
<p>Loading citations:<br>
<img alt="image of loading citr" src="https://i1.wp.com/rmhogervorst.nl/cleancode/images/example_loading_citr.PNG?w=456" data-recalc-dims="1"></p>
<p>Adding a citation to a document:<br>
<img alt="image of adding a citation" src="https://i1.wp.com/rmhogervorst.nl/cleancode/images/example_adding_citation_with_citr.PNG?w=456" data-recalc-dims="1"></p>
<p>This adds the correct citation as you can see</p>
<p><img alt="side by side comparison" src="https://i2.wp.com/rmhogervorst.nl/cleancode/images/example_citation_side_by_side.PNG?w=456" data-recalc-dims="1"></p>
<h2>Conclusion</h2>
<p>This piece became a bit longer then anticipated but I discovered new behavior of <code>citr</code>, so there’s that.<br>
All in all RStudio / R and related tools can become the complete workbench for reading in data, cleaning and analyzing, and also writing the manuscript.</p>
<p>For practical examples of reproducible work see this book^[https://www.practicereproducibleresearch.org/]</p>
<h2>References</h2>
<p><a target="_blank" rel="nofollow" href="http://rmhogervorst.nl/cleancode/blog/2017/03/14/writing-manuscripts-in-rstudio.html">Writing manuscripts in Rstudio, easy citations</a> was originally published by at <a target="_blank" rel="nofollow" href="http://rmhogervorst.nl/cleancode">Clean Code</a> on March 14, 2017.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://rmhogervorst.nl/cleancode/blog/2017/03/14/writing-manuscripts-in-rstudio.html"> Clean Code</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/oUTLrvwx_cQ"></a></div>