FAQ: What's a reproducible example (`reprex`) and how do I create one?

Why reprex?

Getting unstuck is hard. Your first step here is usually to create a reprex, or reproducible example. The goal of a reprex is to package your code, and information about your problem so that others can run it and feel your pain. Then, hopefully, folks can more easily provide a solution.

What's in a Reproducible Example?

Parts of a reproducible example:

  1. background information - Describe what you are trying to do. What have you already done?
  2. complete set up - include any library() calls and data to reproduce your issue.
    data for a reprex: Here's a discussion on setting up data for a reprex
  3. make it run - include the minimal code required to reproduce your error on the data provided.
    People should be able to copy and paste your code chunk and get the same error.
    How do I format my text so it has nice code chunks?
  4. minimal - strip away everything that is not directly related to your problem. This usually involves creating a much smaller and simpler set of code and data compared to that which created your issue.

Use the reprex-package to Build Your Reproducible Example

When creating a reprex by hand, it’s easy to accidentally miss something that means your code can’t be run on someone else’s computer. Avoid this problem by using the reprex-package.
The reprex package will save effort for you and others who want to help.

Personal and Private Information

Users should not share personal or protected information in topic threads or reprex data.
What if you need to share contact info or an ip, or discuss data for a reprex related to a protected dataset? What if you see a violation of this policy? Check out Personally Identifiable & Protected Information Guide guide and our Privacy Policy


Beginners - Step by Step Help

Help asking R-related questions (not specific to the reprex-package)

Background on the reprex-package

7 Likes

A post was split to a new topic: Best Practices: how to prepare your own data for use in a reprex if you can’t, or don’t know how to reproduce a problem with a built-in dataset?

Thanks, Miles, this is great!

A few more resources to add:


Not reprex-specific, but helpful for asking R-related questions

2 Likes

17 posts were split to a new topic: Reprex vs. reprex-package discussions

For sharing simple data.frames (those containing only basic types, no dates, no factors, and no row names) I suggest using wrapr::draw_frame() to build sharable examples.

For example suppose our example was the following data.

d <- head(ggplot2::diamonds) 

wrapr::draw_frame can share this data in a very legible form:

library("wrapr")
cat(draw_frame(d))

This outputs the following (older versions of wrapr do not add the "::" qualifier).

wrapr::build_frame(
   "carat", "cut"      , "color", "clarity", "depth", "table", "price", "x" , "y" , "z"  |
   0.23   , "Ideal"    , "E"    , "SI2"    , 61.5   , 55     , 326L   , 3.95, 3.98, 2.43 |
   0.21   , "Premium"  , "E"    , "SI1"    , 59.8   , 61     , 326L   , 3.89, 3.84, 2.31 |
   0.23   , "Good"     , "E"    , "VS1"    , 56.9   , 65     , 327L   , 4.05, 4.07, 2.31 |
   0.29   , "Premium"  , "I"    , "VS2"    , 62.4   , 58     , 334L   , 4.2 , 4.23, 2.63 |
   0.31   , "Good"     , "J"    , "SI2"    , 63.3   , 58     , 335L   , 4.34, 4.35, 2.75 |
   0.24   , "Very Good", "J"    , "VVS2"   , 62.8   , 57     , 336L   , 3.94, 3.96, 2.48 )

The point is, with the wrapr package loaded the above output is actually executable code that produces the same data.frame. One can then copy and paste the above code to start a fresh example from this data (and not need to include steps that took one to this point).

(Was asked to post this to this thread here.)

4 Likes

It is now out (and yes, it is great!)

1 Like