<html lang="en"> <head> <title>R Markdown</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link href="https://unpkg.com/@stencila/thema@2/dist/themes/tufte/styles.css" rel="stylesheet"> <script src="https://unpkg.com/@stencila/thema@2/dist/themes/tufte/index.js" type="text/javascript"></script> <script src="https://unpkg.com/@stencila/components@<=1/dist/stencila-components/stencila-components.esm.js" type="module"></script> <script src="https://unpkg.com/@stencila/components@<=1/dist/stencila-components/stencila-components.js" type="text/javascript" nomodule=""></script> </head> <body> <main role="main"> <article itemscope="" itemtype="http://schema.org/Article" data-itemscope="root"> <h1 itemprop="headline">R Markdown</h1> <meta itemprop="image" content="https://via.placeholder.com/1200x714/dbdbdb/4a4a4a.png?text=R%20Markdown"> <ol data-itemprop="authors"> <li itemscope="" itemtype="http://schema.org/Person" itemprop="author"> <meta itemprop="name" content="Nokome Bentley"><span data-itemprop="givenNames"><span itemprop="givenName">Nokome</span></span><span data-itemprop="familyNames"><span itemprop="familyName">Bentley</span></span> </li> </ol><span itemscope="" itemtype="http://schema.org/Organization" itemprop="publisher"> <meta itemprop="name" content="Unknown"><span itemscope="" itemtype="http://schema.org/ImageObject" itemprop="logo"> <meta itemprop="url" content="https://via.placeholder.com/600x60/dbdbdb/4a4a4a.png?text=Unknown"> </span> </span> <section data-itemprop="description"> <h2 data-itemtype="http://schema.stenci.la/Heading">Abstract</h2> <meta itemprop="description" content="Authoring executable documents with R Markdown"> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">Authoring executable documents with R Markdown</p> </section> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">R Markdown is a popular format for reproducible research. This article provides a guide to how to write, and collaborate on, a reproducible research article using R Markdown and Stencila. As well as describing the basics of embedding R code in your article, it covers how to include meta-data (such as author names and affiliations), citations and references. Finally, we discuss how you can preview your article and convert it to other formats using Stencila.</p> <h2 itemscope="" itemtype="http://schema.stenci.la/Heading" id="introduction">Introduction </h2> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph"><a href="https://rmarkdown.rstudio.com/" itemscope="" itemtype="http://schema.stenci.la/Link">R Markdown</a> is a popular format for writing reproducible documents. <a href="https://daringfireball.net/projects/markdown/syntax" itemscope="" itemtype="http://schema.stenci.la/Link">Markdown</a> is a simple text format, which, in the words of it's creator, is "intended to be as easy-to-read and easy-to-write as is feasible". R Markdown extends Markdown with some custom syntax for embedding executable R code.</p> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">There are many resources available for how to use R Markdown for reproducible research, including:</p> <ul itemscope="" itemtype="http://schema.org/ItemList"> <li itemscope="" itemtype="http://schema.org/ListItem" itemprop="itemListElement"> <meta itemprop="position" content="1"> <meta itemprop="url" content="#1">Chris Hartgerink (2017) <em itemscope="" itemtype="http://schema.stenci.la/Emphasis">Composing reproducible manuscripts using R Markdown</em> <a href="https://elifesciences.org/labs/cad57bcf/composing-reproducible-manuscripts-using-r-markdown" itemscope="" itemtype="http://schema.stenci.la/Link">https://elifesciences.org/labs/cad57bcf/composing-reproducible-manuscripts-using-r-markdown</a> </li> <li itemscope="" itemtype="http://schema.org/ListItem" itemprop="itemListElement"> <meta itemprop="position" content="2"> <meta itemprop="url" content="#2">Daniel Nüst, Vicky Steeves, Rémi Rampin, Markus Konkol, Edzer Pebesma (2018)<em itemscope="" itemtype="http://schema.stenci.la/Emphasis">Writing reprocible geoscience papers using R Markdown, Docker, and GitLab</em> <a href="https://vickysteeves.gitlab.io/repro-papers/index.html" itemscope="" itemtype="http://schema.stenci.la/Link">https://vickysteeves.gitlab.io/repro-papers/index.html</a> </li> <li itemscope="" itemtype="http://schema.org/ListItem" itemprop="itemListElement"> <meta itemprop="position" content="3"> <meta itemprop="url" content="#3">Paul Bauer (2018) <em itemscope="" itemtype="http://schema.stenci.la/Emphasis">Writing a Reproducible Paper in R Markdown</em> <a href="https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3175518" itemscope="" itemtype="http://schema.stenci.la/Link">https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3175518</a> </li> </ul> <h2 itemscope="" itemtype="http://schema.stenci.la/Heading" id="code-chunks">Code chunks </h2> <h3 itemscope="" itemtype="http://schema.stenci.la/Heading" id="the-basics">The basics</h3> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">At the heart of R Markdown are "code chunks". Code chunks can produce outputs, or they can be used to execute arbitrary R code. For example, here we assign a variable called <code itemscope="" itemtype="http://schema.stenci.la/CodeFragment">randoms</code> which will be an array of 1000 random numbers sampled from a normal distribution:</p> <stencila-code-chunk itemscope="" itemtype="http://schema.stenci.la/CodeChunk" data-programminglanguage="r"> <pre class="language-r" itemscope="" itemtype="http://schema.stenci.la/CodeBlock" slot="text"><code>randoms <- rnorm(1000)</code></pre> </stencila-code-chunk> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">When you assign a variable in a code chunk you can reuse it in another chunk, later in the document. For example, we can plot the frequency distribution of the random numbers we assigned to the <code itemscope="" itemtype="http://schema.stenci.la/CodeFragment">randoms</code> variable earlier:</p> <stencila-code-chunk itemscope="" itemtype="http://schema.stenci.la/CodeChunk" data-programminglanguage="r"> <pre class="language-r" itemscope="" itemtype="http://schema.stenci.la/CodeBlock" slot="text"><code>hist(randoms, breaks=30, col="grey", main="")</code></pre> </stencila-code-chunk> <h3 itemscope="" itemtype="http://schema.stenci.la/Heading" id="adding-figure-and-table-captions">Adding figure and table captions</h3> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">R Markdown allows you to specify "options" for code chunks. One such option is <code itemscope="" itemtype="http://schema.stenci.la/CodeFragment">fig.cap</code> which allows you to specify a figure caption:</p> <figure itemscope="" itemtype="http://schema.stenci.la/Figure"> <stencila-code-chunk itemscope="" itemtype="http://schema.stenci.la/CodeChunk" data-programminglanguage="r"> <pre class="language-r" itemscope="" itemtype="http://schema.stenci.la/CodeBlock" slot="text"><code>plot(randoms)</code></pre> </stencila-code-chunk> <figcaption> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">My figure</p> </figcaption> </figure> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">That is OK for short captions, but when you have longer captions you will probably want to use Bookdown style text references e.g.</p> <figure itemscope="" itemtype="http://schema.stenci.la/Figure"> <stencila-code-chunk itemscope="" itemtype="http://schema.stenci.la/CodeChunk" data-programminglanguage="r"> <pre class="language-r" itemscope="" itemtype="http://schema.stenci.la/CodeBlock" slot="text"><code>plot(randoms)</code></pre> </stencila-code-chunk> <figcaption> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">This is a slightly longer caption for my figure including some <strong itemscope="" itemtype="http://schema.stenci.la/Strong">strong</strong> emphasis.</p> </figcaption> </figure> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">Bookdown style text references are good for longer, single paragraph captions. However, for more structured captions having a title and paragraph as is often found in academic journals we suggest that you use a code chunk block extension. e.g.</p> <figure itemscope="" itemtype="http://schema.stenci.la/Figure" title="Figure 3"><label data-itemprop="label">Figure 3</label> <stencila-code-chunk itemscope="" itemtype="http://schema.stenci.la/CodeChunk" data-programminglanguage="r"> <pre class="language-r" itemscope="" itemtype="http://schema.stenci.la/CodeBlock" slot="text"><code>plot(randoms)</code></pre> </stencila-code-chunk> <figcaption> <h4 itemscope="" itemtype="http://schema.stenci.la/Heading" id="the-title-of-my-plot"> The title of my plot</h4> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">A paragraph for my figure including some <strong itemscope="" itemtype="http://schema.stenci.la/Strong">strong</strong> emphasis.</p> </figcaption> </figure> <h2 itemscope="" itemtype="http://schema.stenci.la/Heading" id="inline-code-chunks">Inline code chunks</h2> <p itemscope="" itemtype="http://schema.stenci.la/Paragraph">In R Markdown you can also inline "inline" code chunks within paragraphs and other text content. For example, here is the mean of the <code itemscope="" itemtype="http://schema.stenci.la/CodeFragment">randoms</code> variable that we assigned earlier: <stencila-code-expression programming-language="r" itemscope="" itemtype="http://schema.stenci.la/CodeExpression"><code class="r" slot="text">mean(randoms)</code><output slot="output"></output> </stencila-code-expression>. In Stencila, we call these <code itemscope="" itemtype="http://schema.stenci.la/CodeFragment">CodeExpression</code>s, because they are intended to display a calculated value, and shouldn't be used for statements, such as assigning a variable.</p> </article> </main> </body> </html>