An Introduction to Reproducible Research in R and R Studio.

**Updated 4th April 2016**

I have recently volunteered to give a few talks on how straightforward it us to conduct research in a reproducible manner using R and the R Studio environment. This talk covers the following topics:

  • What is reproducible research, and why should we do it?
  • Four simple rules for reproducibility.
  • A step by step guide for conducting reproducible research in R Studio

I intend to write on this topic in more detail in a future post, but for now if you are interested in changing the way you do research in R Studio, feel free to have a look through the slides below, which you can also download from here:

Make R beep when R Markdown finishes – or when it fails.

I often run time-consuming scripts, so I’m a big fan of ‘s Rasmus Bååth beepr package in R. I often put beep() at the end of my .R files to let me know when analyses are done.

However, when using R Markdown and knitr in RStudio, this is a poor solution when there is an error somewhere in my script. This is because knitr will terminate its R session before it gets to my beep() line at the end.

One way to get around this is to assign what R does on an error and what it does when it runs an .Rmd script successfully. Once the beepr package is installed, running the following markdown chunk at the beginning of the file will make .Rmd beep when it exits normally, or due to error:

```{r echo = F}
options(error = function(){    # Beep on error
  beepr::beep()
  Sys.sleep(1)
  }
 )

.Last <- function() {          # Beep on exiting session
  beepr::beep()
  Sys.sleep(1)
  }
```

The reason Sys.sleep(1) is there to give knitr some time to actually play the sounds before terminating.

NB. If you accidentally set this in your workspace outside knitr (i.e. running chunks in the console), getting a beep on every error can be super annoying. In that case, reset it with the following code:

options(error = NULL)

Hope someone finds this useful!