17  Reproducible examples

17.1 Why use reprex?

The motivation below is copied directly from: https://www.tidyverse.org/help/

If you need help getting unstuck, the first step is to create a reprex, or reproducible example. The goal of a reprex is to package your problematic code in such a way that other people can run it and feel your pain. Then, hopefully, they can provide a solution and put you out of your misery.

There are two parts to creating a reprex:

  • First, you need to make your code reproducible. This means that you need to capture everything, i.e., include any library() calls and create all necessary objects. The easiest way to make sure you’ve done this is to use the reprex package.

  • Second, you need to make it minimal. Strip away everything that is not directly related to your problem. This usually involves creating a much smaller and simpler R object than the one you’re facing in real life or even using built-in data.

That sounds like a lot of work! And it can be, but it has a great payoff:

  • 80% of the time creating an excellent reprex reveals the source of your problem. It’s amazing how often the process of writing up a self-contained and minimal example allows you to answer your own question.

  • The other 20% of time you will have captured the essence of your problem in a way that is easy for others to play with. This substantially improves your chances of getting help!

17.2 reprex()

Help me help you

In order to create a reproducible example …

Step 1. Write some R code and copy it into the clipboard (command-c or ctrl-c).

Step 2. Type reprex() into the Console.

Step 3. Look at the Viewer to the right. Copy the Viewer output into GitHub, Slack, an email, Stack Overflow, Posit Community, etc.

Some places to learn more about reprex include

  • A blog about it: https://teachdatascience.com/reprex/
  • The reprex vignette: https://reprex.tidyverse.org/index.html
  • reprex dos and donts: https://reprex.tidyverse.org/articles/reprex-dos-and-donts.html
  • Jenny Bryan webinar on reprex: “Help me help you. Creating reproducible examples” https://resources.rstudio.com/webinars/help-me-help-you-creating-reproducible-examples-jenny-bryan
  • Some advice: https://stackoverflow.com/help/minimal-reproducible-example

17.3 reprex demo

Let’s say I want to send out a query to help me understand what happens when I add months and days to January 31.

17.3.1 First try

Code:

jan31 + months(0:11) + days(31)

Output of reprex():

jan31 + months(0:11) + days(31)
#> Error in eval(expr, envir, enclos): object 'jan31' not found

The error tells me that there isn’t an object called jan31. So if I want my friend to try to reproduce my results, they won’t be able to do it on their computer (assuming they don’t have an object named jan31).

17.3.2 Second try

Code:

  jan31 <- ymd("2021-01-31")
  jan31 + months(0:11) + days(31)

Output of reprex():

  jan31 <- ymd("2021-01-31")
#> Error in ymd("2021-01-31"): could not find function "ymd"
  jan31 + months(0:11) + days(31)
#> Error in eval(expr, envir, enclos): object 'jan31' not found

Now the error is that the ymd() function can’t be found. That is because I didn’t load in the lubridate package!

17.3.3 Third try

Code:

  library(lubridate)
  jan31 <- ymd("2021-01-31")
  jan31 + months(0:11) + days(31)

Output of reprex():

  library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
  jan31 <- ymd("2021-01-31")
  jan31 + months(0:11) + days(31)
#>  [1] "2021-03-03" NA           "2021-05-01" NA           "2021-07-01"
#>  [6] NA           "2021-08-31" "2021-10-01" NA           "2021-12-01"
#> [11] NA           "2022-01-31"

On the third try, I get the output that I expect to get (and don’t understand)! I can now copy the output of the reprex and send it to a friend or message board to get help understand why the output is what it is.

17.4 Reflection questions

17.5 Ethics considerations