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?
Reproducible code is key to get help. However, there are many users that might be skeptical of pasting even a chunk of their data. For instance, they could be working with sensitive data or on an original data collected to use in a research paper. For any reason, I thought it would be nice to have a handy function for "deforming" my data before pasting it publicly. The
anonymize
function from the packageSciencesPo
is very silly, but for me it works nicely withdput
function.Then I anonymize it:
One may also want to sample few variables instead of the whole data before apply anonymization and dput command.
Please do not paste your console outputs like this:
We can not copy-paste it directly.
To make questions and answers properly reproducible, try to remove
+
&>
before posting it and put#
for outputs and comments like this:One more thing, if you have used any function from certain package, mention that library.
Here is a good guide.
The most important point is: Just make sure that you make a small piece of code that we can run to see what the problem is. A useful function for this is
dput()
, but if you have very large data, you might want to make a small sample dataset or only use the first 10 lines or so.EDIT:
Also make sure that you identified where the problem is yourself. The example should not be an entire R script with "On line 200 there is an error". If you use the debugging tools in R (I love
browser()
) and Google you should be able to really identify where the problem is and reproduce a trivial example in which the same thing goes wrong.Often you need some data for an example, however, you don't want to post your exact data. To use some existing data.frame in established library, use data command to import it.
e.g.,
and then do the problem
Inspired by this very post, I now use a handy function
reproduce(<mydata>)
when I need to post to StackOverflow.QUICK INSTRUCTIONS
If
myData
is the name of your object to reproduce, run the following in R:Details:
This function is an intelligent wrapper to
dput
and does the following:dput
outputobjName <- ...
so that it can be easily copy+pasted, but...The source is available here:
Example:
DF is about 100 x 102. I want to sample 10 rows, and a few specific columns
Gives the following output:
Notice also that the entirety of the output is in a nice single, long line, not a tall paragraph of chopped up lines. This makes it easier to read on SO questions posts and also easier to copy+paste.
Update Oct 2013:
You can now specify how many lines of text output will take up (ie, what you will paste into StackOverflow). Use the
lines.out=n
argument for this. Example:reproduce(DF, cols=c(1:3, 17, 23), lines.out=7)
yields:The R-help mailing list has a posting guide which covers both asking and answering questions, including an example of generating data:
The word small is especially important. You should be aiming for a minimal reproducible example, which means that the data and the code should be as simple as possible to explain the problem.
EDIT: Pretty code is easier to read than ugly code. Use a style guide.