<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/composing-reproducible-manuscripts-using-r-markdown/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-posts.com/composing-reproducible-manuscripts-using-r-markdown/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The technology is available for researchers to create a reproducible manuscript whereby calculations and graphs are generated computationally – thereby saving researcher time and avoiding human error. <a target="_blank" rel="nofollow" href="https://elifesciences.org/?utm_source=rblogger&utm_campaign=RMkdwnApr17">eLife</a> is exploring ways to support life and biomedical scientists who wish to communicate their research in this way. In this post, Chris Hartgerink, a metascience researcher at Tilburg University, the Netherlands, describes how he composes a reproducible manuscript using R Markdown. For any researchers interested in submitting a manuscript to eLife in R Markdown, or a similar format, please let us know by email to <a target="_blank" rel="nofollow" href="mailto:innovation@elifesciences.org?subject=Submitting%20as%20rmd">innovation@elifesciences.org</a>.</p>
<p> </p>
<p>Blog post by Chris Hartgerink, Tilburg University</p>
<p>This week, a paper that was almost three years in the making <a target="_blank" rel="nofollow" href="https://doi.org/10.1525/collabra.71">finally got published</a> (Hartgerink et al., 2017). I feel confident about the paper and the results in it, not because it took three years to write, but because it’s the output of a dynamic document that I produced using R Markdown.</p>
<p>A dynamic document? This means that I no longer had to manually enter all results from the data into tables or the text – the computer did it for me. All I had to do was point it in the right direction. Figures? The same! It saved me tons of time after I made the initial investment to learn how to use it. (Something else that saved me time was <a target="_blank" rel="nofollow" href="https://github.com/chartgerink/2014tgtbf/commits/master">git version control</a>, but that’s for another time.)</p>
<p>Why is this important? We are all human, and we make mistakes. And that’s okay! What matters is how we try to remedy those mistakes if they do occur. Even more importantly, if we can change the way we work in order to prevent some of them, that can help us tremendously. I think dynamic documents like R Markdown help us do so.</p>
<p><a target="_blank" rel="nofollow" href="https://daringfireball.net/projects/markdown/">Markdown</a> is a simple document language, which you can create in any text editor, including Windows Notepad and Notes for Mac. Markdown is supported in multiple tools designed to make life and work easier, including Reddit, Hypothes.is, Github and several blogging and commenting tools. The purpose of Markdown is simply to define the document structure and formatting. For example, the writer can define header levels using hash signs, and easily format text (Figure 1).</p>
<p><img alt="" src="https://i0.wp.com/elifesciences.org/sites/default/files/news/hartgerink_post_fig_1.png?w=660&ssl=1" data-recalc-dims="1"></p>
<p><strong>Figure 1. Basic syntax of Markdown</strong></p>
<p>This makes formatting as you write quick – no more buttons or keyboard shortcuts. Subsequently, the text file can be exported in multiple file formats, including PDF, html and even as a Word file to satisfy co-authors who prefer to use Word’s track-changes function.</p>
<p>R Markdown is a flavour of Markdown: it allows you to include R code either as a code chunk between paragraphs of text, or as code within the text. This code is executable.￼ As such, you can do analyses, make figures and format results automatically.</p>
<p>One problem I’ve worked on previously is checking the accuracy of <em>p</em>-values in the literature. My colleagues and I found that <em>p</em>-values can be mistyped or miscalculated, leading to inaccurate reporting of results, however unintentionally, in half of all papers, and leading to potential changes in the conclusions in one out of eight papers (in psychology; Nuijten et al. 2015). Enabling researchers to insert <em>p</em>-values via direct computation instead of manually copying results from statistical programmes will resolve this issue, for a start.</p>
<p>R Markdown is simple and easy to learn and use. I will show just one exciting aspect here. More extensive <a target="_blank" rel="nofollow" href="http://rmarkdown.rstudio.com/articles_intro.html">step-by-step guides are available</a>. If you want to follow along, you will need to follow the steps described in Table 1.</p>
<table cellpadding="1" border="1" cellspacing="1">
<tbody>
<tr>
<th scope="row">Step 1</th>
<td>Install R from <a target="_blank" rel="nofollow" href="https://cran.r-project.org/">https://cran.r-project.org/</a> and Rstudio from <a target="_blank" rel="nofollow" href="https://www.rstudio.com/products/rstudio/download/">https://www.rstudio.com/products/rstudio/download/</a></td>
</tr>
<tr>
<th scope="row">Step 2</th>
<td>
In RStudio, install the R Markdown package by typing in the console:<br>
> install.packages(‘R Markdown’)<br>
You will need to select a CRAN local to you, from which the package will be downloaded.
</td>
</tr>
<tr>
<th scope="row">Step 3</th>
<td>Once R Markdown is installed, go to File > New File > R Markdown file. This loads a new .Rmd file with a basic template already set out for you.</td>
</tr>
</tbody>
</table>
<div><strong>Table 1. Getting started with R Markdown</strong></div>
<div> </div>
<div>Usually, we tend to type results in the running text ourselves, as depicted in Figure 2. By clicking “Knit”, R Markdown creates a document (right) from a simple plain-text file (left). This is the basic function of Markdown and its associated flavours.</div>
<div> </div>
<p><img alt="" src="https://i2.wp.com/elifesciences.org/sites/default/files/news/20170330-hartgerink-rmarkdown-fig2.png?w=660&ssl=1" data-recalc-dims="1"></p>
<p><strong>Figure 2. Using R Markdown to write a document</strong></p>
<p>However, in this text, we have a <em>p</em>-value that is calculated based on a <em>t</em>-value and the number of degrees of freedom. Let’s make this dynamic to ensure we have the rounding correct (Figure 3).</p>
<p><img alt="" src="https://i0.wp.com/elifesciences.org/sites/default/files/news/20170330-hartgerink-rmarkdown-fig3.png?w=660&ssl=1" data-recalc-dims="1"></p>
<p><strong>Figure 3. Using R Markdown to generate a document with dynamic results, to ensure accuracy of reporting and reproducibility of results</strong></p>
<p>As we see, the original contained a mistake (<em>p</em> = 0.027 has become <em>p</em> = 0.028) – using R Markdown allowed us to catch that error by using R code to generate and properly round the <em>p</em>-value (i.e., round(pt(q = 1.95, df = 69, lower.tail = FALSE), 3) calculates the <em>p</em>-value and rounds the result to three decimal places). So, there are no more mistakes, and we can be confident in our reporting. Disclaimer: of course, you can still input wrong code – garbage in, garbage out.</p>
<p>Further, you can generate plots from your research data within your dynamic document. Figure 4 demonstrates using an example dataset pre-loaded in R (you can find more example datasets by typing data() into the console).</p>
<p><img alt="" src="https://i0.wp.com/elifesciences.org/sites/default/files/news/2017-03-30-hartgerink-rmarkdown-fig4.png?w=660&ssl=1" data-recalc-dims="1"></p>
<p><strong>Figure 4. Using R Markdown to dynamically generate plots within a document</strong></p>
<p>Finally, you can even use citations and alter the citation style throughout the whole document without any problem; my experience is that it’s easier with R Markdown than with EndNote or Mendeley.</p>
<p>These are just simple examples. You can write entire manuscripts in this way. That’s what I did for our Collabra manuscript: you can view the raw manuscript at <a target="_blank" rel="nofollow" href="https://github.com/chartgerink/2014tgtbf/blob/master/submission/manuscript.Rnw">https://github.com/chartgerink/2014tgtbf/blob/master/submission/manuscript.Rnw</a>. (Note: I preferred LaTeX for that project and used Sweave, which is R Markdown for LaTeX.)</p>
<p>Using R Markdown presents the opportunity to link results and graphs in the article directly to the data and code that produce them. All it takes is some initial time investment to learn how to work with it (Markdown can be learned in five minutes) and change your workflow to accommodate this modern approach to writing manuscripts.</p>
<p>A downside to working this way is that raw R Markdown files are not currently supported as a submission format by journals, meaning that we need to export our manuscripts to traditional Word or PDF formats before submitting – stripping out that rich, reproducible layer of information R Markdown facilitates. I hope dynamic documents will become more and more widespread in the future, both in how often they’re used by the authors and how publishers support this type of document, to truly innovate how scholarly information is communicated and consumed. Imagine interacting with a statistical result or graph in a research article and being presented with the underlying code – this would allow you to more directly evaluate the methods in a paper and empower you as a reader to be critical of what you are presented with.</p>
<p><strong>References:</strong></p>
<p>Hartgerink, C.H.J., Wicherts, J.M. and van Assen, M.A.L.M., (2017). Too Good to be False: Nonsignificant Results Revisited. <em>Collabra: Psychology</em>. 3(1), p.9. <a target="_blank" rel="nofollow" href="https://doi.org/10.1525/collabra.71">https://doi.org/10.1525/collabra.71</a></p>
<p>Nuijten, M. B., Hartgerink, C. H. J., van Assen, M. A. L. M., Epskamp, S., and Wicherts, J. M. (2015). The prevalence of statistical reporting errors in psychology (1985–2013). <em>Behavior Research Methods</em>, 48(4), pp.1205–1226. <a target="_blank" rel="nofollow" href="https://doi.org/10.3758/s13428-015-0664-2">https://doi.org/10.3758/s13428-015-0664-2</a> </p>
<p> </p>
<p>This blog post is adapted from a post published by Chris Hartgerink at <a target="_blank" rel="nofollow" href="http://onsnetwork.org/chartgerink/2017/03/30/reproducible-manuscripts-are-the-future/">https://onsnetwork.org/chartgerink/2017/03/30/reproducible-manuscripts-are-the-future/</a>.</p>
<p>For more R news and tutorials, please visit <a target="_blank" rel="nofollow" href="https://www.r-bloggers.com/">https://www.r-bloggers.com/</a>.</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://r-posts.com/composing-reproducible-manuscripts-using-r-markdown/"> R-posts.com</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/7rvqdVvQG_E"></a></div>