Writing transparent papers using R Markdown

DEC 12th 2017 I’ve recently started writing my papers in R Markdown. R Markdown is a feature of R which allows you to integrate text and code. The end result is a script that, based on your raw data, produces the final version of your manuscript. This includes figures with fancy layouts and -notably- the output of analyses. I really like this approach, as it results in fully transparent papers – every step up to your final figures and stats is fully tractable and reproducible with a single click!

Here’s an example I’ve copy-pasted from Hadley Wickam’s brilliant (free!) book: ‘R for Data Science’:

There is one other way to embed R code into an R Markdown document: directly into the text, with: `r `. This can be very useful if you mention properties of your data in the text. For example, in the example document I used at the start of the chapter I had:

We have data about `r nrow(diamonds)` diamonds. Only `r nrow(diamonds) - nrow(smaller)` are larger than 2.5 carats. The distribution of the remainder is shown below:

When the report is knit, the results of these computations are inserted into the text:

We have data about 53940 diamonds. Only 126 are larger than 2.5 carats. The distribution of the remainder is shown below:

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s