When discussing performance with colleagues, teaching, sending a bug report or searching for guidance on mailing lists and here on Stack Overflow, a reproducible example is often asked and always helpful.
What are your tips for creating an excellent example? How do you paste data structures from r in a text format? What other information should you include?
Are there other tricks in addition to using dput()
, dump()
or structure()
? When should you include library()
or require()
statements? Which reserved words should one avoid, in addition to c
, df
, data
, etc.?
How does one make a great r reproducible example?
Guidelines:
Your main objective in crafting your questions should be to make it as easy as possible for readers to understand and reproduce your problem on their systems. To do so:
This does take some work but seems like a fair trade-off since you are asking others to do work for you.
Providing Data:
Built-in Data Sets
The best option by far is to rely on built-in datasets. This makes it very easy for others to work on your problem. Type
data()
at the R prompt to see what data is available to you. Some classic examples:iris
mtcars
ggplot2::diamonds
(external package, but almost everyone has it)See this SO QA for how to find data sets suitable for your problem.
If you are able to rephrase your problem to use the built-in datasets you are much more likely to get good answers (and upvotes).
Self Generated Data
If your problem is very specific to a type of data that is not represented in the existing data sets, then provide the R code that generates the smallest possible dataset that your problem manifests itself on. For example
Now someone trying to answer my question can copy/paste those two lines and start working on the problem immediately.
dput
As a last resort, you can use
dput
to transform a data object to R code (e.g.dput(myData)
). I say as a "last resort" because the output ofdput
is often fairly unwieldy, annoying to copy-paste, and obscures the rest of your question.Provide Expected Output:
Someone once said:
If you can add something like "I expected to get this result":
to your question, people are much more likely to quickly understand what you are trying to do. If your expected result is large and unwieldy, then you probably haven't thought enough about how to simplify your problem (see next).
Explain Your Problem Succinctly
The main thing to do is to simplify your problem as much as possible before you ask your question. Re-framing the problem to work with the built-in datasets will help a lot in this regard. You will also often find that just by going through the process of simplification you will answer your own problem.
Here are some examples of good questions:
In both cases, the user's problems are almost certainly not with the simple examples they provide. Rather they abstracted the nature of their problem and applied it to a simple data set to ask their question.
Why Yet Another Answer To This Question?
This answer focuses on what I think is the best practice: use built-in data sets and provide what you expect as a result in a minimal form. The most prominent answers focus on other aspects. I don't expect this answer to rising to any prominence; this is here solely so that I can link to it in comments to newbie questions.
If you have large dataset which cannot be easily put to the script using
dput()
, post your data to pastebin and load them usingread.table
:Inspired by @Henrik.
The answers so far are obviously great for the reproducibility part. This is merely to clarify that a reproducible example cannot and should not be the sole component of a question. Don't forget to explain what you want it to look like and the contours of your problem, not just how you have attempted to get there so far. Code is not enough; you need words also.
Here's a reproducible example of what to avoid doing (drawn from a real example, names changed to protect the innocent):
The following is sample data and part of function I have trouble with.
How can I achieve this ?
Apart of all above answers which I found very interesting, it could sometimes be very easy as it is discussed here :- HOW TO MAKE A MINIMAL REPRODUCIBLE EXAMPLE TO GET HELP WITH R
There are many ways to make a random vector Create a 100 number vector with random values in R rounded to 2 decimals or random matrix in R
Note that sometimes it is very difficult to share a given data because of various reasons such as dimension etc. However, all above answers are great and very important to think and use when one wants to make a reproducible data example. But note that in order to make a data as representative as the original (in case the OP cannot share the original data), it is good to add some information with the data example as (if we call the data mydf1)
Moreover, one should know the type, length and attributes of a data which can be Data structures
To quickly create a
dput
of your data you can just copy (a piece of) the data to your clipboard and run the following in R:for data in Excel:
for data in a txt file:
You can change the
sep
in the latter if necessary. This will only work if your data is in the clipboard of course.Personally, I prefer "one" liners. Something along the lines:
The data structure should mimic the idea of writer's problem and not the exact verbatim structure. I really appreciate it when variables don't overwrite my own variables or god forbid, functions (like
df
).Alternatively, one could cut a few corners and point to a pre-existing data set, something like:
Don't forget to mention any special packages you might be using.
If you're trying to demonstrate something on larger objects, you can try
If you're working with spatial data via the
raster
package, you can generate some random data. A lot of examples can be found in the package vignette, but here's a small nugget.If you're in need of some spatial object as implemented in
sp
, you can get some datasets via external files (like ESRI shapefile) in "spatial" packages (see the Spatial view in Task Views).